【JAVASE】BoneCP 教程

来源:互联网 发布:科研立项题目 软件 编辑:程序博客网 时间:2024/05/25 18:12

BoneCP 是高性能数据库连接池。同类软件还有C3PO和DBCP之类。


官网  http://www.jolbox.com/ 

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).


  • jdbcUrl

    The JDBC connection string URL.

    Default: None

  • username

    The DB username to use.

    Default: None

  • password

    The DB password to use.

    Default: None

  • partitionCount

    In order to reduce lock contention and thus improve performance, each incoming connection request picks off a connection from a pool that has thread-affinity, i.e. pool[threadId % partition_count]. The higher this number, the better your performance will be for the case when you have plenty of short-lived threads. Beyond a certain threshold, maintenence of these pools will start to have a negative effect on performance (and only for the case when connections on a partition start running out).

    Default: 1, minimum: 1, recommended: 3-4 (but very app specific)

  • maxConnectionsPerPartition

    The number of connections to create per partition. Setting this to 5 with 3 partitions means you will have 15 unique connections to the database. Note that BoneCP will not create all these connections in one go but rather start off with minConnectionsPerPartition and gradually increase connections as required.

  • minConnectionsPerPartition

    The number of connections to start off with per partition.

  • acquireIncrement

    When the available connections are about to run out, BoneCP will dynamically create new ones in batches. This property controls how many new connections to create in one go (up to a maximum of maxConnectionsPerPartition). Note: This is a per partition setting.


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 urlconfig.setUsername("sa");// set the usernameconfig.setPassword("");// set the passwordconfig.setXXXX(...);// (other config options here)BoneCP connectionPool = new BoneCP(config); // setup the connection poolConnection connection;connection = connectionPool.getConnection(); // fetch a connection...  do something with the connection here ...connection.close();// close the connectionconnectionPool.shutdown();// close the connection pool

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 the class below (either via explicit listing of the class name or via <component-scan>.
  @Componentpublic class Foo { @AutowiredDataSource ds;  public void testBoneCP() throws SQLException {    Connection connection = ds.getConnection();    System.out.println(connection); // do something with the connection here.. } }

0 0
原创粉丝点击