The connection pool is used to create and manage database connections. connections in the buffer pool can be used by any thread that needs them. When a thread needs to operate on a database using JDBC, a connection is requested from the pool. After the connection is used, it is returned to the connection pool, waiting for other threads to serve.
The main advantages of the connection pool are as follows.
1. Reduce the connection creation time. Connections in the connection pool are ready and reusable. After the connection pool is obtained, the database can be accessed directly, reducing the number and time of connection creation.
2. simplified programming mode. When using a connection pool, each separate thread can operate like creating a JDBC connection, allowing users to directly use the JDBC programming technology.
Third, control the use of resources. If you do not use a connection pool, you need to create a connection to access the database each time. In this way, the system stability is greatly affected by the system connection requirements, which may lead to resource waste and high load exceptions. The connection pool maximizes performance and controls resource utilization to a certain level. The connection pool can control the number of connections in the pool, enhancing the stability of the system in the case of a large number of users.
Next, briefly describe the working principle of the connection pool.
The core idea of connection pool technology is connection reuse. By establishing a database connection pool and a set of connection usage, allocation, and management policies, connections in the connection pool can be efficiently and securely reused, this avoids the overhead of frequent establishment and shutdown of database connections.
The working principle of the Connection Pool consists of three parts: the establishment of the connection pool, the use and management of connections in the connection pool, and the closure of the connection pool.
1. Create a connection pool. Generally, during system initialization, the connection pool is established according to the system configuration and several connection objects are created in the pool so that they can be obtained from the connection pool when used. The connections in the connection pool cannot be created or closed at will, which avoids system overhead caused by the arbitrary establishment and closure of connections. Java provides many container classes to easily build connection pools, such as vector and stack.
2. Manage the connection pool. The connection pool management policy is the core of the connection pool mechanism. The allocation and release of connections in the connection pool have a great impact on the system performance. Its management policy is:
When a customer requests a database connection, first check whether there is any idle connection in the connection pool. If there is a idle connection, the connection is allocated to the customer. If there is no idle connection, check whether the number of connections currently opened has reached the maximum number of connections. If the number does not reach the maximum number, create a new connection to the requesting customer. If the number reaches the maximum number of connections, wait according to the set maximum waiting time, if the maximum wait time is exceeded, an exception is thrown to the customer.
When a customer releases a database connection, the customer first determines whether the reference times of the connection have exceeded the specified value. If so, the customer deletes the connection from the connection pool. Otherwise, the database is retained as another customer service.
This policy ensures effective reuse of database connections and avoids system resource overhead caused by frequent connection establishment and release.
3. Close the connection pool. When the applicationProgramWhen you exit, close all connections in the connection pool and release resources related to the connection pool. This process is the opposite of creating a connection pool.