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. 使用PowerShell 命令集进行SQL Server 2012 备份和还原

    最近心相不错,所以打算翻译一些英文文档做福利,原文在此,翻译有不足的地方还请各位兄弟指点. 讨论什么是DBA最重要的工作的时候,你最常听到就是一条就是DBA只要做好备份和恢复.事实如此,如果你不做备份 ...

  2. 杭电ACM 汉字统计

    汉字统计 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. c++实现二分查找

    简要描述: 二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除 困难. 条件:查找的数组必须要为有序数组. 二分查找的过程剩简要描述如下图: 二种实 ...

  4. LinkList Operation

    链表典型数据结构: #define ElemType int typedef struct LinkNode{ ElemType value; struct LinkNode* next; }; 相比 ...

  5. 我的Python成长之路---第五天---Python基础(17)---2016年1月30日(晴)

    常用模块 1.模块介绍 模块,用一砣代码实现了某个功能的代码集合. 模块分为三种 自定义模块 内置标准模块(又称标准库) 开源模块 2.常用模块 time模块 time.time() import t ...

  6. Nginx阅读笔记(三)之proxy_pass用法

    在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走,如果没有/,则会把匹配的路径部分也给代理走. 假设访问 ...

  7. qemu核心机制分析-协程coroutine

    关于协程coroutine前面的文章已经介绍过了,本文总结对qemu中coroutine机制的分析,qemu 协程coroutine基于:setcontext函数族以及函数间跳转函数siglongjm ...

  8. 我的ubuntu

    题外话:不知不觉也已经大三,最近思考了很多.在腾讯网看到了对李嘉诚的一篇专访,感触颇深. 想起来我从第一次接触ubuntu到现在也有一年了,记得第一个版本还是12.04,不过很快就换成了12.10,在 ...

  9. Mac平台编译mupdf-qt的开源项目

    How to compile mupdf-qt Compile on Linux Install tools and thirdparty libraries You should install s ...

  10. [WPF疑难]避免窗口最大化时遮盖任务栏

    原文 [WPF疑难]避免窗口最大化时遮盖任务栏 [WPF疑难]避免窗口最大化时遮盖任务栏 周银辉 WPF窗口最大化时有个很不好的现象是:如果窗口的WindowStyle被直接或间接地设置为None后( ...