Java中的BoneCP数据库连接池

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/

    官方性能测试:

    Single Thread
    • 1,000,000 get connection / release connection requests
    • No delay between getting/releasing connection.
    • Pool size range: 20-50.
    • Acquire increment: 5
    • Helper threads: 1
    • Partition count: 1 
    Multi-Thread
    • 500 threads each attempting 100 get/release connection
    • No delay between getting/releasing connection.
    • Pool size range: 50-200.
    • Acquire increment: 5
    • Helper threads: 5 
    Multi-Thread 10ms delay
    • 500 threads each attempting 100 get/release connection
    • We introduce a 10ms delay (Thread.sleep()) between the acquire connection and the release connection to simulate work being done with the connection.
    • Pool size range: 50-200.
    • Acquire increment: 5
    • Helper threads: 5 
    Multi-Thread 25ms delay
    • 500 threads each attempting 100 get/release connection
    • We introduce a 25ms delay (Thread.sleep()) between the acquire connection and the release connection to simulate work being done with the connection.
    • Pool size range: 50-200.
    • Acquire increment: 5
    • Helper threads: 5 
    Multi-Thread 50ms delay
    • 500 threads each attempting 100 get/release connection
    • We introduce a 50ms delay (Thread.sleep()) between the acquire connection and the release connection to simulate work being done with the connection.
    • Pool size range: 50-200.
    • Acquire increment: 5
    • Helper threads: 5 
    Multi-Thread 75ms delay
    • 500 threads each attempting 100 get/release connection
    • We introduce a 75ms delay (Thread.sleep()) between the acquire connection and the release connection to simulate work being done with the connection.
    • Pool size range: 50-200.
    • Acquire increment: 5
    • Helper threads: 5 
    Prepared Statement (single threaded)
    • Fetches a single connection then calls 1,000,000 connection.prepareStatement(...) followed by an immediate statement close.
    • No delay between calls
    • Max statements: 30
    • Max statements per connection: 30
    • Helper threads: 5 
    Prepared Statement (multi-threaded)
    • 500 Threads each attempting 100 get/release connection with no delays. After obtaining a connection, make a call to connection.prepareStatement(...) followed by a close statement request.
    • No delay between calls
    • Pool size range: 50-200.
    • Acquire increment: 5.
    • Helper threads: 5 
    Prepared Statement (multi-threaded, 10ms delay)
    • 500 Threads each attempting 100 get/release connection with no delays. After obtaining a connection, make a call to connection.prepareStatement(...) followed by a close statement request.
    • 10ms delay between open connection/release connection to simulate work done by the application.
    • Pool size range: 50-200.
    • Acquire increment: 5.
    • Helper threads: 5 
    Prepared Statement (multi-threaded, 25ms delay)
    • 500 Threads each attempting 100 get/release connection with no delays. After obtaining a connection, make a call to connection.prepareStatement(...) followed by a close statement request.
    • 25ms delay between open connection/release connection to simulate work done by the application.
    • Pool size range: 50-200.
    • Acquire increment: 5.
    • Helper threads: 5 
    Prepared Statement (multi-threaded, 50ms delay)
    • 500 Threads each attempting 100 get/release connection with no delays. After obtaining a connection, make a call to connection.prepareStatement(...) followed by a close statement request.
    • 50ms delay between open connection/release connection to simulate work done by the application.
    • Pool size range: 50-200.
    • Acquire increment: 5.
    • Helper threads: 5 
    Prepared Statement (multi-threaded, 75ms delay)
    • 500 Threads each attempting 100 get/release connection with no delays. After obtaining a connection, make a call to connection.prepareStatement(...) followed by a close statement request.
    • 75ms delay between open connection/release connection to simulate work done by the application.
    • Pool size range: 50-200.
    • Acquire increment: 5.
    • Helper threads: 5 
    • 点我下载本文工程代码
      在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();
                        }
                    }
                }
            }
        }
      点我下载本文工程代码
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值