The use of database connection pool DBCP and C3P0

Source: Internet
Author: User
Tags connection pooling

to request, if the corresponding user request efficiently, the application developer is a very important issue. Here are some of the solutions I have been exposed to   share with you.  
   learned computer networks know that in an internal LAN, most of the use of private addresses, to deal with external  , must have a corresponding legitimate external address. However, the number of internal users is huge, one machine an external ip  the
is unrealistic. So there's a concept called connection pooling. Because not every user to the Internet at the same time, when a   users need to surf the internet, he can get an external IP address from the connection pool, thereby accessing the external network. When this   user no longer needs to surf the Internet, this IP address is put back into the connection pool and can be accessed by other users. Here   's connection pool is primarily to address the problem of IP address number. In the database, there is also the concept of connection pooling. I think this   connection pool is mainly through the reuse of the connection, thus more efficient implementation of the response to user requests. The common connection pool developed for java  is mainly dbcp and c3p0, of course, after understanding the principle of connection pooling, users can also develop and create self-  connection pool.  
    Database connection Pooling principle: can refer to this article, no longer repeat:  
    http://www.kuqin.com/database/20080903/16384.html 
    The following mainly describes the use of DBCP and c3p0 under two examples, while giving comparisons.   


1 DBCP.
DBCP is a Java Connection pool project on Apache and a connection pool component used by Tomcat. Separate the

Using DBCP requires 3 packages: Common-dbcp.jar,common-pool.jar,common-collections.jar.

   Public classDbcputils {Private StaticDbcputils dbcputils=NULL; PrivateBasicdatasource bds=NULL; PrivateDatasourceconnectionfactory dscf=NULL; Privatedbcputils () {if(bds==NULL) BDS=NewBasicdatasource ();        Bds.seturl (Dbconsts.url);        Bds.setusername (Dbconsts.username);        Bds.setpassword (Dbconsts.password);                Bds.setdriverclassname (Dbconsts.driverclass); Bds.setmaxactive (100); Bds.setinitialsize (20); Bds.setmaxidle (20); Bds.setminidle (10); DSCF=Newdatasourceconnectionfactory (BDS); }     Public synchronized Staticdbcputils getinstance () {if(dbcputils==NULL) Dbcputils=Newdbcputils (); returndbcputils; }     PublicConnection getconnection () {Connection con=NULL; Try{con=(Connection) dscf.createconnection (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returncon; }         Public Static voidMain (string[] args)throwsSQLException {Connection con=NULL; Longbegin=System.currenttimemillis ();  for(inti=0;i<1000000;i++) {con=dbcputils.getinstance (). getconnection ();        Con.close (); }            LongEnd=System.currenttimemillis (); System.out.println ("Time-consuming:" + (End-begin) + "MS"); }}


The result: Time-consuming: 2078ms

2 c3p0.
C3P0 is an open source JDBC connection pool, which is published in the Lib directory with Hibernate, including implementation

The JDBC3 and JDBC2 extension specifications describe the DataSources objects of the connection and statement pools. When in use

The C3p0-*.jar package needs to be imported.

 Public classC3p0utils {Private StaticC3p0utils dbcputils=NULL; PrivateCombopooleddatasource cpds=NULL; Privatec3p0utils () {if(cpds==NULL) {CPDs=NewCombopooleddatasource ();        } cpds.setuser (Dbconsts.username);        Cpds.setpassword (Dbconsts.password);        Cpds.setjdbcurl (Dbconsts.url); Try{cpds.setdriverclass (dbconsts.driverclass); } Catch(propertyvetoexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } cpds.setinitialpoolsize (100); Cpds.setmaxidletime (20); Cpds.setmaxpoolsize (100); Cpds.setminpoolsize (10); }     Public synchronized Staticc3p0utils getinstance () {if(dbcputils==NULL) Dbcputils=Newc3p0utils (); returndbcputils; }     PublicConnection getconnection () {Connection con=NULL; Try{con=cpds.getconnection (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        returncon; }         Public Static voidMain (string[] args)throwsSQLException {Connection con=NULL; Longbegin=System.currenttimemillis ();  for(inti=0;i<1000000;i++) {con=c3p0utils.getinstance (). getconnection ();        Con.close (); }            LongEnd=System.currenttimemillis (); System.out.println ("Time-consuming:" + (End-begin) + "MS"); }}

Through the above two programs can be seen, DBCP has a higher efficiency than C3P0, but in practice, dbcp may appear lost

Connection is possible, and c3p0 stability is high. Therefore, C3P0 is widely used in practical applications.

The use of database connection pool DBCP and C3P0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.