C3p0 connection pool template,
A connection pool is a technology used to create and manage a connection buffer pool. These connections are ready for use by any thread that needs them.
Now I am creating a template for the p3c0 connection pool.
First, p3c0 is open-source, so go to the official website to download the p3c0 jar package. Import data in the project and download the driver for your database connection.
The connection pool template code is as follows:
Package com. fish;
Import java. beans. PropertyVetoException;
Import java. SQL. Connection;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Import java. util. ResourceBundle;
Import com. mchange. v2.c3p0. ComboPooledDataSource;
/**
* The tool class used to connect to the database is defined as non-inherited and private access.
*/
Public final class DBTool {
// Confidence in configuring the connection pool using the configuration file. This is the name of the configuration file.
Final private static String OPTION_FILE_NAME = "mysqldatabase ";
Private static Connection conn;
Static ResourceBundle res;
// Connection pool class
Static ComboPooledDataSource CPPS;
Static {
// Obtain the file from the configuration file
Res = ResourceBundle. getBundle (OPTION_FILE_NAME );
// Create a connection pool
CPPS = new ComboPooledDataSource ();
// Driver name
String driver = res. getString ("jdbc. driver ");
// Connection url
String url = res. getString ("jdbc. url ");
// Database username
String user = res. getString ("jdbc. username ");
// Database Password
String password = res. getString ("jdbc. password ");
// Maximum number of connections in the connection pool
String poolMax = res. getString ("c3p0. maxPoolSize ");
// The minimum number of connections in the connection pool
String poolMin = res. getString ("c3p0. minPoolSize ");
// Number of connections allowed when resources are exhausted
String PoolAcquireIncrement = res. getString ("c3p0. acquireIncrement ");
Try {
CPPS. setDriverClass (driver );
} Catch (PropertyVetoException e ){
System. out. println ("driver not found ");
}
CPPS. setJdbcUrl (url );
CPPS. setUser (user );
CPPS. setPassword (password );
CPPS. setMinPoolSize (Integer. parseInt (poolMin ));
CPPS. setAcquireIncrement (Integer. parseInt (PoolAcquireIncrement ));
CPPS. setMaxPoolSize (Integer. parseInt (poolMax ));
}
/**
* Obtain the database connection
*
* @ Return conn
*/
Public static Connection getConnection (){
Try {
Conn = CPPS. getConnection ();
} Catch (SQLException e ){
System. out. println ("connection failed ");
}
Return conn;
}
/**
* Release resources
*
*/
Public static void closeJDBC (Connection conn, Statement statement,
ResultSet rs ){
If (null! = Rs ){
Try {
Rs. close ();
} Catch (SQLException e ){
E. printStackTrace ();
Throw new RuntimeException (e );
} Finally {
If (null! = Statement ){
Try {
Statement. close ();
} Catch (SQLException e ){
E. printStackTrace ();
Throw new RuntimeException (e );
} Finally {
If (null! = Conn ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
Throw new RuntimeException (e );
}
}
}
}
}
}
}
}
2. The mysqldatabase. properties file is as follows:
Jdbc. driver = com. mysql. jdbc. Driver
Jdbc. url = jdbc: mysql: /127.0.0.1: 3306/datacenter3
Jdbc. username = root
Jdbc. password = 1234
C3p0. maxPoolSize = 30
C3p0. minPoolSize = 10
C3p0. acquireIncrement = 10
In the future, you can directly modify the database you want to connect to in this file.
Test: Example
Package com. fish;
Public class Test2 {
Public static void main (String [] args) throws Exception {
System. out. println (DBTool. getConnection ());
}
}
Output result:
14:44:04 com. mchange. v2.log. MLog <clinit>
Message: MLog clients using java 1.4 + standard logging.
2014-11-16 14:44:04 com. mchange. v2.c3p0. C3P0Registry banner
Information: Initializing c3p0-0.9.2.1 [built 20-March-2013 11:16:28 + 0000; debug? True; trace: 10]
14:44:04 com. mchange. v2.c3p0. impl. AbstractPoolBackedDataSource getPoolManager
Information: Initializing c3p0 pool... com. mchange. v2.c3p0. comboPooledDataSource [acquireIncrement-> 10, latency-> 30, acquireRetryDelay-> 1000, autoCommitOnClose-> false, automaticTestTable-> null, disable-> false, checkoutTimeout-> 0, connectionCustomizerClassName-> null, connectionTesterClassName-> com. mchange. v2.c3p0. impl. defaultConnectionTester, dataSourceName-> 1hge1d1951evdfumup12ui | 5ffb18, debugUnreturnedConnectionStackTraces-> false, description-> null, driverClass-> com. mysql. jdbc. driver, factoryClassLocation-> null, Token-> false, identityToken-> token | 5ffb18, idleConnectionTestPeriod-> 0, initialPoolSize-> 3, jdbcUrl-> jdbc: mysql: // 127.0.0.1: 3306/datacenter3, latency-> 0, maxConnectionAge-> 0, maxIdleTime-> 0, maxIdleTimeExcessConnections-> 0, maxPoolSize-> 30, maxStatements-> 0, maxStatementsPerConnection-> 0, minPoolSize-> 10, numHelperThreads-> 3, preferredTestQuery-> null, properties-> {user = ******, password = ******}, propertyCycle-> 0, success-> 0, testConnectionOnCheckin-> false, testConnectionOnCheckout-> false, unreturnedConnectionTimeout-> 0, userOverrides->{}, success-> false]
Com. mchange. v2.c3p0. impl. NewProxyConnection @ d19bc8