c3p0 is an open source JDBC Connection pool that implements the data source and Jndi bindings to support the standard extensions of the JDBC3 specification and JDBC2. The open source projects that currently use it are hibernate,spring and so on. C3P0 data sources are used more in project development.
1. c3p0 Difference from DBCP
DBCP No automatic recycle of idle connections
c3p0 Automatic recovery of idle connection function
c3p0 support for more database connection pooling options.
2. Importing related jar packages
C3p0-0.9.0.jar
3. c3p0 Detailed Parameters
datasource.c3p0.acquireincrement=10 When the connection in the connection pool is exhausted, c3p0 creates the number of new connections at once;
datasource.c3p0.minpoolsize=50 the minimum number of connections that are kept in the connection pool. The default is
datasource.c3p0.maxpoolsize=400 the maximum number of connections that are kept in the connection pool. Default is;
datasource.c3p0.initialpoolsize=50 The number of connections created at initialization should be taken between Minpoolsize and Maxpoolsize. The default is 3;
datasource.c3p0.maxidletime=1800 maximum idle time, the connection that exceeds the idle time is discarded. 0 or negative numbers will never be discarded. The default is 0;
datasource.c3p0.acquireretryattempts=100 defines the number of repeated attempts to obtain a new connection from the database after a failure, by default;
datasource.c3p0.acquireretrydelay=20 two times the interval in the connection, in milliseconds, the default is three;
Datasource.c3p0.debugunreturnedconnectionstacktraces=true
Datasource.c3p0.maxstatements=0jdbc standard parameter that controls the number of PreparedStatement loaded within the data source . However, because the pre-cached statement belong to a single connection instead of the entire connection pool. So setting this parameter takes many factors into account, and if both maxstatements and maxstatementsperconnection are 0, the cache is closed. The default is 0;
datasource.c3p0.idleconnectiontestperiod=1800 How many seconds to check for idle connections in all connection pools, by default 0 means no check;
datasource.c3p0.breakafteracquirefailure=true getting a connection failure will cause all threads waiting to get the connection to throw an exception. However, the data source is still valid and continues to try to get the connection the next time the getconnection () is tuned. If set to True, the data source will declare broken and permanently shut down after attempting to acquire a connection failure. The default is false;
Datasource.c3p0.testconnectiononcheckout=false Please use it only when you need it because of high performance consumption. If set to True then the validity of each connection submission is officer. It is recommended to use Idleconnectiontestperiod or automatictesttable
datasource.c3p0.autocommitonclose=true all uncommitted operations are rolled back by default when the connection is closed. The default is False;
datasource.c3p0.maxstatementsperconnection=100 the maximum number of cache statement that a single connection in a connection pool has . The default is 0;
4. using C3PO to implement the source code of database connection pool
Package com.db;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* Simple creation of database connection pools with c3p0 Open source Tools
* @author Fan Fangming
*/
public class Easyc3p0 {
privatestatic Combopooleddatasource ds = null;
// Create a database connection pool in a static code block
static{
try{
// Creating a C3P0 database connection pool from code
ds= new Combopooleddatasource ();
Ds.setdriverclass ("Oracle.jdbc.driver.OracleDriver");
Ds.setjdbcurl ("JDBC:ORACLE:THIN:@140.207.38.242:1521:O2O");
Ds.setuser ("Bm114_test");
Ds.setpassword ("Bm114_test");
Ds.setinitialpoolsize (10);
Ds.setminpoolsize (5);
Ds.setmaxpoolsize (20);
}catch (Exception e) {
E.printstacktrace ();
}
}
// get a database connection from the data source
Publicstatic Connection getconnection () throws SQLException {
Returnds.getconnection ();
}
// To close a database connection
publicstatic void Close (Connection conn, Statement St, ResultSet rs) {
if (rs! = null) {
try{
// close the ResultSet object that stores the query results
Rs.close ();
}catch (Exception e) {
E.printstacktrace ();
}
rs= null;
}
if (st! = null) {
try{
// Close the statement object that is responsible for executing the SQL command
St.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
IF (conn! = null) {
try{
// Returning the connection connection object to the database connection pool
Conn.close ();
}catch (Exception e) {
E.printstacktrace ();
}
}
}
publicstatic void Main (string[] args) {
Connectioncon = null;
PREPAREDSTATEMENTST = null;
Resultsetrs = null;
try{
// Get database connection
Con= easyc3p0.getconnection ();
Stringsql = "Select sysdate from Dual";
PREPAREDSTATEMENTPS = con.preparestatement (sql);
Rs= Ps.executequery ();
while (Rs.next ()) {
StringValue = rs.getstring ("Sysdate");
System.out.println (value+ ", database connection succeeded");
}
}catch (Exception e) {
E.printstacktrace ();
}finally {
// Freeing Resources
Easyc3p0.close (Con,st, RS);
}
}
}
5. Run Results
2015-02-03 16:16:27.0, Database Connection succeeded
The memory is inferior to the bad pen 22-java database connection pool C3P0