The realization principle of database connection pool

1. Advantages of database connection pool technology

1. Resource reuse
Because database connections are reused, a large amount of performance overhead caused by frequent creation and release of connections is avoided. On the basis of reducing system consumption, on the other hand, it also improves the stability of the system's operating environment (reducing memory fragmentation and the number of temporary database processes/threads).

2. Faster system response speed
During the initialization process of the database connection pool, several database connections are often created and placed in the pool for standby. At this time, the initialization of the connection has been completed. For business request processing, the existing available connections are directly used to avoid the time overhead of the database connection initialization and release process, thereby reducing the overall response time of the system.

3. The new resource allocation method is
for systems where multiple applications share the same database. The database connection pool technology can be implemented at the application layer through the configuration of database connections. It may be a new topic a few years ago. For the current business system, if The application of the connection pool has not been considered in the design, so... Add this part of the content in the design document. The limitation of the maximum number of available database connections for an application prevents an application from monopolizing all database resources.

4. Unified connection management to avoid database connection leakage.
In the implementation of a relatively complete database connection pool, the occupied connection can be forcibly recovered according to the pre-set connection occupation timeout. This avoids resource leakage that may occur in conventional database connection operations. A minimized database connection pool implementation:

2. The principle of database connection pool

 2.1 JDBC

JDBC is a specification that follows the JDBC interface specification. Each database manufacturer implements its own driver (Driver), as shown in the following figure:

When an application obtains a database connection, it needs to specify the type of Driver in the form of URL. After obtaining a specific connection, it can operate different types of databases according to a fixed interface, such as: obtaining Statement separately , executing SQL to obtain ResultSet, etc., Such as the following example:

import java.sql.*;

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection dbConn = DriverManager.getConnection(“jdbc:oracle:thin:@127.0.0.1:1521:oracle”,”username”,”password”);
Statement st = dbConn.createStatement();
ResultSet rs = st.executeQuery(“select * from demo_table”);

…some data source operation in here

rs.close();
st.close();
dbConn.close();

After completing the data operation, be sure to close all the database resources involved. Although this has no effect on the logic of the application, it is a critical operation. The above is a simple example. If you mix with many if-else and exceptions, the management of resources will inevitably be overwhelming. Like the memory leak problem in C, the Java system will also face the bad luck of crashing. Therefore, the management of database resources depends on the application system itself, which is a hidden danger of insecurity and instability.

2.2 JDBC连接池
在标准JDBC对应用的接口中,并没有提供资源的管理方法。所以,缺省的资源管理由应用自己负责。虽然在JDBC规范中,多次提及资源的关闭/回收及其他的合理运用。但最稳妥的方式,还是为应用提供有效的管理手段。所以,JDBC为第三方应用服务器(Application Server)提供了一个由数据库厂家实现的管理标准接口:连接缓冲(connection pooling)。引入了连接池( Connection Pool )的概念 ,也就是以缓冲池的机制管理数据库的资源。

JDBC最常用的资源有三类:

— Connection: 数据库连接。

— Statement: 会话声明。
— ResultSet: 结果集游标。

分别存在以下的关系 :

这是一种“爷—父—子”的关系,对Connection的管理,就是对数据库资源的管理。举个例子: 如果想确定某个数据库连接(Connection)是否超时,则需要确定其(所有的)子Statement是否超时,同样,需要确定所有相关的 ResultSet是否超时;在关闭Connection前,需要关闭所有相关的Statement和ResultSet。
因此,连接池(Connection Pool)所起到的作用,不仅仅简单地管理Connection,还涉及到 Statement和ResultSet。

2.3 连接池(ConnectionPool)与资源管理
ConnectionPool以缓冲池的机制,在一定数量上限范围内,控制管理Connection,Statement和ResultSet。任何数据库的资源是有限的,如果被耗尽,则无法获得更多的数据服务。
在大多数情况下,资源的耗尽不是由于应用的正常负载过高,而是程序原因。
在实际工作中,数据资源往往是瓶颈资源,不同的应用都会访问同一数据源。其中某个应用耗尽了数据库资源后,意味其他的应用也无法正常运行。因此,ConnectionPool的第一个任务是限制:每个应用或系统可以拥有的最大资源。也就是确定连接池的大小(PoolSize)。
ConnectionPool的第二个任务:在连接池的大小(PoolSize)范围内,最大限度地使用资源,缩短数据库访问的使用周期。许多数据库中,连接(Connection)并不是资源的最小单元,控制Statement资源比Connection更重要。以Oracle为例:
每申请一个连接(Connection)会在物理网络(如 TCP/IP网络)上建立一个用于通讯的连接,在此连接上还可以申请一定数量的Statement。同一连接可提供的活跃Statement数量可以达到几百。在节约网络资源的同时,缩短了每次会话周期(物理连接的建立是个费时的操作)。但在一般的应用中,多数按照2.1范例操作,这样有10个程序调用,则会产生10次物理连接,每个Statement单独占用一个物理连接,这是极大的资源浪费。 ConnectionPool可以解决这个问题,让几十、几百个Statement只占用同一个物理连接, 发挥数据库原有的优点。
通过ConnectionPool对资源的有效管理,应用可以获得的Statement总数到达 :

(并发物理连接数)×(每个连接可提供的Statement数量)

例如某种数据库可同时建立的物理连接数为 200个,每个连接可同时提供250个Statement,那么ConnectionPool最终为应用提供的并发Statement总数为: 200 × 250 = 50,000个。这是个并发数字,很少有系统会突破这个量级。所以在本节的开始,指出资源的耗尽与应用程序直接管理有关。
对资源的优化管理,很大程度上依靠数据库自身的JDBC Driver是否具备。有些数据库的JDBC Driver并不支持Connection与Statement之间的逻辑连接功能,如SQLServer,我们只能等待她自身的更新版本了。
对资源的申请、释放、回收、共享和同步,这些管理是复杂精密的。所以,ConnectionPool另一个功能就是,封装这些操作,为应用提供简单的,甚至是不改变应用风格的调用接口。

3.简单JDBC连接池的实现

根据第二章中原理机制,Snap-ConnectionPool(一种简单快速的连接池工具,可在www.snapbug.net下载)按照部分的JDBC规范,实现了连接池所具备的对数据库资源有效管理功能。
3.1 体系描述
在JDBC规范中,应用通过驱动接口(Driver Interface)直接方法数据库的资源。为了有效、合理地管理资源,在应用与JDBC Driver之间,增加了连接池: Snap-ConnectionPool。并且通过面向对象的机制,使连接池的大部分操作是透明的。参见下图,Snap-ConnectionPool的体系:

图中所示,通过实现JDBC的部分资源对象接口( Connection, Statement, ResultSet ),在 Snap-ConnectionPool内部分别产生三种逻辑资源对象: PooledConnection, PooledStatement和 PooledResultSet。它们也是连接池主要的管理操作对象,并且继承了JDBC中相应的从属关系。这样的体系有以下几个特点:
— 透明性。在不改变应用原有的使用JDBC驱动接口的前提下,提供资源管理的服务。应用系统,如同原有的 JDBC,使用连接池提供的逻辑对象资源。简化了应用程序的连接池改造。
— 资源封装。复杂的资源管理被封装在 Snap-ConnectionPool内部,不需要应用系统过多的干涉。管理操作的可靠性、安全性由连接池保证。应用的干涉(如:主动关闭资源),只起到优化系统性能的作用,遗漏操作不会带来负面影响。
— 资源合理应用。按照JDBC中资源的从属关系,Snap-ConnectionPool不仅对Connection进行缓冲处理,对Statement也有相应的机制处理。在2.3已描述,合理运用Connection和Statement之间的关系,可以更大限度地使用资源。所以,Snap- ConnectionPool封装了Connection资源,通过内部管理PooledConnection,为应用系统提供更多的Statement 资源。
— 资源连锁管理。Snap-ConnectionPool包含的三种逻辑对象,继承了JDBC中相应对象之间的从属关系。在内部管理中,也依照从属关系进行连锁管理。例如:判断一个Connection是否超时,需要根据所包含的Statement是否活跃;判断Statement也要根据 ResultSet的活跃程度。

3.2 连接池集中管理ConnectionManager
ConnectionPool是Snap-ConnectionPool的连接池对象。在Snap-ConnectionPool内部,可以指定多个不同的连接池(ConnectionPool)为应用服务。ConnectionManager管理所有的连接池,每个连接池以不同的名称区别。通过配置文件适应不同的数据库种类。如下图所示:

通过ConnectionManager,可以同时管理多个不同的连接池,提供通一的管理界面。在应用系统中通过 ConnectionManager和相关的配置文件,可以将凌乱散落在各自应用程序中的数据库配置信息(包括:数据库名、用户、密码等信息),集中在一个文件中。便于系统的维护工作。

  3.3 连接池使用范例

对2.1的标准JDBC的使用范例,改为使用连接池,结果如下:

import java.sql.*;
import net.snapbug.util.dbtool.*;

..ConnectionPool dbConn = ConnectionManager
.getConnectionPool(“testOracle” );
Statement st = dbConn.createStatement();
ResultSet rs = st.executeQuery(
“select * from demo_table” );

some data source operation
in herers.close();st.close();

在例子中,Snap-ConnectionPool封装了应用对Connection的管理。只要改变JDBC获取Connection的方法,为获取连接池(ConnectionPool)(粗体部分),其他的数据操作都可以不做修改。按照这样的方式,Snap- ConnectionPool可帮助应用有效地管理数据库资源。如果应用忽视了最后资源的释放: rs.close() 和 st.close(),连接池会通过超时(time-out)机制,自动回收。

4.小结

无论是Snap-ConnectionPool还是其他的数据库连接池,都应当具备一下基本功能:

-对源数据库资源的保护

-充分利用发挥数据库的有效资源

-简化应用的数据库接口,封闭资源管理。

-对应用遗留资源的自动回收和整理,提高资源的再次利用率。

在这个前提下,应用程序才能投入更多的精力于各自的业务逻辑中。数据库资源也不再成为系统的瓶颈。


Guess you like

Origin blog.51cto.com/15082402/2644351