日期:2014-05-16  浏览次数:20782 次

使用BoneCP数据库连接池JAVA版本

?

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.7
依赖的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 c