---
title: "BONECP使用"
author: "sharefling"
date: "2014-05-19T00:20:22Z"
tags: []
categories: ["编程语言"]
slug: "bonecp"
draft: false
lastmod: "2014-05-19T00:20:22Z"
id: 2068032
url: "https://sharefling.iteye.com/blog/2068032"
---

最近在使用数据库的一些操作，就顺便想仔细了解·一下JDBC连接池，和驱动程序（没有读懂，就不献丑了）

常见的数据源有dbcp,c3p0,网上资料一大把，这里就讲述BONECP，其实官网(http://www.jolbox.com/)上也有比较简单的示例，API，文档和一些常见问题，这里就写几个简单的例子，和一些我的看法

我的一个例子

package bonecptest;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

 

import com.jolbox.bonecp.BoneCP;

import com.jolbox.bonecp.BoneCPConfig;

import com.jolbox.bonecp.ConnectionHandle;

 

public class TestCp {

 

public static void main(String\[] args) {

// TODO Auto-generated method stub

 

try {

Class.forName("org.gjt.mm.mysql.Driver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.err.println("驱动加载失败");

}

 

try {

 

BoneCPConfig config= new BoneCPConfig(); 

config.setJdbcUrl("jdbc:mysql://localhost/book");

config.setUsername("root");

config.setPassword("root");

config.setMaxConnectionsPerPartition(10);

config.setMinConnectionsPerPartition(5);

//config.setPartitionCount(2);//连接池内部连接池组数

//config.setAcquireIncrement(5);

 

 

BoneCP pool=new BoneCP(config);

ConnectionHandle connection=(ConnectionHandle) pool.getConnection();

PreparedStatement pre=connection.prepareStatement("select * from admin");

ResultSet set= pre.executeQuery();

while(set.next()) {

System.out.println(set.getString("admin\_name"));

}

 

//connection.close();

 

for(int i=1;i&lt;=13;i++){

ConnectionHandle c=(ConnectionHandle) pool.getConnection();

System.out.println(i+":"+c.getCatalog());

c.close();//使用完之后要释放，类似于对象池里面的returnobject(object o)，但是这里面的close()，只是将connection返回到连接池里面，而不是像jdbc原生connection真正关闭（如果是一样的话，那就相当与重新创建，没有重用connection，那就毫无意义，而且你可以注释掉c.close()方法，就会发现创建到第十个程序就不会继续运行，你想嘛，这里需要创建从连接池获取，但是连接池去没有空闲的连接\*\*\*\*\*前提是此连接池不进行扩容操作），而且这里的connection其实本身还是conenctionhandle，提供的close()并没有进行原生close()调用，而是释放，回归到连接池里面

 

}

 

 

 

System.out.println(pool.getTotalCreatedConnections()+";"+pool.getTotalFree()+";"+pool.getTotalLeased());//查看连接池总归创建了多少连接，空闲连接，已使用连接

pool.shutdown();

 

} 

catch(Exception e) {

 

}

 

 

 

 

}

 

}

 

这是一个非常简单的测试程序，没有什么特别的，就用来进行简单的获取connection，然后进行连数据库操作。

对于数据库操作，现在感觉是这样的

数据库-&gt;数据库驱动-&gt;获取元数据-&gt;数据库连接-&gt;数据库连接池（亦成为连接池，数据源）

而在数据库框架中配置datasource其实配置就是连接池，因为本身jdbc是一个连接方式也提供一个获取connection，像是一套java连接数据库的规范示例，而不是说针对某个数据源，所以配置dbcp，c3p0其实和数据hi有jdbc connection没有什么区别，没有其他人说什么数据源就特别之处，就是提供connection，至于怎么操作，事务管理，回滚那些就跟数据源没什么关系，嗯哪，他就是对象池，makeobject（） return connection。

我所理解的数据源就是连接池，有现成的数据源框架，也可以我们自定义实现，我觉得需要满足一些要求可以称之为一个好的连接池

1.满足基本提供connection等基本功能，即对象池,体现复用性

2.能够应对并发

3.能保证并发现象安全

4.使用多线程进行管理，使用线程池。

c3p0，dbcp数据源已经非常成熟，至于bonecp还存在，官网有解释，看了一下bonecp的源码是满足以上4个条件，前两者使用过没看源代码，但是应该也是满足的，不然大家也不会接受。

 

bonecp关键类有BoneCPConfig（配置数据库），BoneCP（提供操作的连接池，内附线程池），ConnectionPartition（进行具体管理连接的对象池），ConnectionHandle（继承与conenction的连接）,StatementReleaseHelperThread(衔接连接操作)，

 

这里是close（）方法源代码

 

public void close() throws SQLException {

        try {

            if (!this.logicallyClosed) {

                this.logicallyClosed = true;

                this.threadUsingConnection = null;

                this.pool.releaseConnection(this);//释放

 

                if (this.doubleCloseCheck){

                    this.doubleCloseException = this.pool.captureStackTrace(CLOSED\_TWICE\_EXCEPTION\_MESSAGE);

                }

            } else {

                if (this.doubleCloseCheck && this.doubleCloseException != null){

                    String currentLocation = this.pool.captureStackTrace("Last closed trace from thread \["+Thread.currentThread().getName()+"]:\\n");

                    logger.error(String.format(LOG\_ERROR\_MESSAGE, this.doubleCloseException, currentLocation));

                }

            }

        } catch (SQLException e) {

            throw markPossiblyBroken(e);

        }

    }

 

 

之后释放具体行为

 

 \*/

    protected void releaseConnection(Connection connection) throws SQLException {

        ConnectionHandle handle = (ConnectionHandle)connection;

 

        // hook calls

        if (handle.getConnectionHook() != null){

            handle.getConnectionHook().onCheckIn(handle);

        }

 

        // release immediately or place it in a queue so that another thread will eventually close it. If we're shutting down,

        // close off the connection right away because the helper threads have gone away.

        if (!this.poolShuttingDown && this.releaseHelperThreadsConfigured){

            if (!handle.getOriginatingPartition().getConnectionsPendingRelease().tryTransfer(handle)){

                handle.getOriginatingPartition().getConnectionsPendingRelease().put(handle);//具体操作

            }

        } else {

            internalReleaseConnection(handle);

        }

    }

 

 

具体上没有看懂，但肯定是回收connection，而且绝对没有对原生connection进行close()，个人对于源代码没有看懂，初测为是将blockingqueue中connection对象进行了标记，尽管是使用消费-生产，但是使用标记应该有其他的用途