Database - Connection Pool

Why use connection pooling:

- The establishment and closing of database connections consumes huge resources.

-Traditional database access method: One database access corresponds to one physical connection, and the physical connection must be opened and closed every time the database is operated, which seriously damages the system performance.

A database connection pool is an ideal solution for managing concurrent access to database connections.

The DriverManager manages database connections for single-threaded situations. In multi-threaded concurrent situations, in order to reuse database connections, control the total number of concurrent connections, and protect the database from connection overload, a database connection pool must be used.


-----Solution: Database Connection Pool----------

When the system is initially running, it actively establishes enough connections to form a pool. Every time an application requests a database connection, it does not need to reopen the connection, but takes out the existing connection from the pool. After using it, it will not be closed but returned.


Using Apache DBCP connection pooling

-DBCP (DateBase connection pool): database connection pool

-A Java connection pool open source project of Apache, which is also a connection pool component used by Tomcat.

-Connection pooling is a buffer pooling technique for creating and managing connections, preparing them for use by any application that needs them.


First we import the jar package, such as dbcp.

The digital version is purely usable, the others are beta versions.

In the digital version, for example, 1.4 is the latest version, and there are three kinds of Jar packages in it.

The first javadoc is the manual in the package, the second sources is the source code in the package, the third is what we need, and after importing, maven will automatically help us import the source code and use it.




The first time to use dbcp to connect to the database: it should be noted that the connection also has necessary parameters




The following uses DBUtils to encapsulate dbcp to make code reusable:





After packaging, test the feasibility:




Case 3 verifies the concurrency characteristics of the connection pool:



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324800010&siteId=291194637