hibernate各种连接池的比较

时间:2022-09-20 07:51:53
hibernate开发组推荐c3p0,spring开发组推荐dbcp,但是dbcp连接池有weblogic连接池同样的问题,就是强行关闭连接或数据库重启后,无法 reconnect ,告诉连接被重置,这个设置可以解决。hibernate in action推荐c3p0和proxool。

强烈推荐proxool,因为他不但可以监控后台。还可以有效的释放连接。在connection close时,也就是归还connection,
关闭所有的statement,并且判断是否autocommit,如果不行,就rollback,并且设置true,
可以参考proxool的org.logicalcobwebs.proxool.ConnectionResetter类
connection pool把connection reset置回initial state。
dbcp的配置
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>

 <property name="validationQuery">
<value>SELECT 1</value>
</property>
<property name="testOnBorrow">
<value>true</value>
</property>
</bean>

c3p0的配置, 注意是''driverClass' , 'jdbcUrl', 'user' , 'password'

<bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass">
   <value>oracle.jdbc.driver.OracleDriver</value>
  </property>
  <property name="jdbcUrl">
   <value>jdbc:oracle:thin:@218.202.151.179:58580:ORA9I</value>
  </property>
  <property name="user">
   <value>oa</value>
  </property>
  <property name="password">
   <value>XXXX</value>
  </property>
  <property name="minPoolSize">
   <value>15</value>
  </property>
  <property name="acquireIncrement">
   <value>5</value>
  </property>
  <property name="maxPoolSize">
   <value>25</value>
  </property>

 

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>${db.driverClass}</value>
        </property>
        <property name="jdbcUrl">
            <value>${db.url}</value>
        </property>
         <!--
        <property name="user">
            <value>${db.user}</value>
        </property>
        <property name="password">
            <value>${db.pass}</value>
        </property>
        -->
        <property name="properties">
            <props>
                <prop key="c3p0.acquire_increment">5</prop>
                <prop key="c3p0.idle_test_period">100</prop>
                <prop key="c3p0.max_size">100</prop>
                <prop key="c3p0.max_statements">0</prop>
                <prop key="c3p0.min_size">10</prop>
                <prop key="user">${db.user}</prop>
                <prop key="password">${db.pass}</prop>