BoneCP is a fast, free, open-source, Java database connection pool (JDBC Pool) library. If you are familiar with C3P0 and DBCP then you already know what this means. For the rest, this is a library that will manage a database connection for you to get faster database access in your application. 即 Single Thread
Multi-Thread
Multi-Thread 10ms delay
Multi-Thread 25ms delay
Multi-Thread 50ms delay
Multi-Thread 75ms delay
Prepared Statement (single threaded)
Prepared Statement (multi-threaded)
Prepared Statement (multi-threaded, 10ms delay)
Prepared Statement (multi-threaded, 25ms delay)
Prepared Statement (multi-threaded, 50ms delay)
Prepared Statement (multi-threaded, 75ms delay)
点我下载本文工程代码 package com.bonecp; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.jolbox.bonecp.BoneCP; import com.jolbox.bonecp.BoneCPConfig; /** * @author sxyx2008 * */ public class ExampleJDBC { public static void main(String[] args) { BoneCP connectionPool = null; Connection connection = null; try { // load the database driver (make sure this is in your classpath!) Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { e.printStackTrace(); return; } try { // setup the connection pool BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/demo"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb config.setUsername("root"); config.setPassword("root"); //设置每60秒检查数据库中的空闲连接数 config.setIdleConnectionTestPeriod(60); //设置连接空闲时间 config.setIdleMaxAge(240); //设置每个分区中的最大连接数 30 config.setMaxConnectionsPerPartition(30); //设置每个分区中的最小连接数 10 config.setMinConnectionsPerPartition(10); //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数 config.setAcquireIncrement(5); //连接释放处理 config.setReleaseHelperThreads(3); //设置分区 分区数为3 config.setPartitionCount(3); //设置配置参数 connectionPool = new BoneCP(config); // setup the connection pool connection = connectionPool.getConnection(); // fetch a connection if (connection != null){ System.out.println("Connection successful!"); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(" select * from person "); // do something with the connection. while(rs.next()){ System.out.println(rs.getString(1)); // should print out "1"' System.out.println(rs.getString(2)); // should print out "1"' } } connectionPool.shutdown(); // shutdown connection pool. } catch (SQLException e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
package com.bonecp;
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.jolbox.bonecp.BoneCPDataSource; public class ExampleDataSource { public static void main(String[] args) { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { e.printStackTrace(); } BoneCPDataSource dataSource=new BoneCPDataSource(); dataSource.setUsername("root"); dataSource.setPassword("root"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo"); dataSource.setMaxConnectionsPerPartition(10); dataSource.setMinConnectionsPerPartition(5); dataSource.setIdleConnectionTestPeriod(60); dataSource.setIdleMaxAge(240); dataSource.setAcquireIncrement(5); dataSource.setReleaseHelperThreads(3); try { connection=dataSource.getConnection(); if (connection != null){ System.out.println("Connection successful!"); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(" select * from person "); // do something with the connection. while(rs.next()){ System.out.println(rs.getString(1)); // should print out "1"' System.out.println(rs.getString(2)); // should print out "1"' } } } catch (SQLException e) { e.printStackTrace(); }finally{ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
xml方式配置bonecp <?xml version="1.0" encoding="UTF-8"?>
连接代码<bonecp-config> <default-config> <property name="jdbcUrl">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> <property name="username">scott</property> <property name="password">tiger</property> <property name="partitionCount">3</property> <property name="maxConnectionsPerPartition">30</property> <property name="minConnectionsPerPartition">10</property> <property name="acquireIncrement">3</property> </default-config> </bonecp-config> package com.bonecp; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import com.jolbox.bonecp.BoneCP; import com.jolbox.bonecp.BoneCPConfig; /** * @author sxyx2008 * */ public class ExampleJDBC { public static void main(String[] args) { BoneCP connectionPool = null; Connection connection = null; try { // load the database driver (make sure this is in your classpath!) Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (Exception e) { e.printStackTrace(); return; } try { // setup the connection pool BoneCPConfig config = null; try { config = new BoneCPConfig("bonecp-config.xml"); } catch (Exception e) { e.printStackTrace(); } /* config.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb config.setUsername("scott"); config.setPassword("tiger"); //设置每60秒检查数据库中的空闲连接数 config.setIdleConnectionTestPeriod(60); //设置连接空闲时间 config.setIdleMaxAge(240); //设置每个分区中的最大连接数 30 config.setMaxConnectionsPerPartition(30); //设置每个分区中的最小连接数 10 config.setMinConnectionsPerPartition(10); //当连接池中的连接耗尽的时候 BoneCP一次同时获取的连接数 config.setAcquireIncrement(5); //连接释放处理 config.setReleaseHelperThreads(3); //设置分区 分区数为3 config.setPartitionCount(3); */ //设置配置参数 connectionPool = new BoneCP(config); // setup the connection pool long startTime=System.currentTimeMillis(); //创建100个连接 for (int i = 0; i < 100; i++) { connection = connectionPool.getConnection(); // fetch a connection } long endtTime=System.currentTimeMillis(); System.out.println("-------->total seconds :"+(endtTime-startTime)); if (connection != null){ System.out.println("Connection successful!"); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(" select * from emp "); // do something with the connection. while(rs.next()){ System.out.println(rs.getString(1)); // should print out "1"' System.out.println(rs.getString(2)); // should print out "1"' } } connectionPool.shutdown(); // shutdown connection pool. } catch (SQLException e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } |
|