鉴于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。 

posted on 2012-04-19 02:33  星^_^風  阅读(279)  评论(0编辑  收藏  举报