Database connection pool,
Use a pool to manage connections, which can be reused. With the pool, we don't have to create a Connection by ourselves, but get the Connection object by eating it. After the Connection is used, the close () method of the Connection call will not actually close the Connection, but will return the Connection "to the pool. The Connection object can be used by the pool.
1. Pool parameters (all pool parameters have default values ):
- Initial size: 10;
- Minimum idle connections: 3;
- Increment: minimum unit (5) created at a time );
- Maximum number of idle connections: 12;
- Maximum number of connections: 20;
- Maximum waiting time: 1000 milliseconds.
2. Four connection Parameters
The connection pool also uses four connection parameters to create a connection object.
3. Implemented interfaces
The connection pool must be implemented through the javax. SQL. DataSource interface.
The Connection object returned by the Connection pool. Its close () method is different. The close () method called is not to close, but to return the Connection to the pool.
4. DBCP connection pool example:
1 package cn. itcast. jdbc; 2 3 import org. apache. commons. dbcp2.BasicDataSource; 4 import org. junit. test; 5 import java. SQL. connection; 6 import java. SQL. SQLException; 7 8/** 9 * DBCP connection pool 10 */11 public class Demo1 {12 @ Test13 public void fun1 () throws SQLException {14/** 15*1. Create a connection pool 16*2. Configure four parameters 17*3. Configure the pool parameter 18*4. Obtain the connection object 19 */20 BasicDataSource. = new BasicDataSource (); 21 dataSource. setDriverClassName ("com. mysql. jdbc. driver "); 22 dataSource. setUrl ("jdbc: mysql: // localhost: 3306/mydb1"); 23 dataSource. setUsername ("root"); 24 dataSource. setPassword (""); 25 26 dataSource. setMaxTotal (20); 27 dataSource. setMaxIdle (3); 28 dataSource. setMaxWaitMillis (1000); 29 30 Connection con = dataSource. getConnection (); 31 System. out. println (con. getClass (). getName (); // result: org. apache. commons. dbcp2.PoolingDataSource $ PoolGuardConnectionWrapper
32 /*
33 * The Connection Pool uses four parameters to create a Connection object, that is, the Connection object provided by the mysql driver.
34 * The Connection Pool uses the mysql connection object for decoration and only enhances the close () method;
35 * The close () method of the decorated Connection is used to return the current Connection to the pool;
36 **/
37 con. close (); // return the connection to the pool;
38}
39}
5. C3P0
① C3P0 is an open source free connection pool.
② Use of C3P0
In C3P0, the pool class is CombopooledDataSource.
Related jar package download link: https://sourceforge.net/projects/c3p0/files/latest/download? Source = files
Import c3p0-0.9.5.2.jar and mchange-commons-java-0.2.11.jar packages, mysql-connector-java-5.1.44-bin.jar for MySQL Databases)
3 c3p0 can also specify the configuration file, and the configuration file can be properties, can also be xml, of course, xml advanced, but c3p0 configuration file name must be "c3p0-config.xml ", and put it under the class path (src ).
④ Example:
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <c3p0-config> 3 <! -- Default configuration information --> 4 <default-config> 5 <! -- Connect to four major parameter configurations --> 6 <property name = "droverClass"> com. mysql. jdbc. driver </property> 7 <property name = "jdbcUrl"> jdbc: mysql: // localhost: 3306/mydb1 </property> 8 <property name = "user"> root </property> 9 <property name = "password"> </property> 10 <! -- Pool parameter configuration --> 11 <property name = "acquireIncrement"> 3 </property> 12 <property name = "initialPoolSize"> 10 </property> 13 <property name =" minPoolSize "> 2 </property> 14 <property name =" maxPoolSize "> 10 </property> 15 </default-config> 16 <! -- Configuration information specifically provided for oracle --> 17 <named-config name = "oracle-config"> 18 <! -- Connect Four Major Parameter configurations --> 19 <property name = "droverClass"> oracle. jdbc. driver. oracleDriver </property> 20 <property name = "jdbcUrl"> jdbc: oracle: thin: @ address: Port: ORCL </property> 21 <property name = "user"> root </property> 22 <property name = "password"> </property> 23 <! -- Pool parameter configuration --> 24 <property name = "acquireIncrement"> 3 </property> 25 <property name = "initialPoolSize"> 10 </property> 26 <property name =" minPoolSize "> 2 </property> 27 <property name =" maxPoolSize "> 10 </property> 28 </named-config> 29 </c3p0-config>
1 import com. mchange. v2.c3p0. comboPooledDataSource; 2 import org. junit. test; 3 import java. beans. propertyVetoException; 4 import java. SQL. connection; 5 import java. SQL. SQLException; 6 7/** 8 * c3p0 9 */10 public class Demo1 {11/* 12 * manually configure 13 **/14 @ Test15 public void fun1 () throws PropertyVetoException, SQLException {16 // create a connection pool object 17 ComboPooledDataSource dataSource = new ComboPooledDataSource (); 18 // Configure four parameters of the Pool 19 dataSource. setDriverClass ("com. mysql. jdbc. driver "); 20 dataSource. setJdbcUrl ("jdbc: mysql: // localhost: 3306/mydb1"); 21 dataSource. setUser ("root"); 22 dataSource. setPassword (""); 23 24 // set the pool to 25 dataSource. setAcquireIncrement (5); 26 dataSource. setInitialPoolSize (20); 27 dataSource. setMinPoolSize (2); 28 dataSource. setMaxPoolSize (50); 29 30 Connection con = dataSource. getConnection (); 31 System. o Ut. println (con); 32 con. close (); 33} 34/* 35 * default configuration file 36 **/37 @ Test38 public void fun2 () throws SQLException {39/* 40 * when creating a connection pool object, this object will automatically load the configuration file, which we do not need to specify. 41 **/42 ComboPooledDataSource dataSource = new ComboPooledDataSource (); 43 Connection con = dataSource. getConnection (); 44 System. out. println (con); 45 con. close (); 46} 47/* 48 * use the naming configuration information 49 **/50 @ Test51 public void fun3 () throws SQLException {52/* 53 * Parameters specify the name of the configuration element 54 **/55 ComboPooledDataSource dataSource = new ComboPooledDataSource ("oracle-config "); 56 Connection con = dataSource. getConnection (); 57 System. out. println (con); 58 con. close (); 59} 60}