Implementation of a simple JDBC Connection pool (i)

Source: Internet
Author: User

1. Foreword

database applications, which are often used in many software systems, are indispensable to the development of large-scale systems. However, if the database resources are not well managed (such as: the cursor (ResultSet), Statement, Connection (Connection), etc.), it will lead directly to the stability of the system. This kind of instability factors, not only by the database or the system itself, only after the formal use of the system, with the flow, the increase in users will gradually reveal.

In B based Java development system, JDBC is the main way for programmers to deal with database, and provides a complete database operation method interface. However, given the applicability of the specification, JDBC only provides the most direct database operation specification, the management of database resources, such as: the management and buffering of physical connection, expect the provision of 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 resource is managed effectively in a simple way.

2. Connection Pool Technical 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 illustration:

When you get a database connection, you need to specify the type of driver as a URL, and after you get a specific connection, you can manipulate different types of databases, such as obtaining statement, executing SQL resultset, and so on, as 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 you complete the data operation, you must also close all the database resources involved. This has no effect on the logic of the application, but 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 will inevitably be sparse. As with the memory leak problem in C, the Java system also faces a crash of bad luck. Therefore, the management of database resources relies on the application system itself, which is a hidden danger of insecurity and instability.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.