Database connection pools C3P0 and DBCP

Source: Internet
Author: User
Tags connection pooling

Reprint: http://www.cnblogs.com/haogeBlogs/p/5856302.html

The most common open source data connection pools are c3p0, dbcp, and Proxool, with three of them:
The Hibernate Development Group recommends the use of c3p0;
Spring Development Group recommends the use of DBCP (DBCP connection pool has WebLogic connection pool the same problem, is forced to close the connection or the database restart, cannot reconnect, tell the connection is reset, this setting can be resolved);
Hibernate in action recommends using C3P0 and proxool;

DBCP Required Jar:commons-dbcp.jar, Commons-pool.jar
C3P0 Required Jar:c3p0-0.9.2.1.jar Mchange-commons-java-0.2.3.4.jar

Proxool no contact for the time being

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.
DBCP Introduction:
DBCP (database connection pool), DB connection pool. is a Java Connection pool project on Apache and a connection pool component used by Tomcat. Use of DBCP alone requires 3 packages: Common-dbcp.jar,common-pool.jar, Common-collections.jar because establishing a database connection is a very time-consuming and resource-intensive behavior, 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 a line, run out and then put back.
The difference between C3P0 and DBCP:
DBCP does not automatically recycle the idle connection function C3P0 has the automatic recycle idle connection function
The two are mainly to the data connection processing way different! The C3P0 provides maximum idle time and DBCP provides the maximum number of connections.
The former connection is broken when the connection exceeds the maximum idle connection time. DBCP all connections will be disconnected when the number of connections exceeds the maximum number of connections.

How to configure:

<!--configuring DBCP data Sources --
      <bean id= "DataSource2" destroy-method= "Close" class= "Org.apache.commons.dbcp.BasicDataSource" > <prop        Erty name= "Driverclassname" value= "${jdbc.driverclassname}"/> <property name= "url" value= "${jdbc.url}"/> <property name= "username" value= "${jdbc.username}"/> <property name= "password" value= "${jdbc.passwor D} "/> <!--pool started with the number of connections created--<property name=" InitialSize "value=" 5 "/> <!--can be allocated from the pool at the same time The maximum number of connections. When set to 0, it means no limit. --<property name= "maxactive" value= "/> <!--the maximum number of idle connections that will not be released in the pool. When set to 0, it means no limit. --<property name= "Maxidle" value= "/> <!--the minimum number of connections that remain idle in the pool without creating a new connection. --<property name= "Minidle" value= "3"/> <!--Set Auto Recycle timeout connection--<property name= "Remo  Veabandoned "value=" true "/> <!--auto-reclaim time-out (in seconds)-<property name=" Removeabandonedtimeout " Value= "$"/> <!--set when auto-recycle timeout connectionTimeout Error--<property name= "logabandoned" value= "true"/> <!--wait timeout in milliseconds, before throwing an exception, the pool waits for the connection to be recycled the most Long time (when no connection is available).  Set to 1 to indicate an infinite wait. --<property name= "maxwait" value= "/> </bean> <!--configuration c3p0 data Source--&lt ; Bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method= "close" > <property nam        E= "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 idle time, unused in 60 seconds, the connection is discarded. If0 will never be discarded. default:0--<property name= "MaxIdleTime" value= "$"/> <!--when the connection in the connection pool runs out, c3p0 the number of connections fetched at the same time. Default:3--<property name= "Acquireincrement" value= "5"/> <!--JDBC Standard parameters to control the loading of prepareds in the data source Number of tatements. However, because the pre-cached statements belong to a single connection instead of the entire connection pool.          So setting this parameter takes into account a variety of factors. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. default:0--> <property name= "maxstatements" value= "0"/> <!--every 60 seconds to check for idle connections in all connection pools. 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 and continues to try 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 Only inUse it when you need it. If set to true then the validity of each connection submission is officer. We recommend using methods such as Idleconnectiontestperiod or automatictesttable to improve the performance of your connectivity tests. Default:false--<property name= "Testconnectiononcheckout" value= "false"/> </bean>

Database connection pooling c3p0 and dbcp

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.