(BY huihoo.org quaker tan) Database connection pool The basic principle of traditional database connection (refers to the connection with the DRIVERMANAGER and the basic implementation DataSource), a database connection object corresponds to a physical database connection, the establishment of the database connection, and close the pair The system is the operation of the system resource, which is especially obvious in the performance of the system in the application environment of the multi-layer structure. In a multi-layer structure, the connection pool can be apparent from the connection pool, which means that the connection pool means that when the application needs to call a database connection, the database is reused by returning. Database connections are replaced by re-creating a database connection. In this way, the application can reduce the database connection operation, especially in multi-layer environments, multiple clients can meet system requirements by sharing a small amount of physical database connections. By connecting pool technology Java applications not only improve system performance, but also improves measurable. The database connection pool is running in the background and there is no impact on the application's coding. The premise of this situation is that the application must replace the original DRIVERMANAGER class by the DataSource object (an instance of the Javax.Sql.DataSource interface) to obtain the database connection. A class that implements a Javax.SQL.DataSource interface can also support the database connection pool, but the code that gets the database connection is basically the same. The code is as follows: A DataSource object usually registers on the JNDI naming service, the application can get the DataSource object registered on the JNDI service by standard. CONTEXT CTX = New InitialContext (); DataSource DS = (DataSource) CTX.lookup ("JDBC / OpenBase"); if the current DataSource does not support the database connection pool, the application will get a Connection object corresponding to the physical database connection. If the current DataSource object supports the database connection pool, the application automatically gets reuse database connections without creating a new database connection. Reuse database connections and newly established database connections are not available. The application can access the data for the operation of accessing the data by reused access to the database, and the database connection should be explicitly invoked after the operation is completed. Connection con = DS.GetConnection ("User", "PWD"); related database operations; con.close (); When the data connection is turned off, the currently used database connection will not be physically closed, but back to Reuse in the database connection pool. Database connection pool framework JDBC3.0 specification in JDBC3.0 specification provides a framework that supports database connection pools, this framework simply specifies how to support the implementation of the connection pool, and the specific implementation of the connection pool JDBC 3.0 specification is not related Provisions. Through this framework, the developers of different roles can make the database connection pool. The implementation of the specific database connection pool can be divided into JDBC DRIVER-level and Application Server levels. Any related work in the implementation of the JDBC Driver level is implemented by the developers of a specific database vendor's JDBC Drvier, that is, JDBC Driver requires both support for database connection pools to implement specific implementations.
The JDBC Driver developers and Application Server developers of the Database Connection Pool in the Application Server level are implemented to implement the implementation of the database connection pool (but now the mechanism and specifications of the connection pools implemented by most Application Server vendors " It is mentioned that there is a difference), in which a specific database vendor's JDBC Driver provides a database connection pool support and a specific Application Server vendor provides a specific implementation of the database connection pool. The JDBC3.0 specification specifies the following classes and interfaces to support the implementation of the database connection pool. Javax.sql.connectionEvent javax.sql.connectionPoolDataSource javax.sql.pooledconnection javax.sql.connectionEventListener Co., Ltd. is a class in addition to javax.sql.connectionEvent, and other interfaces.
C: /1.jpg JDBC3.0 Connection Chart of Connection Pool Framework This figure can probably understand the location of the application in a typical three-layer environment by this figure. In the database connection pool implementation, the JDBC Driver developers of a particular database vendor provide connection pool support, while the specific Application Server provides connection pool implementation is more complicated, and other implementations can be considered as a simplified situation. This will be described below. There are mainly two user roles in this frame, which are: JDBC Driver developers of a specific database vendor, will then connect to the pool developers in Driver Vendor Specific Application Server, which will also be referred to as Pooling Vendor C: / 2. BMP JDBC3.0 specification In the above case, the UML map between the various interfaces and classes is detailed in detail: Driver Vendor DataSource: Driver Vendor must provide a concrete implementation of a ConnectionPoolDataSource interface, through this interface POOLING Vendor can get a PooledConnection object that enables a third-party connection pool to get a database connection generated by JDBC Driver using a specific database vendor. The role played by the ConnectionPoolDataSource interface here can be considered as a factory that produces the PooledConnection object. Driver Vendor PooledConnection: Driver Vendor must provide a class implemented by the standard PooledConnection interface, which allows Pooling Vendor to implement the connection pool based on JDBC Driver provides connection pool support. A concrete PooledConnection object represents a physical database connection; creating a Connection object by the PooledConnection object is just a handle pointing to the PooledConnetion object. The role of the PooledConnection object-played role in the framework in the JDBC 3.0 connection pool can be treated as a factory that produces the Connection object. Pooling Vendor DataSource: POOLING VENDOR must implement the DataSource interface, this interface is an entry point that interacts with the connection pool implementation module. CONNECTIONPOOLDATASOURCE creates the PooledConnection object as needed. Pooling Vendor Connection Cache: This module is the specific implementation of Pooling Vendor on the connection pool. The JDBC 3.0 specification does not specify the interface between the DataSource object and the database connection pool implementation, so the interaction between them is defined by the pooling vendor. In general, a specific implementation of a database connection pool contains one or several specific classes, but must include a class implementation standard ConnectionEventListener interface in the connection pool implementation module. When a PooledConnectionD object is turned off or an exception, the PooledConnection object will send a ConnectionEventEvent object to the ConnectionEventListener interface, and the connection pool implementation module will turn off or reuse the PooledConnection according to the returned ConnectionEvent object. ConnectionEvent: When the connection pool is implemented, when the application calls connection.close () Attempts to turn off the database connection, you need to have an advertisement to connect to the connection pool implementation module, and the current database is reused for the current database physical connection (PooledConnection object).
In order to enable the connecting pool implementation module to get this "notification", the connection pool implementation module must implement the ConnectionEventListener interface, and also register as a listener for the PooledConnection object. The connection pool implementation module is registered as a listener through the PooledConnection.addConnectionEventListener () method. Specific call flow in a typical three-layer environment: Get a database connection when the application is called by calling DataSource.getConnection (). Pooling Vendor implemented DataSource object Find in the connection pool to see if there is a valid PooledConnection object if there is a useable PooledConnection in the connection pool, if the current PooledConnection is available. If PooledConnection object if the connection pool is not available, or the current PooledConnection object is not correct, then Pooling Vendor call ConnectionPoolDataSource.getPooledConnection create a new class of PooledConnection object, then by the ConnectionPoolDataSource Driver Vendor implemented to meet the requirements will create a new PooledConnection object and return it to the connection pool implementation module for management. Then Pooling Vendor will call PooledConnection.getConnection () to get a logical Connection object, which will return to the application as normal of the normal Connection object. This logical Connection object is actually a handle of the PooledConnection object in the connection pool. When the connection pool is valid, the application calls DataSource.getConnection () will get this handle. Briefly, the Connection object used at this time is just the handle of its creator PooledConnection object. Connection Pool Implementation Module Call PooledConnection.addConnectionEventListener () Survect yourself as a listener of a PooledConnection object, and connect pool implementation modules to connect to the pool implementation module when the database connection needs to be reused or off. When the application turns off the database connection by calling Connection.Close (), then a ConnectionEvent object is created and returned to the connection pool implementation module, and the connection pool implementation module receives this notification, returning the PooledConnection object to the pool for reuse . Other roles in these processes can access the PooledConnection.Close () method, which can access this method only Pooling Vendor, which use this method to operate objects in the connection pool, and pass the PooledConnection.Close () method to turn off the physical database connection. Organize release: admin