Use pools to manage connection, which can be reused with connection. With the pool, we don't have to create connection ourselves, but we get connection objects by eating them. When connection is finished, calling connection's close () method does not actually close the connection, but instead connection "returns" to the pool. The pool is ready to take advantage of this connection object.
1. Pool parameters (all pool parameters have default values):
- Initial Size: 10;
- Minimum number of idle connections: 3;
- Increment: The smallest unit created at a time (5);
- Maximum number of idle connections: 12;
- Maximum number of connections: 20;
- Maximum wait time: 1000 milliseconds.
2. Four Connecting parameters
Connection pooling also uses the four connection parameters to complete the creation of connection objects.
3, the implementation of the interface
Connection pooling must be implemented: Javax.sql.DataSource interface.
The connection object returned by the connection pool, its close () method is different, the close () call to it is not closed, but the connection is returned to the pool.
4. DBCP Connection Pool Example:
1 packageCN.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 { @Test13 public void Fun1 () throws sqlexception{14/**15 * 1, creating connection Pool 16 * 2, configuration Four Large parameter 17 * 3, Configuration Pool parameter 18 * 4, Get Connection object */20 basicdatasource dataSource = new Basicdatasource (); Datasource.setdriverclass Name ("Com.mysql.jdbc.Driver" ); Datasource.seturl ("jdbc:mysql://localhost:3306/mydb1" ); 23 Datasource.setusername ("root" ), Datasource.setpassword ("" ), and Datasource.setmaxtotal (); Datasource.setmaxidle (3 ), Datasource.setmaxwaitmillis (+ ), Connection con = Datasource.getconnection (); System.out.println (Con.getclass (). GetName ());// Result: Org.apache.commons.dbcp2.poolingdatasource$poolguardconnectionwrapper
33
34
35
* * * *
39}
5, C3P0
①C3P0 is an open source free connection pool.
Use of ②C3P0
The C3P0 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 database)
③C3P0 can also specify the configuration file, and the configuration file can be the properties, or XML, of course, XML is advanced, but the C3P0 configuration file name must be "C3p0-config.xml", and placed in the path of the Class (SRC).
④ Example:
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <c3p0-config> 3 <!--default configuration information--4 <DEFAULT-CONFIG&G T 5 <!--connection four 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" &G T;root</property> 9 <property name= "password" ></property>10 <!--pool parameter configuration-->11 <property name= "acquireincrement" >3</property>12 <property name= "Initialpoolsize" >10</proper ty>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-conf IG ">18 <!--connection Four parameters configuration-->19 <property name=" Droverclass ">oracle.jdbc.driver.oracledriver</p Roperty>20 <property name= "Jdbcurl" >jdbc:oracle:thin:@ Address: Port: orcl</property>21 <property name= "user" >ROOT&L t;/property>22 <property name= "password" ></property>23 <!--pool parameter configuration-->24 <pro Perty name= "acquireincrement" >3</property>25 <property name= "Initialpoolsize" >10</property>2 6 <property name= "minpoolsize" >2</property>27 <property name= "Maxpoolsize" >10</proper ty>28 </named-config>29 </c3p0-config>
1 ImportCom.mchange.v2.c3p0.ComboPooledDataSource; 2 ImportOrg.junit.Test; 3 ImportJava.beans.PropertyVetoException; 4 ImportJava.sql.Connection; 5 ImportJava.sql.SQLException; 6 7/** 8 * C3P0 9 */10 public classDEMO1 {11/*12 * Manual Configuration 13 * */14@Test15 public void fun1 () throwspropertyvetoexception,sqlexception {16//Create Connection pool object Combopooleddatasource DataSource = newCombopooleddatasource (); 18//configuration of the four parameters of the pool Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setjdbcurl ("JDBC:MYSQL://LOCALHOST:3306/MYDB1"); Datasource.setuser ("Root"); Datasource.setpassword (""); 23 24//Pool configuration datasource.setacquireincrement (5); Datasource.setinitialpoolsize (20); Datasource.setminpoolsize (2); Datasource.setmaxpoolsize (+ ); Connection con = datasource.getconnection (); System.out.println (con); con.close (); }34/*35 * configuration file default configuration * */37 @Test38 public void fun2 () th Rows sqlexception{39/*40 * When you create a connection pool object, the object automatically loads the configuration file without us specifying it. * */42 Combopooleddatasource dataSource = new Combopooleddatasource (); Connection con = Datasource.getco Nnection (); System.out.println (con); con.close (); }47/*48 * Using named configuration information * */50 @Test51 P ublic void Fun3 () throws sqlexception{52/*53 * constructor parameter specifies the name of the named configuration element. * */55 Combopooleddatasource dataSource = new Co Mbopooleddatasource ("Oracle-config" ), Connection con = datasource.getconnection (); System.out.println (con); con.close (); }60}
Database Connection Pool