Spring3, Hibernate3.6與Proxool鏈接池配置

Proxool鏈接池Spring3.1Hibernate3.6

鑑於Spring3.0不採用Servlet啓動,改用listener,而且針對Mysql與DBCP鏈接池在linux服務器上超時鏈接的Bug,現簡要地作Spring3與Proxool鏈接池的配置:

1.Web.xml配置:
Java代碼
  1. <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!--啓動鏈接池--> <servlet> <servlet-name>ServletConfigurator</servlet-name> <servlet-class> org.logicalcobwebs.proxool.configuration.ServletConfigurator </servlet-class> <init-param> <param-name>xmlFile</param-name> <param-value> WEB-INF/proxool.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--啓動spring(spring3.1)--> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/classes/com/dx/bags/config/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app> proxool.xml<位於WEB-INF目錄下,與web.xml同目錄> [b]本文中因爲有多數據庫鏈接,因而,作了多個配置。alias分別爲db12,db10,db5[/b] <?xml version="1.0" encoding="UTF-8"?> <proxool-config> <proxool> <alias>db12</alias> <driver-url>jdbc:mysql://192.168.1.12:3306/dbname1</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="root" /> <property name="password" value="*******" /> </driver-properties> <maximum-new-connections>10</maximum-new-connections> <prototype-count>5</prototype-count> <test-before-use>true</test-before-use> <test-after-use>true</test-after-use> <house-keeping-sleep-time>60000</house-keeping-sleep-time> <house-keeping-test-sql>select current_date from dual</house-keeping-test-sql> <maximum-connection-count>10</maximum-connection-count> <minimum-connection-count>2</minimum-connection-count> </proxool> <proxool> <alias>db10</alias> <driver-url>jdbc:mysql://192.168.1.10:3306/dbname2</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="root" /> <property name="password" value="******" /> </driver-properties> <maximum-new-connections>10</maximum-new-connections> <prototype-count>5</prototype-count> <test-before-use>true</test-before-use> <test-after-use>true</test-after-use> <house-keeping-sleep-time>60000</house-keeping-sleep-time> <house-keeping-test-sql>select current_date from dual</house-keeping-test-sql> <maximum-connection-count>10</maximum-connection-count> <minimum-connection-count>2</minimum-connection-count> </proxool> <proxool> <alias>db5</alias> <driver-url>jdbc:mysql://192.168.1.5:3306/dbname3</driver-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <driver-properties> <property name="user" value="***" /> <property name="password" value="****" /> </driver-properties> <maximum-new-connections>10</maximum-new-connections> <prototype-count>5</prototype-count> <test-before-use>true</test-before-use> <test-after-use>true</test-after-use> <house-keeping-sleep-time>60000</house-keeping-sleep-time> <house-keeping-test-sql>select current_date from dual</house-keeping-test-sql> <maximum-connection-count>10</maximum-connection-count> <minimum-connection-count>2</minimum-connection-count> </proxool> </proxool-config> Hibernate.cfg.xml配置: 文件位於src目錄下。 <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="myeclipse.connection.profile">mysql</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.proxool.existing_pool">true</property> <property name="hibernate.proxool.xml">proxool.xml</property> <property name="hibernate.proxool.pool_alias">db12</property> <property name="hibernate.proxool.pool_alias">db10</property> <property name="hibernate.proxool.pool_alias">db5</property> </session-factory> </hibernate-configuration> Spring 配置文件中對於datasource和sessionFactory配置片段: <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.db12</value> </property> </bean> <bean id="latestDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.db5</value> </property> </bean> <bean id="adminDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.logicalcobwebs.proxool.ProxoolDriver</value> </property> <property name="url"> <value>proxool.db10</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/dx/bags/hibernate/hbm</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.proxool.pool_alias"> db12 </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.proxool.xml">proxool.xml</prop> <prop key="hibernate.proxool.existing_pool">true</prop> </props> </property> </bean> <bean id="latestdbSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="latestDataSource"></property> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/dx/bags/hibernate/hbm/latest</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.proxool.pool_alias"> db5 </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.proxool.xml">proxool.xml</prop> <prop key="hibernate.proxool.existing_pool">true</prop> </props> </property> </bean> <bean id="adminSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="adminDataSource"></property> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/dx/bags/hibernate/hbm/admin</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.proxool.pool_alias"> db10 </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.proxool.xml">proxool.xml</prop> <prop key="hibernate.proxool.existing_pool">true</prop> </props> </property>   </bean>
關於proxool配置文件中的各參數就很少作說明了。很簡單易懂。鏈接池所要JAR:proxool-0.9.1.jar,proxool-cglib.jar。
相關文章
相關標籤/搜索