Spring3, Hibernate3.6 Proxool接続プール構成


Spring 3.0サーブレット起動を使用せずにlistenerを使用し、MysqlとDBP接続プールがlinuxサーバ上でタイムアウト接続されているBugに対して、Spring 3とProxool接続プールの構成を簡単に行います.
1.Web.xml構成:


<?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.