Database connection pool using the JAVA version BoneCP
Advertisements
BoneCP is fast! For some tests, it's almost 25 times faster than the next fastest connection pool option, not to mention that BoneCP never spin-locks so it won't slow down your application.
Official Homepage: http://jolbox.com/
Download: http://jolbox.com/bonecp/downloads/maven/com/jolbox/bonecp/
The latest version is: 0.7
Dependent on the jar:
- A database that accepts connections
- A driver to go with it
- Google Guava Library, available for free from here .
- The SLF4J logging Library.
- JDK1.5 or higher.
That
bonecp-0.7.0.jar
google-collections-1.0.jar
log4j-1.2.15.jar
mysql-connector-java-5.1.6-bin.jar (mysql driver)
slf4j-api-1.5.10.jar
slf4j-log4j12-1.5.10.jar
Above jar file can be downloaded here http://jolbox.com/bonecp/downloads/maven/
In the jdbc connection pool using BoneCP
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"); // Setting checks every 60 seconds number of idle connections in database config.setIdleConnectionTestPeriod(60); // Set the connection idle time config.setIdleMaxAge(240); // Set maximum number of connections in each partition 30 config.setMaxConnectionsPerPartition(30); // Minimum number of connection settings in each partition 10 config.setMinConnectionsPerPartition(10); // When the connection from the connection pool is exhausted BoneCP while getting the number of connections config.setAcquireIncrement(5); // Connection release config.setReleaseHelperThreads(3); // Set partition partitions 3 config.setPartitionCount(3); // Setting configuration parameters 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(); } } } } } - xml-mode configuration 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>
Connection code
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"); // Setting checks every 60 seconds number of idle connections in database config.setIdleConnectionTestPeriod(60); // Set the connection idle time config.setIdleMaxAge(240); // Set maximum number of connections in each partition 30 config.setMaxConnectionsPerPartition(30); // Minimum number of connection settings in each partition 10 config.setMinConnectionsPerPartition(10); // When the connection from the connection pool is exhausted BoneCP while getting the number of connections config.setAcquireIncrement(5); // Connection release config.setReleaseHelperThreads(3); // Set partition partitions 3 config.setPartitionCount(3); */ // Setting configuration parameters connectionPool = new BoneCP(config); // setup the connection pool long startTime=System.currentTimeMillis(); // Create 100 connections 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(); } } } } }
Use of a DataSource
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(); } } } }
Use in Hibernate BoneCP
Use in Hibernate BoneCP addition to the above mentioned jar files, they also need to download a file called bonecp-provider-0.7.0.jar jar of bonecp-provider's package, it's download location is: http://jolbox .com/bonecp/downloads/maven/com/jolbox/bonecp-provider/0.7.0/bonecp-provider-0.7.0.jar.
In addition, the need to do the following configuration:
<!-- Hibernate SessionFactory --> <bean autowire="autodetect"> <property name="hibernateProperties"> <props> <prop key="hibernate.connection.provider_class">com.jolbox.bonecp.provider.BoneCPConnectionProvider</prop> <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> <prop key="hibernate.connection.url">jdbc:mysql://127.0.0.1/yourdb</prop> <prop key="hibernate.connection.username">root</prop> <prop key="hibernate.connection.password">abcdefgh</prop> <prop key="bonecp.idleMaxAge">240</prop> <prop key="bonecp.idleConnectionTestPeriod">60</prop> <prop key="bonecp.partitionCount">3</prop> <prop key="bonecp.acquireIncrement">10</prop> <prop key="bonecp.maxConnectionsPerPartition">60</prop> <prop key="bonecp.minConnectionsPerPartition">20</prop> <prop key="bonecp.statementsCacheSize">50</prop> <prop key="bonecp.releaseHelperThreads">3</prop> </props> </property> </bean>
Reference: http://www.blogjava.net/sxyx2008/archive/2011/03/16/346384.html
Related Posts of Database connection pool using the JAVA version BoneCP
-
In the servlet use Bean
According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...
-
The EJB3 Persistence
EJB3 persistence with Hibernate is very similar to the mechanism: Environment: Server: JBOSS5.0 Database: MySQL5.0 1. Set up a data source First of all, in jboss-5.0.0.GA \ server \ default \ deploy, the establishment of a database used to connect the dat
-
hibernate generic generic DAO
package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *
-
Servlet brief introduction
Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...
-
can not be represented as java.sql.Timestamp
Development of procedures for the use of hibernate when, some time there is no need to fill in the fields, but after the hibernate query time reported "Java.sql.SQLException: Value'0000-00-00 'can not be represented as java.sql.Timestamp ...
-
First Hibernate Example
Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co
-
Hibernate annotation using notebook
These are the basic common @Entity --Declared an entity bean @Table(name="promotion_info") --For the entity bean mapping for the specified table (Table name ="promotion_info) @Id --Declare that the identifying attribute of the entity bean @GeneratedValue
-
Struts2 + hibernate + spring problem user log in
dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q












