3. Implementation of simple JDBC Connection pool
According to the principle mechanism in the second chapter, Snap-connectionpool (a simple and fast connection pooling tool) realizes the effective management function of the connection pool to the database resources according to the JDBC specification of the part.
3.1 System Description
In the JDBC specification, resources are applied directly to the method database through the driver interface (Driver Interface). In order to manage resources effectively and rationally, a connection pool is added between the application and the JDBC driver: Snap-connectionpool. And through the object-oriented mechanism, the majority of the operation of the connection pool is transparent. See the following figure, the Snap-connectionpool system:
As shown in the figure, three logical resource objects are generated within Snap-connectionpool by implementing some of the resource object interfaces of JDBC (Connection, Statement, ResultSet): Pooledconnection, Pooledstatement and Pooledresultset. They are also the primary management object of the connection pool and inherit the corresponding dependencies in JDBC. Such a system has the following characteristics:
Transparency Provide resource management services without changing the original use of JDBC-driven interfaces. Application systems, like the original JDBC, use the logical object resources provided by the connection pool. Simplifies the application's connection pool transformation.
-Resource encapsulation. Complex resource management is encapsulated within the Snap-connectionpool and does not require excessive interference from the application system. The reliability and security of the management operation are ensured by connection pooling. Application of interference (such as: active shutdown of resources), only to optimize the performance of the system, the omission of the operation will not bring negative impact.
-Rational use of resources. According to the subordinate relationship of the resources in JDBC, snap-connectionpool not only connection the buffer, but also has the corresponding mechanism to deal with the statement. In 2.3 It has been described that the rational use of the relationship between connection and statement can be used more limited by resources. Therefore, Snap-connectionpool encapsulates connection resources and provides more statement resources for application systems through internal management pooledconnection.
-Resource chain management. The Snap-connectionpool contains three logical objects that inherit dependencies between the corresponding objects in JDBC. In the internal management, also according to the subordinate relation carries on the chain management. For example: To determine whether a connection timeout, need to be based on the included statement whether active, Judge statement also according to ResultSet active degree.
3.2 Connection Pool Centralized management ConnectionManager
ConnectionPool is the Snap-connectionpool connection pool object. Within the Snap-connectionpool, multiple different connection pools (ConnectionPool) can be specified for the application service. ConnectionManager manages all connection pools, and each connection pool is distinguished by different names. The configuration file adapts to different database types. As shown in the following illustration:
Through ConnectionManager, you can manage several different connection pools at the same time, providing a management interface of one. In the application system through ConnectionManager and related configuration files, can be scattered in their respective applications of the database configuration information (including: database name, user, password and other information), centralized in a file. Facilitate the maintenance of the system.