Database connection pool using the JAVA version 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.
Official Homepage: http://jolbox.com/
Download: http://jolbox.com/bonecp/downloads/maven/com/jolbox/bonecp/
The latest version is: 0.7
Dependent on the 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.
    That
    bonecp-0.7.0.jar
    google-collections-1.0.jar
    log4j-1.2.15.jar
    mysql-connector-java-5.1.6-bin.jar (mysql driver)
    slf4j-api-1.5.10.jar
    slf4j-log4j12-1.5.10.jar
    Above jar file can be downloaded here http://jolbox.com/bonecp/downloads/maven/

    In the jdbc connection pool using 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");
                // Setting checks every 60 seconds number of idle connections in database
                config.setIdleConnectionTestPeriod(60);
                // Set the connection idle time
                config.setIdleMaxAge(240);
                // Set maximum number of connections in each partition  30
                config.setMaxConnectionsPerPartition(30);
                // Minimum number of connection settings in each partition  10
                config.setMinConnectionsPerPartition(10);
                // When the connection from the connection pool is exhausted BoneCP while getting the number of connections
                config.setAcquireIncrement(5);
                // Connection release
                config.setReleaseHelperThreads(3);
                // Set partition partitions 3
                config.setPartitionCount(3);
                // Setting configuration parameters
                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();
                    }
                }
            }
        }
    }
    
  • xml-mode configuration 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>
    

    Connection code

    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");
                // Setting checks every 60 seconds number of idle connections in database
                config.setIdleConnectionTestPeriod(60);
                // Set the connection idle time
                config.setIdleMaxAge(240);
                // Set maximum number of connections in each partition  30
                config.setMaxConnectionsPerPartition(30);
                // Minimum number of connection settings in each partition  10
                config.setMinConnectionsPerPartition(10);
                // When the connection from the connection pool is exhausted BoneCP while getting the number of connections
                config.setAcquireIncrement(5);
                // Connection release
                config.setReleaseHelperThreads(3);
                // Set partition partitions 3
                config.setPartitionCount(3);
                */
                // Setting configuration parameters
                connectionPool = new BoneCP(config); // setup the connection pool
    
                long startTime=System.currentTimeMillis();
                // Create 100 connections
                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();
                    }
                }
            }
        }
    }
    

    Use of a 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();
                }
            }
    
        }
    
    }
    

    Use in Hibernate BoneCP

    Use in Hibernate BoneCP addition to the above mentioned jar files, they also need to download a file called bonecp-provider-0.7.0.jar jar of bonecp-provider's package, it's download location is: http://jolbox .com/bonecp/downloads/maven/com/jolbox/bonecp-provider/0.7.0/bonecp-provider-0.7.0.jar.

    In addition, the need to do the following configuration:

    <!-- Hibernate SessionFactory -->
    <bean 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>
    

    Reference: http://www.blogjava.net/sxyx2008/archive/2011/03/16/346384.html

分类:Database 时间:2011-09-21 人气:326
分享到:
blog comments powered by Disqus

相关文章

  • JAVA自动操作0racle数据库转mysql数据库 2013-06-10

    1.要注意数据编码,在建表时就应该设置好默认编码,我这里是UTF-8: CREATE TABLE `CODE_ZYB` ( `uid` int(64) NOT NULL AUTO_INCREMENT, `XSH` varchar(5) DEFAULT NULL, `ZYH` varchar(8) DEFAULT NULL, `XKMLH` varchar(2) DEFAULT NULL, `ZYM` varchar(40) DEFAULT NULL, `YWZYM` varchar(80) DE

  • JAVA/JSP学习系列之八(改写MySQL翻页例子) 2014-08-07

    一.前言 其实,改写后的JDBC Data-Source是运行在Servlet中的,通过JNDI去查找数据源.我用Orion试的,将本站<JAVA/JSP学习系列之六(MySQL翻页例子) > 简单改写了一下. 二.配置 (1)JDBC 需要将用到的JDBC驱动Copy到[ORION]/lib目录下 (2)data-source 在[ORION]/config/data-sources.xml文件中加入如下: 〈data-source class="com.evermind.sql.

  • Java and the problem of garbled mysql 2010-09-15

    Solution one: (the most important way) You look my.ini, with or without [Mysql] default-character-set = utf8 [Client] default-character-set = utf8 [Mysqld] default-character-set = utf8 Then create the table, but also with (for example) DROP TABLE IF

  • java.sql.SQLException: ResultSet not open, operation 'next' not permitted. 2010-09-27

    java.sql.SQLException: ResultSet not open, operation 'next' not permitted. Verify that autocommit is OFF. Error Translation: result set is not open, next operation are not allowed. Make sure that auto-commit is turned off. Recently encountered this e

  • Tomcat 6 through JDBC connection pool to connect SQL Server 2000 and MySQL 5 settings 2010-05-27

    SQL Server 2000 connection pool configuration is as follows according to the order operation 1. Download SQL Server 2000 JDBC Driver on \ Tomcat6 \ lib, I am now using msbase.jar, mssqlserver.jar, msutil.jar these three documents 2. In Tomcat 6.0 \ w

  • java program called ORACLE stored procedure and returns data set (resultSet) 2010-11-25

    Divided into three steps: 1. Created corresponding to the stored procedure. create or replace procedure getemps (p_cursor out types.cursorType) as begin open p_cursor for select * from userinfo order by userinfo.u_id; - the name of the table end; 2.

  • Java package for reading resources or JAR file path 2010-07-26

    Here are divided into two specific: The first: the resource file for the general suffix file The second: Resource file for picture files First] [NO1 Use this line of code can get class path of the root class String path = Thread.currentThread (). Get

  • From Java files (JAR) file to read 2010-09-07

    In java.util.jar package. Here to use the main class is JarFile class, it is a. Jar file their own reference. Each of these documents cited by the JarEntry. Now, pass a parameter to the constructor JarFile create a JarFile instance, this parameter ma

  • String [] args = new String [] {sql.toString ()} 2011-06-29

    In this problem StringBuffer sql = new StringBuffer (); Which sql.toString results: 'Test User 1', 'male' String [] args = new String [] {sql.toString ()} args.length 1 results Obviously not what we need. Note: new String []{"","",&quo

  • jdbc mysql transaction set 2011-03-03

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import java.sql.Statement ; public class TranDemo02{ // Define MySQL database driver public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ; //

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 1.065 (s). 12 q(s)