Connection pool detailed, c3p0 and DBCP difference!

Source: Internet
Author: User
Tags connection pooling

Connection pool:

connection pooling is the technique of creating and managing a pooled pool of connections that are ready to be used by any thread that needs them. This technology can significantly improve the performance of database operations.

Benefits of connection pooling:

(1) For most applications, only 1 threads that have access to the JDBC connection are required when they are working on a transaction. When a transaction is not processed, the connection is idle (causing a waste of resources). Instead, the connection pool allows idle connections to be used by other required threads.

(2) When a thread needs to use JDBC to operate on a database, it requests a connection from the pool. When this thread is finished using this connection, it is returned to the connection pool (the connection pool will automatically retract the connection and, of course, we can release it later), so this can be used by other threads that want to use it, so that it does not become a waste of memory.

(3) When the connection is called from the pool, the connection is used exclusively by the thread.

the main advantages of connection pooling are :

reduces connection creation time and Resource Usage : Because the connection is recycled, the time required to create the connection is less, and more importantly, it is not necessary to create a new connection every time the thread calls JDBC, resulting in a waste of resources.

② Simplified Programming pattern: When using connection pooling, each individual thread will be able to operate as if it had created its own JDBC connection, allowing the user to use JDBC programming techniques directly.

Note: each connection to the database has costs (memory, CPU, context switching, and so on) on both the client and server side. Each connection has a limited amount of resources available to the application and database servers. Regardless of whether these connections are doing useful work, a significant portion of these resources will still be used.

Connection pooling maximizes performance while also controlling resource utilization to a certain level, and if beyond that level, the application crashes rather than slows.


How the Connection pool works:

In the practical application development, especially in the Web application system, if using jdbc to directly access the data in the database, every data access request must go through the steps of establishing the database connection, opening the database, accessing the data and closing the database connection , etc. Connecting and opening the database is a time-consuming and resource-intensive task, and if such database operations occur frequently, not only will the performance of the system degrade dramatically, it may even cause the system to crash. Database connection pooling technology is the most common way to solve this problem, although the connection pool does not require our own programming, but we as a user still need to know more about this technology, understand will let us use handy.

the idea of database connection pooling technology is simple :

The database connection is stored as an object in a vector array, and once the database connection is established, the connections can be shared by different database access requests, so that by recycling these established database connections, you can save us from constantly creating new connections. Yes, and resources, Significant savings in system resources and time.

The main operations of the database connection pool are as follows:
(1) Establish database connection pool object (server start).
(2) Create the initial number of database connections (that is, the number of idle connections) according to the parameters specified beforehand, which we can specify when we install the database.
(3) For a database access request, get a connection directly from the connection pool. Create a new database connection if there is no idle connection in the database connection pool object and the number of connections does not reach the maximum (that is, the maximum number of active connections). If the maximum number of connections is reached, the number of pre-determined connections is created.
(4) The database access operation.
(5) Close the database, release all database connections (at this point, close the database connection, not really shut down, but put it in an idle queue.) Release the connection if the number of actual idle connections is greater than the number of initial idle connections).
(6) Release the database connection pool object (during server stop, maintenance, release the database connection pool object, and release all connections).

Implementation principle:

Baidu Encyclopedia has detailed records, I tell the words may not so that you can see the understanding (limited capacity).

c3p0:Definition of C3P0 and DBCP :

c3p0: 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: DBCP database Connection pool, which is a Java database connection pool, is developed by Apache and allows the program to automatically manage the release and disconnection of database connections through a database connection pool.

The difference between C3P0 and DBCP:

dbcp does not automatically reclaim idle connection functions

c3p0 has automatic recycle idle connection function

The two are mainly for the processing of data connections different C3P0 provide maximum idle time, DBCP provide the maximum number of connections. The former is if the connection time exceeds the maximum connection time, the current connection is disconnected. DBCP if the maximum number of connections is exceeded, all connections are disconnected.

Use of the C3P0 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.
1. Guide Package "C3p0-0.9.1.2.jar
2. Import the configuration file (not required, but use the profile in the enterprise)
If you do not use a configuration file, set the database parameter in code to 4
The project is divided into the development environment and the operating environment, at the time of development, the developer can not contact the actual company database. The data is confidential, but the database is also changed after development, if you do not need to change the code, it is to use the configuration file
3. Import the tool class you wrote (Initialize the connection pool to get the connection).
4. The tool class used in DAO gets a connection or connection pool (some tool classes can automatically help us to create a connection).
such as the Dbutil tool class

Configuration file:
The configuration file for some key-value pairs in Java specifically provides such classes and file names.
The suffix name of the Properties file
Properties read the. properties File class
Reading configuration Files
1. Turn the configuration file into a stream
2. Use the properties class provided by Java to load this stream
3. Calling the GetProperties method of the Properties object This method requires a parameter key name to return a value (string type)


Connection pool detailed, c3p0 and DBCP difference!

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.