Why use database connection pool?

1. Why use a database connection pool?

The most primitive database use is to open a connection and use it. After use, be sure to close the connection to release resources. Due to the frequent opening and closing of connections,
there is a certain resource load on the jvm including the database, especially when the application pressure is high, the resource occupation is more likely to cause performance problems. From this, the role of using the connection pool is revealed. His principle is actually not complicated:
first open a certain number of database connections, allocate them to the caller when they are used, and return them to the connection pool after the call is completed. Pay attention to these after returning to the connection pool. The connection is not closed, but is
ready to be allocated to the next caller. From this, it can be seen that the connection pool saves a lot of database connection opening and closing actions, which is self-evident to improve system performance.
2. Several basic concepts: the
minimum connection - the number of connections opened immediately after the application starts and the minimum number of subsequent connections maintained.
The maximum number of connections - the maximum number of connections that the application can use. The number of connections
to grow - the number of new connections opened by the application each time
. An example to illustrate the operation of the connection pool:
Assuming that the minimum and maximum connections are set to 10 and 20, then once the application starts, it will first open 10 database connections, but note that the number in use of the database connection pool at this time is 0--because you are not using these connections, and idle The number is 10. Then you start logging in. Assuming that the login code uses a connection to query, then the database connection pool's in-use number is 1 and the idle number is 9. This does not require opening a connection from the database - because the connection pool is ready. 10 for you. The login is over, what is the current number of connections in the connection pool? Of course it is 0, because that connection has been returned to the connection pool with the end of the transaction. Then 11 people log in at the same time at the same time, what will happen: The connection pool applies (opens) a new connection from the database, and sends it together with the other 10. The number of connections used in this instant connection pool is 11, but it doesn't matter Under normal circumstances, it will become 0 after a while. What if 21 people are logged in at the same time? The 21st person can only wait for the previous person to log in and release the connection to him. At this time, the connection pool has opened 20 database connections - although it is likely that the number of database connections in use has dropped to 0, will 20 connections be maintained? Of course not, the connection pool will close a certain amount of connections within a certain period of time and return it to the database. In this example, the number is 20-10=10, because only the minimum number of connections needs to be maintained, and this time period is also in the connection pool. configured.

 

Reference blog: https://www.cnblogs.com/aaron911/p/6213808.html

Guess you like

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