BoneCP是一个开源的Java数据库连接池,其内部使用了JDK1.5的并发包java.util.concurrent来保证同步,而且采用分段思想,避免单点加锁的竞争(ConcurrentHashMap也是使用这种思想来实现的),具有以下特点(官网):
- Highly scalable, fast connection pool
- Callback (hook interceptor) mechanisms on a change of connection state.
- Partitioning capability to increase performance
- Allows direct access to a connection/statements
- Automatic resizing of pool
- Statement caching support
- Support for obtaining a connection asynchronously (by returning a Future<Connection>)
- Release helper threads to release a connection/statement in an asynchronous fashion for higher performance.
- Easy mechanism to execute a custom statement on each newly obtained connection (initSQL).
- Support to switch to a new database at runtime without shutting down an application
- Ability to replay any failed transaction automatically (for the case where database/network goes down etc)
- JMX support
- Lazy initialization capable
- Support for XML/property configuration
- Idle connection timeouts / max connection age support
- Automatic validation of connections (keep-alives etc)
- Allow obtaining of new connections via a datasource rather than via a Driver
- Datasource/Hibernate support capable
- Debugging hooks to highlight the exact place where a connection was obtained but not closed
- Debugging support to show stack locations of connections that were closed twice.
- Custom pool name support.
- Clean organised code. 100% unit test branch code coverage (over 180 JUnit tests).
- Free, open source and written in 100% pure Java with complete Javadocs.
官网上提供的跟其它连接池的性能测试对比:
Single Thread
- 1,000,000 get connection / release connection requests
- No delay between getting/releasing connection.
- Pool size range: 20-50.
- Acquire increment: 5
- Helper threads: 1
- Partition count: 1
Multi-Thread
- 500 threads each attempting 100 get/release connection
- No delay between getting/releasing connection.
- Pool size range: 50-200.
- Acquire increment: 5
- Helper threads: 5
本次学习基于bonecp-0.7.1.RELEASE版本,主要jar包如下:
com.jolbox.bonecp 连接池核心包
com.jolbox.bonecp.hooks 支持connection状态改变时的事件通知
com.jolbox.bonecp.proxy 代理java.sql.*下的接口,仅供内部使用
此版本主要依赖的外部包有:
slf4j
guava(google lib)
jsr166y
提供外部支持和测试
com.jolbox.bonecp.provider
com.jolbox.bonecp.spring