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. The driver connection pool template code for the driver connection pool you want to connect to the database is as follows: packagecom. fish
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. The driver connection pool template code of the driver connection pool you want to connect to the database is as follows: package com. fish
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;/*** Database Connection Tool class, which 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 configuration file name final private static String OPT ION_FILE_NAME = "mysqldatabase"; private static Connection conn; static ResourceBundle res; // the class of the Connection Pool static combooleddatasource CPPS; static {// obtain the file res = ResourceBundle from the configuration file. getBundle (OPTION_FILE_NAME); // create a connection pool cpd= new ComboPooledDataSource (); // driver name String driver = res. getString ("jdbc. driver "); // connect urlString url = res. getString ("jdbc. url "); // database username String user = res. getString ("jdbc. username "); // Database Password S Tring password = res. getString ("jdbc. password "); // The 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 "); // the 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. s EtPassword (password); CPPS. setMinPoolSize (Integer. parseInt (poolMin); CPPS. setAcquireIncrement (Integer. parseInt (PoolAcquireIncrement); CPPS. setMaxPoolSize (Integer. parseInt (poolMax);}/*** get database Connection ** @ return conn */public static Connection getConnection () {try {conn = CPPS. getConnection ();} catch (SQLException e) {System. out. println ("connection failed");} return conn;}/*** release resource **/public static void closeJDBC (Connec Tion 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. driverjdbc. url = jdbc: mysql: // 127.0.0.1: 3306/datacenter3jdbc. username = rootjdbc. password = 1234c3p0. maxPoolSize = 30c3p0. minPoolSize = 10c3p0. after acquireIncrement = 10, you can directly modify the database you want to connect to in this file. Test: 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
Information: 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] 2014-11-16 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