开源数据库连接池Bonecp应用

来源:互联网 发布:淘宝宠物医生 编辑:程序博客网 时间:2024/05/01 23:45

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

为什么 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数据源

 <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
  destroy-method="close">
  <property name="driverClass" value="net.sourceforge.jtds.jdbc.Driver" />
  <property name="jdbcUrl" value="jdbc:jtds:sqlserver://172.16.0.10:1433/FleetDB;charset=UTF-8" />
  <property name="username" value="sa" />
  <property name="password" value="Fleet2011@DB." />
  <property name="idleConnectionTestPeriod" value="60" />
  <property name="idleMaxAge" value="240" />
  <property name="maxConnectionsPerPartition" value="50" />
  <property name="minConnectionsPerPartition" value="5" />
  <property name="partitionCount" value="1" />
  <property name="acquireIncrement" value="5" />
  <property name="statementsCacheSize" value="100" />
  <property name="releaseHelperThreads" value="3" />
 </bean>

三:Spring+Hibernate配置数据源
<prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</prop>
跟服务器端区别
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>


供参考:Spring+LazyDataSource
<!-- Hibernate SessionFactory -->
            <bean id="sessionFactory" class="..." autowire="autodetect">
            <!-- Tell hibernate to use our given datasource -->
                <property name="dataSource" ref="dataSource" />
           
                <property name="hibernateProperties">
                    <props>
                        <prop key="hibernate.dialect">...</prop>
                        ...
                    </props>
                </property>
            </bean>
           
            <!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
            <bean id="dataSource"
                class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
                <property name="targetDataSource">
                    <ref local="mainDataSource" />
                </property>
            </bean>
           
            <!-- 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="60"/>
                <property name="minConnectionsPerPartition" value="20"/>
                <property name="partitionCount" value="3"/>
                <property name="acquireIncrement" value="10"/>                             
                <property name="statementsCacheSize" value="50"/>
                <property name="releaseHelperThreads" value="3"/>
            </bean>


四:在tomcat下配置BoneCP连接池
1.环境:jdk1.6,tomcat6.0.20,bonecp-0.6.4
2.需要的包:google-collections-1.0.jar,slf4j-api-1.5.11.jar,slf4j-log4j12-1.5.11.jar,bonecp-0.6.4.jar
3.tomcat的配置方式:
<Resource
  name="clientdb"
  auth="Container"
  type="com.jolbox.bonecp.BoneCPDataSource"
  factory="org.apache.naming.factory.BeanFactory"
  driverClass="oracle.jdbc.driver.OracleDriver"
  username=" "
  password=" "
  jdbcUrl="jdbc:oracle:thin:@127.0.0.1:1521:DB"
  idleConnectionTestPeriod="0"
  idleMaxAge="10"
  partitionCount="1"
  maxConnectionsPerPartition="5"
  minConnectionsPerPartition="1"
  connectionTestStatement=""
  initSQL="select 1 from dual"
/>
可以放到conf/server.xml文件的 <GlobalNamingResources>节,也可放到<context>中
4.使用方式:
和dbcp是一样的:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup(JNDIName);
con = ds.getConnection();
5.资源下载:
http://jolbox.com/bonecp/downloads/maven/com/jolbox/bonecp/0.6.4/bonecp-0.6.4.jar
http://google-collections.googlecode.com/files/google-collect-1.0.zip
http://www.slf4j.org/dist/slf4j-1.5.11.zip

原创粉丝点击