DBCP and C3P0 database connection pool

Source: Internet
Author: User
Tags connection pooling

What does a database connection pool do?

Learned computer network is known, in an internal LAN. Most of the use of private addresses, in order to deal with the outside, you must have a corresponding legal external address corresponding. However, the number of internal users is huge. An external IP for a machine is unrealistic. So there's a concept called connection pooling. Since not every user has to surf the internet at the same time. When a user needs to surf the Internet, he is able to get an external IP address from the connection pool to access the network.

When the user no longer needs to surf the Internet. This IP address is put back into the connection pool. Can also give other users access to ask. The connection pool here is primarily to address the problem of the number of IP addresses. and 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 the user request. Common Java-developed connection pools are mainly dbcp and c3p0.

C3P0 Brief Introduction:

C3P0 is an open source JDBC connection pool. It implements the data source and Jndi bindings, and supports the standard extensions of the JDBC3 specification and JDBC2. The open source project that uses it now has hibernate,spring and so on.

DBCP Brief Introduction:

DBCP (database connection pool), DB connection pooling. is a Java Connection pool project on Apache and a connection pool component used by Tomcat. Using DBCP alone requires 3 packages: Common-dbcp.jar,common-pool.jar, Common-collections.jar because establishing a database connection is a time-consuming and resource-intensive activity, the connection pool is pre-established with the database to make some connections, put in memory, the application needs to establish a database connection directly to the connection pool to apply for one, and then put back.  

c3p0 and dbcp differences:

DBCP did not take the initiative to reclaim the spare connection function  .

C3P0 has its own active recycling spare connection function.

dbcp required Jar:commons-dbcp.jar, Commons-pool.jar, common-collections.jar
c3p0 required Jar:c3p0-0.9.2.1.jar mchange-commons-java-0.2.3.4.jar

applicationcontext.xml configuration information such as the following:

<!--Configure DBCP data Source-<bean id= "DataSource2" destroy-method= "Close" class= "Org.apache.commons.dbcp.BasicDataSo Urce "> <property name=" driverclassname "value=" ${jdbc.driverclassname} "/> <property name=" url "V Alue= "${jdbc.url}"/> <property name= "username" value= "${jdbc.username}"/> <property name= "Passwo        Rd "value=" ${jdbc.password} "/> <!--the number of connections created when the pool starts--<property name=" InitialSize "value=" 5 "/> <!--The maximum number of connections that can be allocated from the pool at the same time.

When set to 0, it means no limit.

--<property name= "maxactive" value= "/> <!--pool will not be released up to the maximum number of spare connections. When set to 0, it means no limit.

-<property name= "Maxidle" value= "/> <!--the minimum number of connections in the pool to keep spare without creating a new connection. --<property name= "Minidle" value= "3"/> <!--set your own active recycle timeout connection--<property name= "re Moveabandoned "value=" true "/> <!--self-reclaim time-out (in seconds)-<property name=" Removeabandonedtime Out "value="/> <!--Set the time-out error of the print connection when you actively reclaim the time-out connection-<property name= "logabandoned" Value= "Tru E "/> <!--wait timeout in milliseconds. Before throwing an exception. The maximum time the pool waits for a connection to be reclaimed (when no connection is available). Set to 1 to indicate an infinite wait.

-<property name= "maxwait" value= "/> </bean> <!--configuration c3p0 data source <bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "Close" > <propert Y name= "Jdbcurl" value= "${jdbc.url}"/> <property name= "Driverclass" value= "${jdbc.driverclassname}"/> <property name= "user" value= "${jdbc.username}"/> <property name= "password" value= "${jdbc.password}" /> <!--The maximum number of connections that are kept in the connection pool. Default:15---<property name= "maxpoolsize" value= "/>" <!--the minimum number of connections left in the connection pool. --<property name= "Minpoolsize" value= "1"/> <!--the number of connections obtained at initialization, the value should be between Minpoolsize and Maxpoolsize. Default:3---<property name= "initialpoolsize" value= "/> <!--maximum spare time, unused in 60 seconds the connection is discarded. If 0, it will never be discarded.

default:0--<property name= "MaxIdleTime" value= "$"/> <!--when the connection in the connection pool runs out, C3p0 gets the number of connections at the same time.

Default:3---<property name= "Acquireincrement" value= "5"/> <!--the standard reference number for JDBC. Used to control the number of preparedstatements loaded in the data source. But because the pre-cached statements belong to a single connection instead of the entire connection pool. So there are a number of factors that need to be taken into account in setting this parameter.

Assume that both the maxstatements and the maxstatementsperconnection are 0. The cache is closed. default:0--> <property name= "maxstatements" value= "0"/> <!--Check the spare connections in all connection pools every 60 seconds.

default:0--<property name= "idleconnectiontestperiod" value= "/>" <!--definition to get a new connection from the database The number of repeated attempts after the failure.

Default:30--<property name= "acquireretryattempts" value= "$"/> <!--getting a connection failure will cause all waiting connections The pool to get the connected thread throws an exception.

However, the data source is still valid for retention. and continue trying to get the connection the next time you call Getconnection ().

If set to True, the data source will declare broken and permanently shut down after attempting to acquire a connection failure. default:false--> <property name= "Breakafteracquirefailure" value= "true"/> <!--due to high performance consumption please Use it only when you need it.

If set to true then the validity of each connection submission is officer. It is recommended to use methods such as Idleconnectiontestperiod or automatictesttable to improve the performance of the connection test. Default:false--<property name= "Testconnectiononcheckout" value= "false"/> </bean>



DBCP and C3P0 database connection pool

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.