1 C3P0 Introduction
C3P0 is also open source free connection pool! C3P0 is favored by many people!
2 Use of C3P0
Package Com.cug.c3p0;import Java.beans.propertyvetoexception;import Java.sql.connection;import Java.sql.sqlexception;import Org.junit.test;import Com.mchange.v2.c3p0.combopooleddatasource;public class C3P0Demo {@Testpublic void Fun1 () throws Propertyvetoexception, Sqlexception{combopooleddatasource DataSource = new Combopooleddatasource ();d atasource.setuser ("root");d Atasource.setpassword ("123");d Atasource.setjdbcurl ("JDBC: MYSQL://LOCALHOST:3306/MYDB1 ");d atasource.setdriverclass (" Com.mysql.jdbc.Driver ");d Atasource.setmaxpoolsize (30 );d atasource.setminpoolsize;d atasource.setinitialpoolsize;d atasource.setmaxidletime (3000); Datasource.setacquireincrement (5); Connection conn = Datasource.getconnection ();//[email PROTECTED]SYSTEM.OUT.PRINTLN (conn); Conn.close ();}}
Configuration file Requirements:
- File name: Must be called C3p0-config.xml
- File location: Must be under SRC
C3P0 can also specify a configuration file, and the configuration file can be either properties or close XML. Of course, XML is a bit more advanced. However, the configuration file name for C3P0 must be c3p0-config.xml and must be placed under the classpath.
<?xml version= "1.0" encoding= "UTF-8"? ><c3p0-config><default-config><property name= "JdbcUrl" >jdbc:mysql://localhost:3306/mydb1</property><property name= "Driverclass" >com.mysql.jdbc.Driver </property><property name= "user" >root</property><property name= "password" >123</ Property><property name= "acquireincrement" >3</property><property name= "InitialPoolSize" >10 </property><property name= "minpoolsize" >2</property><property name= "MaxPoolSize" >10</ Property></default-config><named-config name= "Oracle-config" ><property name= "JdbcUrl" >JDBC: Mysql://localhost:3306/mydb1</property><property name= "Driverclass" >com.mysql.jdbc.driver</ Property><property name= "user" >root</property><property name= "password" >123</property ><property name= "acquireincrement" >3</property><property name= "InitialPoolSize" >10</ Property>&lT;property name= "minpoolsize" >2</property><property name= "Maxpoolsize" >10</property></ Named-config></c3p0-config>
public void Fun2 () throws Propertyvetoexception, SQLException {combopooleddatasource ds = new Combopooleddatasource (); Connection con = ds.getconnection (); System.out.println (con); Con.close ();} public void Fun2 () throws Propertyvetoexception, SQLException {combopooleddatasource ds = new Combopooleddatasource (" Orcale-config "); Connection con = ds.getconnection (); System.out.println (con); Con.close ();}
Java Web----C3P0