BoneCP 是一个开源的快速的 JDBC 连接池。BoneCP很小,只有四十几K(运行时需要log4j和Google Collections的支持,这二者加起来就不小了),而相比之下C3P0 要六百多K。另外个人觉得 BoneCP 有个缺点是,JDBC驱动的加载是在连接池之外的,这样在一些应用服务器的配置上就不够灵活。

Bonecp是一个快速的,免费的,开放源代码的java数据库连接池资源库。

BoneCPConfig类可以用来手动设置连接池及其属性,

Class.forName("org.hsqldb.jdbcDriver");       // load the DB driver
        BoneCPConfig config = new BoneCPConfig();     // create a new configuration object
        config.setJdbcUrl("jdbc:hsqldb:mem:test");    // set the JDBC url
        config.setUsername("sa");                     // set the username
        config.setPassword("");                               // set the password
        
        config.setXXXX(...);                          // (other config options here)
        
        BoneCP connectionPool = new BoneCP(config);   // setup the connection pool
        
        Connection connection;
        connection = connectionPool.getConnection();  // fetch a connection
        
        ...  do something with the connection here ...
        
        connection.close();                           // close the connection
        connectionPool.shutdown();                    // close the connection pool

datasource格式创建连接池,

Class.forName("org.hsqldb.jdbcDriver");       // load the DB driver
        BoneCPDataSource ds = new BoneCPDataSource();  // create a new datasource object
        ds.setJdbcUrl("jdbc:hsqldb:mem:test");                // set the JDBC url
        ds.setUsername("sa");                         // set the username
        ds.setPassword("");                           // set the password
        
        ds.setXXXX(...);                              // (other config options here)
        
        Connection connection;
        connection = ds.getConnection();              // fetch a connection
        
        ...  do something with the connection here ...
        
        connection.close();                           // close the connection
        ds.close();                                   // close the datasource pool

基于spring的代码,

Application Context

 
<!-- 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="30"/>
   <property name="minConnectionsPerPartition" value="10"/>
   <property name="partitionCount" value="3"/>
   <property name="acquireIncrement" value="5"/>
   <property name="statementsCacheSize" value="100"/>
   <property name="releaseHelperThreads" value="3"/>
</bean>

Code:

Make sure you define theclass below (either via explicit listing of the class name or via<component-scan>.

 
 
@Component
public class Foo {
@Autowired
DataSource ds;
 public void testBoneCP() throws SQLException {
    Connection connection = ds.getConnection();
    System.out.println(connection); // do something with the connection here..
 }
}

基于spring+hibernate的方式

<!-- 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>

基于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>
            

Jdbc example

The full source code of this example project can befound here:

You will need Apache Maven to automatically includethe required jar files into your classpath. Alternatively you may add therequired JAR files manually.

package com.jolbox;

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;

/** A test project demonstrating the use of BoneCP in a JDBCenvironment.

* @author wwadge

*

*/

public class ExampleJDBC {

/** Start test

* @param args none expected.

*/

public static voidmain(String[] args) {

BoneCPconnectionPool = null;

Connectionconnection = null;

try {

// load thedatabase driver (make sure this is in your classpath!)

Class.forName("org.hsqldb.jdbcDriver");

} catch (Exceptione) {

e.printStackTrace();

return;

}

try {

// setup theconnection pool

BoneCPConfigconfig = new BoneCPConfig();

config.setJdbcUrl("jdbc:hsqldb:mem:test");// jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb

config.setUsername("sa");

config.setPassword("");

config.setMinConnectionsPerPartition(5);

config.setMaxConnectionsPerPartition(10);

config.setPartitionCount(1);

connectionPool= new BoneCP(config); // setup the connection pool

connection =connectionPool.getConnection(); // fetch a connection

if(connection != null){

System.out.println("Connectionsuccessful!");

Statementstmt = connection.createStatement();

ResultSet rs =stmt.executeQuery("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS");// do something with the connection.

while(rs.next()){

System.out.println(rs.getString(1));// 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();

}

}

}

}

}

If you use Maven, the following should besufficient to pull in the JAR files required into your classpath:

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.jolbox</groupId>

<artifactId>examplejdbc</artifactId>

<name>Example JDBCProject</name>

<version>1.0.0</version>

<description>ExampleJDBC project</description>

<dependencies>

<dependency>

<groupId>com.jolbox</groupId>

<artifactId>bonecp</artifactId>

<version>0.6.5</version>

<scope>compile</scope>

</dependency>

<!-- This is the HSQLDBdatabase in use by this test. Replace with MySQL, PostgreSQL etc here if youwish. -->

<dependency>

<groupId>hsqldb</groupId>

<artifactId>hsqldb</artifactId>

<version>1.8.0.10</version>

<scope>compile</scope>

</dependency>

</dependencies>

<repositories>

<repository>

<releases>

<enabled>true</enabled>

</releases>

<id>bonecp-repo</id>

<name>BoneCPRepository</name>

<url>http://jolbox.com/bonecp/downloads/maven</url>

</repository>

</repositories>

</project>

四大流行的java连接池之BoneCP篇的更多相关文章

  1. 四大流行的jdbc连接池之C3P0篇

    C3P0是一个开放源代码的JDBC连接池,它在lib目录中与Hibernate一起发布,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSourc ...

  2. 四个流行的Java连接池之Proxool篇

    Proxool是一个JavaSQL Driver驱动程序,提供了对你选择的其它类型的驱动程序的连接池封装.可以非常简单的移植到现存的代码中.完全可配置.快速,成熟,健壮.可以透明地为你现存的JDBC驱 ...

  3. 几个主流java连接池

    池(Pool)技术在一定程度上可以明显优化服务器应用程序的性能,提高程序执行效率和降低系统资源开销.这里所说的池是一种广义上的池,比如数据库连接池.线程池.内存池.对象池等.其中,对象池可以看成保存对 ...

  4. 转载: 几个主流的Java连接池整理

    https://www.cnblogs.com/linjian/p/4831088.html 池(Pool)技术在一定程度上可以明显优化服务器应用程序的性能,提高程序执行效率和降低系统资源开销.这里所 ...

  5. 几个主流的Java连接池整理

    池(Pool)技术在一定程度上可以明显优化服务器应用程序的性能,提高程序执行效率和降低系统资源开销.这里所说的池是一种广义上的池,比如数据库连接池.线程池.内存池.对象池等.其中,对象池可以看成保存对 ...

  6. [JavaEE] 了解Java连接池

    转载自51CTO http://developer.51cto.com/art/201006/207768.htm 51CTO曾经为我们简单的介绍过Java连接池.要了解Java连接池我们先要了解数据 ...

  7. Java 连接池的工作原理(转)

    原文:Java 连接池的工作原理 什么是连接? 连接,是我们的编程语言与数据库交互的一种方式.我们经常会听到这么一句话“数据库连接很昂贵“. 有人接受这种说法,却不知道它的真正含义.因此,下面我将解释 ...

  8. Redis Java连接池调研

    Redis Java连接池调研 线上服务,由于压力大报错RedisTimeOut,但是需要定位到底问题出现在哪里? 查看Redis慢日志,slowlog get 发现耗时最大的也是11000us也就是 ...

  9. 【JAVA】 Java 连接池的工作原理

    什么是连接?         连接,是我们的编程语言与数据库交互的一种方式.我们经常会听到这么一句话“数据库连接很昂贵“.         有人接受这种说法,却不知道它的真正含义.因此,下面我将解释它 ...

随机推荐

  1. Visual Studio Tools for Unity安装及使用

    Visual Studio Tools for Unity安装及使用 转载自:CSDN 晃了一下,10.1到现在又过去两个月了,这两个月什么也没有学,整天上班下班,从这周末开始拾起unity,为了年后 ...

  2. SQL中distinct的用法(转)

    原文:http://www.cnblogs.com/rainman/archive/2013/05/03/3058451.html 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...

  3. 解决centos7安装wmwaretools找不到kernel header

    解决centos6安装wmwaretools找不到kernel header http://www.centoscn.com/CentosBug/softbug/2015/0525/5531.html ...

  4. Linux下安装JRE

    (1)下载jre-7u5-linux-i586.tar.gz,上传至/root目录 (2)执行tar -zxf jre-7u5-linux-i586.tar.gz (3)mv jre1.7.0_05 ...

  5. Nginx 之五: Nginx服务器的负载均衡、缓存与动静分离功能

    一.负载均衡: 通过反向代理客户端的请求到一个服务器群组,通过某种算法,将客户端的请求按照自定义的有规律的一种调度调度给后端服务器. Nginx的负载均衡使用upstream定义服务器组,后面跟着组名 ...

  6. Nginx小技巧(一)隐藏版本号

    修改nginx.conf server_tokens作用域是http server location语句块 server_tokens默认值是on,表示显示版本信息,设置server_tokens值是 ...

  7. lua学习:使用Lua处理游戏数据

    在之前lua学习:lua作配置文件里,我们学会了用lua作配置文件. 其实lua在游戏开发中可以作为一个强大的保存.载入游戏数据的工具. 1.载入游戏数据 比如说,现在我有一份表单: data.xls ...

  8. cocos2d-x游戏开发系列教程-超级玛丽08-消息机制

    在超级玛丽游戏里,地图类CMGameMap负责所有的程序逻辑,它包含了背景地图,包含了游戏元素精灵,当游戏中的精灵之间发生碰撞时,比如马里奥撞上砖头这种事情发生时,马里奥对象本身不知道怎么处理这个逻辑 ...

  9. HashMap,LinkedHashMap,TreeMap的区别(转)

    Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复.Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快 ...

  10. BZOJ 1833: [ZJOI2010]count 数字计数( dp )

    dp(i, j, k)表示共i位, 最高位是j, 数字k出现次数. 预处理出来. 差分答案, 对于0~x的答案, 从低位到高位进行讨论 -------------------------------- ...