DBCP Database Connection Pool
DBCP is an open source connection pool implementation under the Apache Software Foundation, using the DBCP data source, the application should add the following two jar files to the system:
Commons-dbcp.jar: Connection pooling implementation Commons-pool.jar: Dependent library Tomcat for connection pooling implementations
Connection pool is implemented with this connection pool. The database connection pool can be used either in combination with the application server or independently by the application.
Core code
static{ in = JdbcUtil.class.getClassLoader(). getResourceAsStream("dbcpconfig.properties"); new Properties(); prop.load(in); new BasicDataSourceFactory(); dataSource = factory.createDataSource(prop);}
Package Dpcppooluse;Import Java. SQL. Connection;Import Java. SQL. SQLException;Import Java. Util. Properties;Import Javax. SQL. DataSource;import org. Apache. Commons. DBCP. Basicdatasource;import org. Apache. Commons. DBCP. Basicdatasourcefactory;import org. JUnit. Test;public class Dbcppooldemo {@Test public void testdbcp () throws SQLException, interruptedexception{//D Atasource pool=new Basicdatasource ();//Can not use DataSource, because there is no set. functionBasicdatasource pool=new Basicdatasource ();//Create a Pool objectPool. Setdriverclassname("Com.mysql.jdbc.Driver");Pool. SetUrl("Jdbc:mysql://127.0.0.1:3306/sstud?useunicode=true&characterencoding=utf-8");Pool. Setusername("Root");Pool. SetPassword("1234");System. out. println(Pool. Getdefaultcatalog());//get default parametersSystem. out. println(Pool. Getinitialsize());//Get connection pool initialization sizeSystem. out. println(Pool. Getlogintimeout());//Login Limit timeSystem. out. println(Pool. getmaxwait());//pool maximum wait Time ( -1)System. out. println(Pool. Getmaxidle());////maximum idle time. If a user gets a connection, no, how long it takes to be forcibly retractedSystem. out. println(Pool. Getmaxactive());//pool maximum number of accessSystem. out. println("__________________");Set your own parameters//pool. setmaxwait(1);Pool. Setmaxidle(1);Pool. Setmaxactive( -);for (int i=0; i<20;i++) {Thread. Sleep(1100);Connection Con=pool. getconnection();//priority to get just usedSystem. out. println(i+":::::::::::"+con. Hashcode());Con. Close();}}//Read through profile @Test public void Testpropertyfile () throws exception{Properties P=new properties ();P. Load(ClassLoader. Getsystemclassloader(). Getsystemresourceasstream("Dpcp.properities"))///config file to be placed in src (bin) root directory---classpath rootP. Load(Dbcppooldemo. Class. getResourceAsStream("Dbcp.properities"));//The configuration file is put together with the class of the current classesDataSource pool=basicdatasourcefactory. CreateDataSource(p)//datasource to read from a configuration fileDataSource cannot make general settings//Get connections from its pool (8a) for (int i=0; i<10;i++) {Connection Con=pool. getconnection();System. out. println(i+":::::"+con. Hashcode());if ((i&1)==0) {con. Close();} } }}
- Connection Pooling Dbcputil Tools
Package Dpcppooluse;import Java.io.ioexception;import Java.sql.connection;import java.sql.sqlexception;import Java.util.properties;import Javax.sql.datasource;import org.apache.commons.dbcp.BasicDataSourceFactory; Public classDbcputil {Private StaticDataSource pool=NULL;//Static Basicdatasource pool=new Basicdatasource ();//Create a Pool object Private StaticThreadlocal<connection> tl=NewThreadlocal<connection> ();Static{Properties p=NewProperties ();Try{//Pool.setdriverclassname ("Com.mysql.jdbc.Driver");//Pool.seturl ("Jdbc:mysql://127.0.0.1:3306/sstud?useunicode=true&characterencoding=utf-8");//Pool.setusername ("root");//Pool.setpassword ("1234");Class.forName ("Com.mysql.jdbc.Driver");//need to write yourselfP.load (DbcpPoolDemo.class.getResourceAsStream ("Dbcp.properities"));//configuration file and the class of the current classes are put togetherSystem. out. println (P); Pool=basicdatasourcefactory.createdatasource (P); }Catch(Exception e) {E.printstacktrace (); } }//Return to datasource--pool Public StaticDataSourceGetdatasource(){returnPool } Public StaticThreadlocal<connection>Gettl() {returnTl } Public Static void Settl(ObjectObject) {dbcputil.tl = (threadlocal<connection>)Object; } Public StaticConnectionGetcon() {Connection con=tl.Get();if(con==NULL){Try{con=pool.getconnection (); }Catch(SQLException e) {E.printstacktrace (); } TL.Set(con); }returnCon } Public Static void Main(string[] args) {System. out. println (Getcon ()); }}
C3P0 Database Connection Pool
- C3P0 is an open source JDBC connection pool that implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. The open source projects that currently use it are hibernate,spring and so on.
- And the difference between DPCP:
1). DBCP does not automatically reclaim idle connection functions
2). C3P0 has automatic recycle idle connection function
MySQL note Eight--the use of open source database connection pooling DBCP and C3P0