本文转自:http://www.cnblogs.com/luxh/archive/2012/07/07/2580885.html
BoneCP是一个Java数据库连接池库,官方介绍它的速度非常快,测试值高出C3P0、DBCP很多,性能也非常出色,值得一用。
使用BoneCP有一些要求:
1)Google Guava library
2)The SLF4J logging library
3)JDK1.5 or higher
1、在JDBC中直接使用。
1)classpath路径下log4j.properties文件
2)所需jar
bonecp-0.7.1.RELEASE.jar
guava-12.0.jar
mysql-connector-java-5.1.6-bin.jar
slf4j-api-1.6.6.jar
slf4j-log4j12-1.6.6.jar
log4j-1.2.17.jar
3)简单的测试代码
复制代码
package cn.luxh.bonecp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
public class BoneCPTest {
@Test
public void testBoneCP() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver"); // load the DB driver
BoneCPConfig config = new BoneCPConfig(); // create a new configuration object
// set the JDBC url
config.setJdbcUrl("jdbc:mysql://localhost:3306/spring_struts_jpa?useUnicode=true&characterEncoding=UTF-8");
config.setUsername("root"); // set the username
config.setPassword("root"); // set the password
//一些参数设置
config.setPartitionCount(3);
config.setMaxConnectionsPerPartition(20);
config.setMinConnectionsPerPartition(10);
config.setAcquireIncrement(5);
config.setPoolAvailabilityThreshold(20);
config.setReleaseHelperThreads(2);
config.setIdleMaxAge(240,TimeUnit.MINUTES);
config.setIdleConnectionTestPeriod(60,TimeUnit.MINUTES);
config.setStatementsCacheSize(20);
config.setStatementReleaseHelperThreads(3);
BoneCP connectionPool = new BoneCP(config); // setup the connection pool
Connection connection = connectionPool.getConnection(); // fetch a connection
String sql = "INSERT INTO log4j(message) VALUES ('hello bonecp')";
PreparedStatement ps = connection.prepareStatement(sql);
ps.execute(sql);
ps.close();
connection.close(); // close the connection
connectionPool.shutdown(); // close the connection pool
}
}
复制代码
2、和sring+Hibernate整合使用。
1)除了需要在JDBC中使用需要的jar包、Hibernate包和spring包外,
还需要两个很重要的jar:bonecp-provider-0.7.1-rc2.jar和bonecp-spring-0.7.1-rc1.jar
2)如果是配置SessionFactory,需要注意的是使用的是Hibernate3还是Hibernate4。
复制代码
复制代码
3)如果是配置数据源
复制代码
复制代码
3、和JPA整合使用
1)除了需要在JDBC中直接使用所需的jar包、JPA的jar包外,还需要一个很重要的jar:bonecp-provider-0.7.1-rc2.jar
2)jpa使用Hibernate提供的实现,persistent.xml配置文件如下,需要注意的是使用的是Hibernate3还是Hibernate4
复制代码
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
复制代码
4、BoneCP官方网站:http://jolbox.com/
和BoneCP相关的一些jar下载地址:http://jolbox.com/bonecp/downloads/maven/com/jolbox/
或者:https://repository.cloudera.com/content/groups/cdh-build/com/jolbox/