What pooled data source should I use for Spring 3.1.0, Hibernate 4.0.1.Final, and MySQL 5.1?

旧巷老猫 提交于 2019-12-04 05:28:17

You can use Apache DBCP, which should be a drop in replacement something like this:

<bean id="dataSource" 
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <property name="maxActive" value="10"/> 
    <property name="minIdle" value="5"/> 
    <!-- SELECT 1 is a simple query that returns 1 row in MySQL -->
    <property name="validationQuery" value="SELECT 1"/> 
</bean>

a few things to note

  • you can configure max number of connections.
  • you can configure min number of idle connections.
  • a query that will get executed to validate the connection is still valid.

Further options exist to configure when validation happens.

If you want to use something mature and performant (e.g. not Apache DBCP), use BoneCP.

Here are the options you can tweak:

<!-- BoneCP configuration -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
   <property name="driverClass" value="com.mysql.jdbc.Driver" />
   <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1/yourdb" />
   <property name="username" value="root"/>
   <property name="password" value="abcdefgh"/>
   <property name="idleConnectionTestPeriod" value="60"/>
   <property name="idleMaxAge" value="240"/>
   <property name="maxConnectionsPerPartition" value="30"/>
   <property name="minConnectionsPerPartition" value="10"/>
   <property name="partitionCount" value="3"/>
   <property name="acquireIncrement" value="5"/>
   <property name="statementsCacheSize" value="100"/>
   <property name="releaseHelperThreads" value="3"/>
</bean>

BoneCP forum is very active, and committers are quite responsive.

Another one you can look at (would hear about) is C3PO, although BoneCP performs a lot better.

alexkasko

Apache DBCP is a widely used pool. But do not use its option testWhileIdle. When enabled, It's background evictor thread locks all new connections serving while checking dead connections. It's unacceptable in any non-toy environment. Besides this, we have no problems with it.

You may read more about pools in this SO thread, but keep in mind, that all flames about "fastest pool" only make sense with specific tuning under specific load.

Basically it depends on your application.

There are below conditions(taken from here):

  1. For test or standalone environments outside of a J2EE container, use DriverManagerDataSource
  2. For use in a J2EE container, it is recommended to use a JNDI DataSource provided by the container.
  3. For use of a connection pool datasource outside of a J2EE container, consider Apache's Jakarta Commons DBCP or C3P0.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!