JDBC database connection pool

Disclaimer: The materials used in this column are written by VIP students of Kaige Academy. Students have the right to remain anonymous and have the right to final interpretation of the article. Kaige Academy aims to promote VIP students to learn from each other based on public notes.

1. Database connection pool

What is database connection pooling?

image

The current method of connecting to the database is to obtain the connection object Connection through the interface method DriverManager.getConnection. However, it is very troublesome and resource-intensive to obtain the database connection object in this way every time.
Every time a connection object is needed, it takes time to obtain the database connection object through this method, which is very time-consuming. How to optimize the acquisition of database connection objects to make the time consumption shorter:

image

Use database connection pool: use several database connection object variables to connect with the database before connecting to the database (always connect, not disconnect), wait in the pool, and go directly to the pool when you want to use the database connection object Use the objects that have been connected in advance to use, and then return the objects to the pool after use. If there are three database connection objects connected in advance in the pool, but all three objects are used at one time, when the next user comes to use the database connection object, he has to wait until the previous ones are used up. Objects are also returned to the pool and then used again.
The purpose of the connection pool is to prepare a number of objects that have been connected to the database in advance and are ready to be used, but when you want to use it, take it out of the pool and use it, and then put it back for others to use Optimize database connections.

image

Java company did not do the realization of database connection pool, it just made a specification, and did not give a specific concept of database connection pool, but made a data source javax.sql.DataSource, this database source is only database connection A standardized interface for optimization methods, as for how you implement this database connection pool, you define it yourself. Or you don't want to use the database connection pool yourself, as long as you can provide me with database connection objects in a very optimized way here.

image

image

image

These are the two main methods of the data source interface. This interface only provides the specification for obtaining the database connection object and implements it by yourself. Two popular data source plugins in the industry:

image

C3P0 is a data source plug-in that is used more frequently and occupies less resources. DBCP is a data source plug-in provided by apache. The data source plug-in
launched by DBCP:DATABASE CONNECTION POOL:APACHE:

image

Under components under the apache official website. We use maven to download the jar package.

image

You can choose one of the two above.

image

image

The above is the specific operation class.
The purpose of the database connection pool is to optimize the database connection object. It will not automatically add the driver to you like jdbc. You need to manually add the driver yourself.

image

image

The connection method is as above.

image

image

It can be seen that we can use the class of the data source to obtain multiple connection objects, up to 8. By default, there are 8 connection objects in the pool. You can set the maximum number of connection objects in the pool by yourself:

image

image

Note: After the objects in the connection pool are used up, the close method must be called to put the objects back into the pool.

image

image

If one of them is closed, the fourth one can be connected again. If it is in the old version of DBCP, in this case, the hashcode of the fourth is the same as the hashcode of the third, which means that it is actually After the third connection is completed, the connection object is returned to the pool, and the fourth connection is still the same object.

image

The advantage of C3P0 is that it is fast and takes up less memory. Version 2 of DBCP has improved resource management, version 1 of DBCP is not good at resource management.
C3P0 is an open source data source plug-in, which can perform automatic resource recovery, mainly used in the two third-party frameworks of harbor and spring.

image

Download in maven.

image

The main operation class used is the above one, and its configuration information can be set using set or can be placed in a file.

image

image

image

In fact, the operation classes in DBCP and C3P0 implement the interface javax.sql.DataSource.
In the future, companies generally use C3P0 better, and optimize resources better.

image

The advantage of C3P0 is that it can automatically recycle resources and
use C3P0 in DBManager to obtain connection objects:

image

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326833524&siteId=291194637