Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze the implementation of a simple JDBC connection Pool

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to analyze the implementation of a simple JDBC connection pool? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

JDBC (Java Data Base Connectivity,java Database connection) is a Java API used to execute SQL statements, which can provide unified access to a variety of relational databases. It consists of a set of classes and interfaces written in Java language. JDBC connection pool.

1. Preface

Database application, which is often used in many software systems, is an indispensable assistant in the development of large-scale systems. However, if the database resources are not well managed (for example, the ResultSet, Statement, Connection and other resources of the database are not reclaimed in time), it will often lead to the stability of the system directly. Such unstable factors are not only caused by the database or the system itself, but will be gradually exposed with the increase of traffic and users after the formal use of the system.

In the system developed based on Java, JDBC is the main way for programmers to deal with database, and provides a complete interface for database operation methods. However, considering the applicability of the specification, JDBC only provides the most direct database operation specification. For database resource management, such as physical connection management and buffering, it is expected to be provided by a third-party application server (Application Server).

Based on the JDBC specification, this paper introduces the related database connection pool mechanism, and introduces the related implementation technology if the database resources are managed effectively in a simple way.

two。 Connection pooling technology background

2.1JDBC

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

When an application acquires a database connection, it needs to specify the type of Driver in the way of URL. After obtaining a specific connection, you can operate different types of databases according to fixed APIs, such as obtaining Statement, executing SQL to obtain ResultSet, and so on, as shown in 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 herers.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 key operation. The above is a simple example, if mixed with a large number of if-else, exception, the management of resources is inevitable. Like the memory leak problem in C #, the Java system will also face the bad luck of crash. Therefore, the management of database resources depends on the application system itself, which is a hidden danger of insecurity and instability.

2.2JDBC connection pool

In the interface of standard JDBC to application, there is no method of resource management. Therefore, the default resource management is the application's own responsibility. Although in the JDBC specification, the closure / recovery of resources and other reasonable use are mentioned many times. But the safest way is to provide effective management means for the application. Therefore, JDBC provides a standard management interface for third-party application servers (Application Server) implemented by database vendors: connection buffering (connection pooling). The concept of connection pool (Connection Pool) is introduced, that is, the resource of the database is managed by the mechanism of buffer pool.

There are three types of resources most commonly used in JDBC:

Connection: database connection.

Statement: session declaration.

ResultSet: result set cursor.

There are the following relationships:

This is a "master-father-son" relationship, the management of Connection is the management of database resources. For example: if you want to determine whether a database connection (Connection) times out, you need to determine whether its (all) child Statement timed out, as well as whether all relevant ResultSet timed out; before shutting down Connection, you need to close all related Statement and ResultSet.

Therefore, the role of connection pooling (Connection Pool) is not only to simply manage Connection, but also to Statement and ResultSet.

2.3 connection pooling (ConnectionPool) and resource management

ConnectionPool uses the mechanism of buffer pool to control and manage Connection,Statement and ResultSet within a certain upper limit. The resources of any database are limited, and if it is exhausted, it will not be able to obtain more data services.

In most cases, resource exhaustion is not due to the high normal load of the application, but to the program.

In practical work, data resources are often bottleneck resources, and different applications will access the same data source. After one of the applications runs out of database resources, it means that other applications can not run properly. Therefore, the * tasks of ConnectionPool are to limit the * resources that each application or system can have. That is, determine the size of the connection pool (PoolSize).

The second task of ConnectionPool is to make limited use of resources and shorten the usage cycle of database access within the size of the connection pool (PoolSize). In many databases, a Connection is not the smallest unit of a resource, and controlling Statement resources is more important than Connection. Take Oracle as an example:

Each application for a connection (Connection) establishes a connection for communication on a physical network, such as a TCP/IP network, on which a certain number of Statement can be applied. The number of active Statement that can be provided by the same connection can reach hundreds. While saving network resources, each session cycle is shortened (the establishment of a physical connection is a time-consuming operation). However, in general applications, most of them operate according to the 2.1example, so if there are 10 program calls, there will be 10 physical connections, and each Statement occupies a separate physical connection, which is a great waste of resources. ConnectionPool can solve this problem, so that dozens or hundreds of Statement only occupy the same physical connection, giving full play to the original advantages of the database.

Through the effective management of resources by ConnectionPool, the total number of Statement that can be obtained by the application reaches:

(concurrent physical connections) x (number of Statement that can be provided per connection)

For example, if a database can establish 200 physical connections at the same time, and each connection can provide 250 Statement at the same time, then the total number of concurrent Statement provided by ConnectionPool for the application is 200 x 250 = 50000. This is a concurrent number, and few systems break through this order of magnitude. So at the beginning of this section, it is pointed out that resource exhaustion is related to the direct management of the application.

The optimal management of resources depends to a large extent on whether the database has its own JDBC Driver. The JDBC Driver of some databases does not support the logical connection between Connection and Statement, such as SQLServer, so we have to wait for an updated version of it.

The application, release, recovery, sharing and synchronization of resources are complex and sophisticated. Therefore, another function of ConnectionPool is to encapsulate these operations and provide a simple calling interface for the application, even without changing the style of the application.

3. Implementation of simple JDBC connection Pool

According to the principle and mechanism in the second chapter, Snap-ConnectionPool (a simple and fast connection pooling tool) realizes the effective management function of database resources that connection pooling has according to some JDBC specifications.

3.1 system description

In the JDBC specification, resources that apply direct method databases through the driver interface (Driver Interface). In order to manage resources effectively and reasonably, a connection pool, Snap-ConnectionPool, is added between the application and JDBC Driver. And through the object-oriented mechanism, most of the operation of connection pool is transparent.

By implementing some resource object interfaces (Connection, Statement, ResultSet) of JDBC, three kinds of logical resource objects are generated in Snap-ConnectionPool: PooledConnection, PooledStatement and PooledResultSet. They are also the main management operations for connection pooling and inherit the corresponding dependencies in JDBC. Such a system has the following characteristics:

(1) transparency.

The service of resource management is provided without changing the original application using JDBC driver interface. The application system, like the original JDBC, uses the logical object resources provided by the connection pool. Simplifies the connection pool transformation of the application.

(2) Resource encapsulation.

Complex resource management is encapsulated in Snap-ConnectionPool and does not require too much interference from the application system. The reliability and security of the management operation are guaranteed by the connection pool. The interference of the application (such as actively shutting down resources) can only optimize the performance of the system, and the omission operation will not have a negative impact.

(3) rational use of resources.

According to the dependency of resources in JDBC, Snap-ConnectionPool not only buffers Connection, but also has corresponding mechanism to deal with Statement. As described in 2. 3, the rational use of the relationship between Connection and Statement can make greater use of resources. Therefore, Snap-ConnectionPool encapsulates Connection resources and provides more Statement resources for application systems through internal management of PooledConnection.

(4) chain management of resources.

Snap-ConnectionPool contains three logical objects that inherit the dependencies between the corresponding objects in JDBC. In the internal management, chain management is also carried out according to the subordinate relationship. For example, to judge whether a Connection times out or not, you need to judge whether the included Statement is active or not, and the Statement is also judged by the activity level of the ResultSet.

3.2 connection pooling centralized management ConnectionManager

ConnectionPool is the connection pooling object of Snap-ConnectionPool. Within Snap-ConnectionPool, you can specify several different connection pools (ConnectionPool) to serve the application. ConnectionManager manages all connection pools, each with a different name. Adapt to different types of databases through configuration files.

Through ConnectionManager, you can manage multiple different connection pools at the same time, providing a unified management interface. Through ConnectionManager and related configuration files in the application system, the database configuration information (including database name, user, password, etc.) scattered in their respective applications can be centralized in one file. To facilitate the maintenance of the system.

3.3 sample use of connection pooling

The example of using the standard JDBC of 2.1uses connection pooling instead, and the results are as follows:

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 ()

In the example, Snap-ConnectionPool encapsulates the application's management of Connection. As long as you change the method of JDBC to get Connection, in order to get the connection pool (ConnectionPool) (bold), other data operations can not be modified. In this way, Snap-ConnectionPool can help applications manage database resources effectively. If the application ignores the release of resources: rs.close () and st.close (), the connection pool is automatically reclaimed through the time-out mechanism.

Both Snap-ConnectionPool and other database connection pooling should have the following basic functions:

Protection of source database resources

Make full use of the effective resources of the database

Simplify the database interface of the application and close the resource management.

Automatic recovery and arrangement of application legacy resources to improve the re-utilization of resources.

Under this premise, applications can devote more energy to their respective business logic. Database resources are no longer the bottleneck of the system.

This is the answer to the question on how to analyze the implementation of a simple JDBC connection pool. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report