What Is Connection Pooling?
Connection pooling is a technique for creating and managing a pool of connections that are ready to be used by any thread that needs them.
connection pool
- This connection "gathering" technique is based on the fact that for most applications, when they are processing, it usually takes milliseconds to complete
- In actual application development, especially in WEB application system, if
- 1.Connection pool model
- The connection pool discussed in this article includes a connection pool class (DBConnectionPool) and a connection pool management class (DBConnetionPoolManager). The connection pool class is a "buffer pool" for all connections to a database. It mainly implements the following functions: obtain or create available connections from the connection pool; return the connection to the connection pool after use; before the system is shut down, disconnect Open all connections and release the system resources occupied by the connection; Can also handle invalid connections (originally registered as available connections, which are no longer available for some reason, such as timeouts, communication problems), and can limit the total number of connections in the connection pool. Below a certain predetermined value and not more than a certain predetermined value.
- The connection pool management class is the wrapper of the connection pool class, which conforms to the singleton pattern, that is, there can be only one instance of the connection pool management class in the system. It is mainly used for the management of multiple connection pool objects, and has the following functions: Load and register a JDBC driver for a specific database; Create a connection pool object according to the information given in the property file; To facilitate the management of multiple connection pools Object, to get a name for each connection pool object, to achieve the mapping between the connection pool name and its instance; track the client's use of the connection, in order to close the connection to release resources when needed. The introduction of the connection pool management class is mainly to facilitate the use and management of multiple connection pools. For example, the system needs to connect to different databases, or to the same database, but due to security issues, different users need to use different names and passwords.
- 2. Connection pool implementation
- The main attributes of the connection pool class and connection pool management class and the basic interfaces to be implemented are given below:
- public class DBConnectionPool implements TimerListener {
- private int checkedOut; // Number of connections that have been allocated
- private ArrayList freeConnections = new ArrayList (); // Container, free pool, stores created but not yet allocated connections according to // creation time sequence
- private int minConn; // The minimum number of connections in the connection pool
- private int maxConn; // The maximum number of connections allowed in the connection pool
- private String name; // A name for this connection pool for easy management
- private String password; // The password required to connect to the database
- private String url; // The address of the database to create a connection
- private String user; // User name required to connect to the database
- public Timer timer; // Timer
- public DBConnectionPool (String name, String URL, String user, String
- password, int maxConn) // public constructor
- public synchronized void freeConnection (Connection con) // After use, return the connection to the free pool
- public synchronized Connection getConnection (long timeout) // Get a connection, // timeout is the waiting time
- public synchronized void release () // Disconnect all connections and release the occupied system resources
- private Connection newConnection () // Create a new database connection
- public synchronized void TimerEvent () // Timer event handler
- }
- public class DBConnectionManager {
- static private DBConnectionManager instance; // The only instance of the connection pool management class
- static private int clients; // Number of clients
- private ArrayList drivers = new ArrayList (); // Container, store the database driver
- private HashMap pools = new HashMap (); // Access the connection pool in the form of name / value // the name of the object and the connection pool object
- static synchronized public DBConnectionManager getInstance () // If the only // instance instance has been created, return this instance directly; otherwise, call private
- In the development of database-related applications using JDBC, the management of database connections is a difficult point. In many cases, the excessive system resource overhead caused by the chaotic management of connections has become a bottleneck restricting the efficiency of large enterprise applications. For web applications accessed by many users, systems using database connection technology are much better in efficiency and stability than systems using other traditional methods. This article describes the technology of using JDBC to access the database? Discusses the key issues of database connection management based on connection pool technology and gives an implementation model. The article gives a basic model of the connection pool management program. In order to improve the overall performance of the system, many meaningful extensions can be made on this basis.