Database connectivity is a critical, limited, and expensive resource, which is particularly evident in enterprise-class applications. The management of database connections can significantly affect the scalability and robustness of the entire application, affecting the performance of the program. The database connection pool was raised for this issue.
The database connection pool is responsible for allocating, managing, and freeing the database connection, which allows the application to reuse an existing database connection instead of re-establishing a database connection that has been idle for more than the maximum idle time to avoid missing database connections due to the absence of a database connection being freed. This technology can significantly improve the performance of database operations.
The specific working mechanism is as follows:
When the database connection pool is initialized, a certain number of database connections are created in the connection pool, and the number of these database connections is set by the minimum number of database connections. Regardless of whether these database connections are being used, the connection pool will always be guaranteed to have at least so many connections. The maximum number of database connections for a connection pool limits the maximum number of connections that this pool can occupy, and these requests are added to the wait queue when the number of connections requested by the application to the connection pool exceeds the maximum number of connections.
The minimum number of connections and the maximum number of connections for a database connection pool are set to take into account the following factors:
1) The minimum number of connections is the connection pool has been maintained database connection, so if the application to the database connection is not used, there will be a large number of database connection resources are wasted;
2) The maximum number of connections is the maximum number of connections the connection pool can request, and if the database connection request exceeds this number, subsequent database connection requests are added to the wait queue, which affects subsequent database operations.
3) If the minimum number of connections is too large for the maximum number of connections, the first connection request will take profit, and then the connection request that exceeds the minimum number of connections is equivalent to establishing a new database connection. However, these database connections that are larger than the minimum number of connections will not be released immediately after they are used, and will be placed in the connection pool for reuse or to be freed after an idle timeout.