DBCP and C3P0 database connection pool,

Source: Internet
Author: User

DBCP and C3P0 database connection pool,

What is the database connection pool?

All those who have learned about computer networks know that in an internal LAN, most of them use private addresses. To deal with external networks, they must have corresponding legal external addresses. However, the number of internal users is huge, and an external IP address is unrealistic. In this way, there is a concept called connection pool. Because not every user needs to access the Internet at the same time, when a user needs to access the Internet, he can obtain an external IP address from the connection pool to access the Internet. When the user no longer needs to access the Internet, the IP address is put back in the connection pool and can be accessed by other users. The connection pool is mainly used to solve the problem of IP address quantity. In the database, there is also the concept of a connection pool. I think this connection pool is mainly used for connection reuse to achieve more efficient response to user requests. Common connection pools for java Development are dbcp and c3p0.

Introduction to c3p0:

C3P0 is an open-source JDBC connection pool that implements data source and JNDI binding and supports JDBC3 specifications and standard extensions of JDBC2. Currently, open-source projects using it include Hibernate and Spring.

Dbcp introduction:

DBCP (DataBase connection pool), DataBase connection pool. It 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 Since establishing a database connection is a very time-consuming behavior of resources, so through the connection pool in advance with the database to establish some connections, put in memory, when the application needs to establish a database connection, apply for one in the connection pool, and then store it back. 

Difference between c3p0 and dbcp:

Dbcp does not automatically recycle idle connections..

C3p0 supports automatic recovery of idle connections.

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 is as follows:

<! -- Configure the dbcp data source --> <bean id = "dataSource2" destroy-method = "close" class = "org. apache. commons. dbcp. basicDataSource "> <property name =" driverClassName "value =" $ {jdbc. driverClassName} "/> <property name =" url "value =" $ {jdbc. url} "/> <property name =" username "value =" $ {jdbc. username} "/> <property name =" password "value =" $ {jdbc. password} "/> <! -- Number of connections created when the pool is started --> <property name = "initialSize" value = "5"/> <! -- Maximum number of connections that can be allocated from the pool at the same time. If it is set to 0, there is no limit. --> <Property name = "maxActive" value = "30"/> <! -- Maximum number of idle connections that will not be released in the pool. If it is set to 0, there is no limit. --> <Property name = "maxIdle" value = "20"/> <! -- The minimum number of idle connections in the pool without creating a new connection. --> <Property name = "minIdle" value = "3"/> <! -- Set Automatic Recovery of timeout connections --> <property name = "removeAbandoned" value = "true"/> <! -- Automatic Recovery timeout (in seconds) --> <property name = "removeAbandonedTimeout" value = "200"/> <! -- Set to print the connection timeout error when the timeout connection is automatically reclaimed --> <property name = "logAbandoned" value = "true"/> <! -- The wait time is measured in milliseconds. The maximum wait time for the pool to wait for the connection to be recycled before an exception is thrown (when no available connection is available ). -1 indicates unlimited waiting. --> <Property name = "maxWait" value = "100"/> </bean> <! -- Configure the c3p0 data source --> <bean id = "dataSource" class = "com. mchange. v2.c3p0. comboPooledDataSource "destroy-method =" close "> <property 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 retained in the connection pool. Default: 15 --> <property name = "maxPoolSize" value = "100"/> <! -- The minimum number of connections retained in the connection pool. --> <Property name = "minPoolSize" value = "1"/> <! -- The number of connections obtained during initialization. The value must be between minPoolSize and maxPoolSize. Default: 3 --> <property name = "initialPoolSize" value = "10"/> <! -- Maximum idle time. connections are dropped if they are not used within 60 seconds. If it is 0, it will never be discarded. Default: 0 --> <property name = "maxIdleTime" value = "30"/> <! -- The number of connections that c3p0 obtains at the same time when connections in the connection pool are exhausted. Default: 3 --> <property name = "acquireIncrement" value = "5"/> <! -- JDBC standard parameter, used to control the number of PreparedStatements loaded in the data source. However, the pre-cached statements belong to a single connection rather than the entire connection pool. Therefore, you need to consider many factors when setting this parameter. If both maxStatements and maxStatementsPerConnection are 0, the cache is disabled. Default: 0 --> <property name = "maxStatements" value = "0"/> <! -- Check all idle connections in the connection pool every 60 seconds. Default: 0 --> <property name = "idleConnectionTestPeriod" value = "60"/> <! -- Defines the number of repeated attempts after a new connection fails to be obtained from the database. Default: 30 --> <property name = "acquireRetryAttempts" value = "30"/> <! -- Failed to obtain the connection will cause all threads waiting for the connection pool to obtain the connection to throw an exception. However, the data source is still valid, and the next call to getConnection () will continue to get the connection. If it is set to true, the data source will be declared disconnected and permanently closed after the connection fails to be obtained. Default: false --> <property name = "breakAfterAcquireFailure" value = "true"/> <! -- Because of high performance consumption, use it only when needed. If it is set to true, the validity of each connection is verified when it is submitted. We recommend that you use idleConnectionTestPeriod or automaticTestTable to improve the connection test performance. Default: false --> <property name = "testConnectionOnCheckout" value = "false"/> </bean>





Related Article

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.