Java中的BoneCP数据库连接池(转载)

来源:互联网 发布:windows指纹识别器 编辑:程序博客网 时间:2024/05/22 15:36

BoneCP is a fast, free, open-source, Java database connection pool (JDBC Pool) library. If you are familiar with C3P0 and DBCP then you already know what this means. For the rest, this is a library that will manage a database connection for you to get faster database access in your application.
BoneCP is fast! For some tests, it's almost 25 times faster than the next fastest connection pool option, not to mention that BoneCP never spin-locks so it won't slow down your application.
官方主页:http://jolbox.com/
下载地址:http://jolbox.com/bonecp/downloads/maven/com/jolbox/bonecp/
目前最新版本为:0.6.7.2
依赖的jar包:

  • A database that accepts connections
  • A driver to go with it
  • Google Guava library, available for free from here.
  • The SLF4J logging library.
  • JDK1.5 or higher.


    bonecp-0.7.0.jar
    google-collections-1.0.jar
    log4j-1.2.15.jar
    mysql-connector-java-5.1.6-bin.jar(mysql驱动)
    slf4j-api-1.5.10.jar
    slf4j-log4j12-1.5.10.jar
    以上jar包可以在这里下载http://jolbox.com/bonecp/downloads/maven/

    点我下载本文工程代码
    在jdbc中使用BoneCP连接池


    • package com.bonecp;
       
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
       
      import com.jolbox.bonecp.BoneCP;
      import com.jolbox.bonecp.BoneCPConfig;
       
      /** 
       * 
      @author sxyx2008
       *
       
      */
      public class ExampleJDBC {
       
          
      public static void main(String[] args) {
              BoneCP connectionPool 
      = null;
              Connection connection 
      = null;
       
              
      try {
                  
      // load the database driver (make sure this is in your classpath!)
                  Class.forName("com.mysql.jdbc.Driver");
              } 
      catch (Exception e) {
                  e.printStackTrace();
                  
      return;
              }
              
              
      try {
                  
      // setup the connection pool
                  BoneCPConfig config = new BoneCPConfig();
                  config.setJdbcUrl(
      "jdbc:mysql://localhost:3306/demo"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
                  config.setUsername("root"); 
                  config.setPassword(
      "root");
                  
      //设置每60秒检查数据库中的空闲连接数
                  config.setIdleConnectionTestPeriod(60);
                  
      //设置连接空闲时间
                  config.setIdleMaxAge(240);
                  
      //设置每个分区中的最大连接数 30
                  config.setMaxConnectionsPerPartition(30);
                  
      //设置每个分区中的最小连接数 10
                  config.setMinConnectionsPerPartition(10);
                  
      //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数
                  config.setAcquireIncrement(5);
                  
      //连接释放处理
                  config.setReleaseHelperThreads(3);
                  
      //设置分区  分区数为3
                  config.setPartitionCount(3);
                  
      //设置配置参数
                  connectionPool = new BoneCP(config); // setup the connection pool
                  
                  connection 
      = connectionPool.getConnection(); // fetch a connection
                  
                  
      if (connection != null){
                      System.out.println(
      "Connection successful!");
                      Statement stmt 
      = connection.createStatement();
                      ResultSet rs 
      = stmt.executeQuery(" select * from person "); // do something with the connection.
                      while(rs.next()){
                          System.out.println(rs.getString(
      1)); // should print out "1"'
                          System.out.println(rs.getString(2)); // should print out "1"'
                      }
                  }
                  connectionPool.shutdown(); 
      // shutdown connection pool.
              } catch (SQLException e) {
                  e.printStackTrace();
              } 
      finally {
                  
      if (connection != null) {
                      
      try {
                          connection.close();
                      } 
      catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
      }


      使用DataSource

      package com.bonecp;

      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;

      import com.jolbox.bonecp.BoneCPDataSource;

      public class ExampleDataSource {
          
          
      public static void main(String[] args) {
              
              Connection connection 
      = null;
              
              
      try {
                  Class.forName(
      "com.mysql.jdbc.Driver");
              } 
      catch (Exception e) {
                  e.printStackTrace();
              }
              
              BoneCPDataSource dataSource
      =new BoneCPDataSource();
              dataSource.setUsername(
      "root");
              dataSource.setPassword(
      "root");
              dataSource.setJdbcUrl(
      "jdbc:mysql://localhost:3306/demo");
              dataSource.setMaxConnectionsPerPartition(
      10);
              dataSource.setMinConnectionsPerPartition(
      5);
              dataSource.setIdleConnectionTestPeriod(
      60);
              dataSource.setIdleMaxAge(
      240);
              dataSource.setAcquireIncrement(
      5);
              dataSource.setReleaseHelperThreads(
      3);
              
      try {
                  connection
      =dataSource.getConnection();
                  
      if (connection != null){
                      System.out.println(
      "Connection successful!");
                      Statement stmt 
      = connection.createStatement();
                      ResultSet rs 
      = stmt.executeQuery(" select * from person "); // do something with the connection.
                      while(rs.next()){
                          System.out.println(rs.getString(
      1)); // should print out "1"'
                          System.out.println(rs.getString(2)); // should print out "1"'
                      }
                  }
              } 
      catch (SQLException e) {
                  e.printStackTrace();
              }
      finally{
                  
      try {
                      connection.close();
                  } 
      catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
              
              
          }
          
      }


      在Hibernate中使用BoneCP
      在Hibernate中使用BoneCP除了需要上面提到的jar包 之外,还需要下载一个名为bonecp-provider-0.7.0.jar的bonecp-provider的jar包,它的下载位置 是:http://jolbox.com/bonecp/downloads/maven/com/jolbox/bonecp-provider /0.7.0/bonecp-provider-0.7.0.jar。
      除此之外,还需要做如下配置:

    • <!-- 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.statementsCacheSize">50</prop>  
    •             <prop key="bonecp.releaseHelperThreads">3</prop>  
    •         </props>  
    •     </property>  
    • </bean>  
    • xml方式配置bonecp

      <?xml version="1.0" encoding="UTF-8"?>
      <bonecp-config>  
        
      <default-config>  
          
      <property name="jdbcUrl">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>  
          
      <property name="username">scott</property>  
          
      <property name="password">tiger</property>  
          
      <property name="partitionCount">3</property>  
          
      <property name="maxConnectionsPerPartition">30</property>  
          
      <property name="minConnectionsPerPartition">10</property>  
          
      <property name="acquireIncrement">3</property>  
        
      </default-config>   
      </bonecp-config> 
      连接代码

      package com.bonecp;
       
      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;

      import com.jolbox.bonecp.BoneCP;
      import com.jolbox.bonecp.BoneCPConfig;
       
      /** 
       * 
      @author sxyx2008
       *
       
      */
      public class ExampleJDBC {
       
          
      public static void main(String[] args) {
              BoneCP connectionPool 
      = null;
              
              Connection connection 
      = null;
              
      try {
                  
      // load the database driver (make sure this is in your classpath!)
                  Class.forName("oracle.jdbc.driver.OracleDriver");
              } 
      catch (Exception e) {
                  e.printStackTrace();
                  
      return;
              }
              
              
      try {
                  
      // setup the connection pool
                  BoneCPConfig config = null;
                  
      try {
                      config 
      = new BoneCPConfig("bonecp-config.xml");
                  } 
      catch (Exception e) {
                      e.printStackTrace();
                  }
                  
      /*
                  config.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
                  config.setUsername("scott"); 
                  config.setPassword("tiger");
                  //设置每60秒检查数据库中的空闲连接数
                  config.setIdleConnectionTestPeriod(60);
                  //设置连接空闲时间
                  config.setIdleMaxAge(240);
                  //设置每个分区中的最大连接数 30
                  config.setMaxConnectionsPerPartition(30);
                  //设置每个分区中的最小连接数 10
                  config.setMinConnectionsPerPartition(10);
                  //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数
                  config.setAcquireIncrement(5);
                  //连接释放处理
                  config.setReleaseHelperThreads(3);
                  //设置分区  分区数为3
                  config.setPartitionCount(3);
                  
      */
                  
      //设置配置参数
                  connectionPool = new BoneCP(config); // setup the connection pool
                  
                  
      long startTime=System.currentTimeMillis();
                  
      //创建100个连接
                  for (int i = 0; i < 100; i++) {
                      connection 
      = connectionPool.getConnection(); // fetch a connection
                  }
                  
      long endtTime=System.currentTimeMillis();
                  
                  System.out.println(
      "-------->total seconds :"+(endtTime-startTime));
                  
                  
      if (connection != null){
                      System.out.println(
      "Connection successful!");
                      Statement stmt 
      = connection.createStatement();
                      ResultSet rs 
      = stmt.executeQuery(" select * from emp "); // do something with the connection.
                      while(rs.next()){
                          System.out.println(rs.getString(
      1)); // should print out "1"'
                          System.out.println(rs.getString(2)); // should print out "1"'
                      }
                  }
                  connectionPool.shutdown(); 
      // shutdown connection pool.
              } catch (SQLException e) {
                  e.printStackTrace();
              } 
      finally {
                  
      if (connection != null) {
                      
      try {
                          connection.close();
                      } 
      catch (SQLException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
      }
    点我下载本文工程代码

    上文来自: http://www.cnblogs.com/batys/archive/2012/01/09/2316792.html

    -----------------------------------分割线-1----------------------------------

    原文: http://appleblue.iteye.com/blog/838069

    BoneCP是一种新的数据连接技术,以其效率高,速度快著称。经过实践检验也确实如此。参考BoneCP

    以下是BoneCP与Spring配置的步骤:

    1、获取BoneCP的jar包 网址:http://jolbox.com/index.html?page=http://jolbox.com/download.html

    2、获取如下jar包: bonecp-0.7.0.jar  bonecp-provider-0.7.0.jar  bonecp-spring-0.7.0.jar  google-collections-1.0.jar  slf4j-api-1.6.1.jar  slf4j-log4j12-1.6.1.jar

    3、开发环境 myeclipse6.5   jboss4.2.3    windows server 2003  oracle9i

    4、将以上jar包放入项目的lib目录下即可,spring配置如下:

    <bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
      destroy-method="close">
      <property name="driverClass"
       value="oracle.jdbc.driver.OracleDriver" />
      <property name="jdbcUrl" value="数据库路径" />
      <property name="username" value="数据库用户名" />
      <property name="password" value="数据库密码" />

     检查数据库连接池中空闲连接的间隔时间

      <property name="idleConnectionTestPeriod" value="60" />

    连接池中未使用的链接最大存活时间
      <property name="idleMaxAge" value="240" />

    设置每个分区含有connection最大个数
      <property name="maxConnectionsPerPartition" value="30" />

    设置每个分区含有connection最小个数
      <property name="minConnectionsPerPartition" value="10" />

    设置每个分区数
      <property name="partitionCount" value="3" />

    设置分区中的connection增长数量
      <property name="acquireIncrement" value="5" />

     设置连接池阀值

      <property name="poolAvailabilityThreshold" value="10"></property>
      连接时间
      <property name="connectionTimeout" value="3000"></property>

     </bean>

     

    -----------------------------------分割线-2----------------------------------

    原文: 

    http://blog.csdn.net/wozailongyou/article/details/5783302




原创粉丝点击