BoneCP是一个快速,开源的数据库连接池。帮你管理数据连接让你的应用程序能更快速地访问数据库

比C3P0/DBCP连接池快25倍。

该项目主页:http://jolbox.com/about.html

 

为什么 BoneCP 连接池的性能这么高呢?

1. BoneCP 不用 synchronized 关键字来处理多线程对资源的争用,而是使用 java.util.concurrent 包中的锁机制,这个包是在 JDK 1.5 才开始有的;

2. 分区机制,尽管使用了锁,但还是存在着资源争用的问题,因此 BoneCP 可配置多个连接池分区,每个分区独立管理,互不影响。

 

尽管连接池的性能并不会是一个系统中的瓶颈,但是我们单纯从连接池这个角度来看 BoneCP ,也是值得我们去学习的。

 

 

²     JDBC连接属于数据源

// load the DB driver

Class.forName("org.hsqldb.jdbcDriver");      

 

// create a new datasource object

BoneCPDataSource ds = new BoneCPDataSource(); 

 

// set the JDBC url

ds.setJdbcUrl("jdbc:hsqldb:mem:test");              

 

// set the username

ds.setUsername("sa");                        

 

// set the password

ds.setPassword("");                          

 

// (other config options here)

ds.setXXXX(...);                             

       

Connection connection;

connection = ds.getConnection();             

// fetch a connection

       

...  do something with the connection here ...

 

// close the connection

connection.close();                          

// close the datasource pool

ds.close();                

 

²     Spring配置数据源

<!-- BoneCP configuration -->
<bean id="mainDataSource" 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="statementCacheSize" value="100"/>
   <property name="releaseHelperThreads" value="3"/>
</bean>

 

 

²     Spring+Hibernate配置数据源

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean"               autowire="autodetect">
<property name="hibernateProperties">
<props>
<prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://127.0.0.1/yourdb</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">abcdefgh</prop>
<prop key="bonecp.idleMaxAge">240</prop>
<prop key="bonecp.idleConnectionTestPeriod">60</prop>
<prop key="bonecp.partitionCount">3</prop>
<prop key="bonecp.acquireIncrement">10</prop>
<prop key="bonecp.maxConnectionsPerPartition">60</prop>
<prop key="bonecp.minConnectionsPerPartition">20</prop>
<prop key="bonecp.preparedStatementCacheSize">50</prop>
<prop key="bonecp.statementsCachedPerConnection">30</prop>
<prop key="bonecp.releaseHelperThreads">3</prop>
</props>
</property>
</bean>

 

 

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐