在介绍怎么使用之前,先来看看BoneCP的特性(Features ):
- 高度可扩展, 快速的连接池. 注:1)不用synchronized 关键字来处理多线程对资源的争用,而是使用 java.util.concurrent 包中的锁机制;2)首次使用分区机制来分开管理数据库连接;或许还有其他原因.
- Callback (hook interceptor) mechanisms on a change of connection state.
- 利用分区技术提高性能
- 允许直接访问一个连接或者语句
- 智能调整连接池大小
- SQL语句缓存支持
- 支持异步获取数据库连接 (通过返回Future<Connection>的形式)
- 通过释放连接助理进程来释放数据库连接,提高性能.
- 通过initSQL参数在每次获取连接的时候执行SQL
- 支持数据库热切换
- 自动重试失败的数据库操作(当数据库或者网络挂掉的时候)
- JMX support
- 延迟初始化能力(Lazy initialization capable)
- 自动检测连接可用性 (keep-alives 等)
- 允许直接通过数据源而不是通过驱动来获取一个新的数据库连接(Allow obtaining of new connections via a datasource rather than via a Driver)
- Datasource/Hibernate support capable
- Debug支持准确地高亮那些已经得到但是还没有关闭的链接(Debugging hooks to highlight the exact place where a connection was obtained but not closed)
- Debug支持展示那些被关闭两次的链接地址堆栈信息(Debugging support to show stack locations of connections that were closed twice. )
- 支持自定义连接池名称.
- 干净的代码结构,TestCase代码覆盖率达到100% (over 125 JUnit tests).
- 免费的,开源的而且都是用java干的,最重要的是有很完整的javadocs支持。(Free, open source and written in 100% pure Java with complete Javadocs).
1.Maven引用库文件
<repositories> <repository> <releases> <enabled>true</enabled> </releases> <id>bonecp-repo</id> <name>BoneCP Repository</name> <url>http://jolbox.com/bonecp/downloads/maven</url> </repository> </repositories>
pom.xml
<dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp</artifactId> <version>0.6.7.2</version> </dependency>
2.手动初始化:
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
3.Spring配置
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"> <!-- 数据库驱动 --> <property name="driverClass" value="${aliLibrary.db.driverClass}" /> <!-- 相应驱动的jdbcUrl,你懂的 --> <property name="jdbcUrl" value="${aliLibrary.db.jdbcUrl}" /> <!-- 数据库的用户名 --> <property name="username" value="${aliLibrary.db.username}" /> <!-- 数据库的密码 --> <property name="password" value="${aliLibrary.db.password}" /> <!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 --> <property name="idleConnectionTestPeriod" value="${aliLibrary.db.idleConnectionTestPeriod}" /> <!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 --> <property name="idleMaxAge" value="${aliLibrary.db.idleMaxAge}" /> <!-- 每个分区最大的连接数 --> <property name="maxConnectionsPerPartition" value="${aliLibrary.db.maxConnectionsPerPartition}" /> <!-- 每个分区最小的连接数 --> <property name="minConnectionsPerPartition" value="${aliLibrary.db.minConnectionsPerPartition}" /> <!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定--> <property name="partitionCount" value="${aliLibrary.db.partitionCount}" /> <!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 --> <property name="acquireIncrement" value="${aliLibrary.db.acquireIncrement}" /> <!-- 缓存prepared statements的大小,默认值:0 --> <property name="statementsCacheSize" value="${aliLibrary.db.statementsCacheSize}" /> <!-- 每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能 --> <property name="releaseHelperThreads" value="${aliLibrary.db.releaseHelperThreads}" /> </bean>
4.其它功能
connectionTestStatement:在做keep-alive的时候的SQL语句。
statementsCachedPerConnection:No of statements that can be cached per connection,反正源码中不推荐使用,就别用了.
initSQL:在每次到数据库取连接的时候执行的SQL语句,只执行一次。
closeConnectionWatch:如果设置为true,则会增加一个线程监控关闭连接时的情况,如果关闭时出现异常,则打出错误日志,主要用于debug。上线后记得关掉。
logStatementsEnabled:如果设置为true,就会打印执行的SQL语句,如果你用了其他能打印SQL语句的框架,那就不必了。
acquireRetryDelay:在获取连接失败后,第二次参试前的延迟时间,默认为7000毫秒。
acquireRetryAttempts:在获取连接失败后的重试次数,默认为5次。
lazyInit:如果设置为true,那么连接池不会自动创建最小连接数的链接,而是保持为空,直到有需求要获取连接。
transactionRecoveryEnabled:如果设置为true,则会保存该链接上的所有活动,以备下次重试的时候使用,这里指的活动是数据库操作。
connectionHookClassName:Connection hook class name.没看懂…
poolName:上面特性中说到的自定义连接池名称。
disableJMX:控制JMX的支持开关。
connectionTimeout:获取连接的时候最大的等待时间,默认值为:Long.MAX_VALUE