JDBC connection

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.

JDBC

1. Connection pool

Establishing database connections is time-consuming and resource-consuming, and the number of connections that a database server can establish at the same time is limited. In large-scale web applications, there may be hundreds of requests to access the database at the same time. The program allocates a database connection for each client request, which will result in a sharp drop in performance. In order to reuse the database connection, improve the response time to the request and the performance of the server, connection pooling technology can be used. The connection pool technology establishes multiple database connection objects in advance, and then saves the connection objects in the connection pool. When a client request arrives, a connection object is taken out from the pool to serve the client. When the request is completed, the client program calls the close() method. , put the connection object back into the pool.
In an ordinary database access program, the connection object obtained by the client program is a physical connection, and calling the close() method of the connection object will close the connection, but using the connection pool technology, the connection object obtained by the client program is one of the physical connections in the connection pool. Handle, call the close() method of the connection object, the physical connection is not closed, the realization of the data source just deletes the connection between the connection object in the client program and the connection object in the pool.

image

1. First prepare some database connection objects and put them in the pool
. 2. Wait for the program to grab the object. If the subsequent program does not catch the object, it will be in a waiting state. Java provides
3. The purpose of the database connection pool is to optimize the database connection object. Automatically add the driver to you, you need to add the driver yourself

Javax.sql.DataSource data source interface, no matter what method you use to optimize, just give me a data source object. There is a method getConnection() method, which returns the Connection object.

2、DBCP

Available to download the jar package through the maven repository

image

org.apache.commons.dbcp2.BasicDataSource specific operation class

image

The old version of the maximum number of connections method setMaxActive(3);

Note: After the objects in the connection pool are used up, you must call close and put them back in the pool, otherwise it will affect the connection between other programs and the database

Version 1: Resource management is not very good

3、C3P0

JAR packages can be downloaded via maven

image

Automatically recycle resources

image

Guess you like

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