Connection Pool technology and technology

Source: Internet
Author: User

Connection Pool technology and technology

Concept

The connection pool is a buffer pool technology for creating and managing a connection. After the connection pool is ready, it will be used by any thread that needs them. The technology that aggregates connections is based on the fact that for most applications, it usually takes several milliseconds to complete when they are being processed.

Principle

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, the connection pool can be efficiently and securely reused, this avoids the overhead of frequent establishment and shutdown of database connections. In addition, the original JDBC connection is encapsulated to facilitate database applications. The use of connections (especially for transaction processing) improves the development efficiency. Because of the existence of this encapsulation layer, the processing logic of the application and the access logic of specific data are isolated, it is possible to reuse the application itself.

Components

The connection pool consists of the establishment of the connection pool, the use and management of connections in the connection pool, and the closure of the connection pool.

Connection Pool Creation

The connection pool established by the application is actually a static connection pool. The so-called static connection pool refers to the connection pool that has been allocated during system initialization and cannot close the connection at will. Java provides many container classes to easily build a Connection pool, such as Vector, Stack, Servlet, and Bean. by reading the database Connection property file Connection. property, you can establish a Connection with the database instance. During system initialization, create a connection according to the corresponding configuration and place it in the connection pool, so that you can obtain the connection pool when necessary. This avoids the overhead caused by arbitrary connection establishment and shutdown.

Connection usage management in the connection pool

The connection pool management policy is the core of the connection pool mechanism. The allocation and release policies of the connection pool are very important for effective reuse and connection. The method we adopt is a famous design pattern: Reference Counting (Reference Counting ). This mode is widely used in resource reuse. It applies this method to the distribution and release of connections and keeps an application count for each database connection to record the number of users of the connection. The specific implementation method is as follows:

When a customer requests a database connection, first check whether there is any idle connection in the connection pool (that is, there is no allocated connection ). If there is an idle connection, the connection is allocated to the customer and processed accordingly (that is, mark the connection as being used, and Add 1 to the reference count ). If there is no idle connection, check whether the current number of connections has reached maxCon (maximum number of connections). If not, create a new connection to the requesting customer; if maxWaitTime is reached, it will be waited according to the set maxWaitTime (maximum wait time). If there is no idle connection after waiting for maxWaitTime, an exception Without idle connection will be thrown to the user.

When releasing 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 and determines whether the total number of connections in the current connection pool is less than minConn (minimum number of connections ), if the value is smaller than the value, the connection pool is full. If the value is not greater than the value, the connection is marked as open for reuse. This policy ensures the effective reuse of database connections and avoids system resource overhead caused by frequent connection establishment and release.

Close connection pool

When the application exits, the connection pool should be closed. When the connection pool is established, the connection objects applied to the database should be returned to the database in a unified manner (that is, the database connection used should be closed ), this is the opposite process of establishing a connection pool.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.