Implementing a JDBC Database connection pool in Java

Source: Internet
Author: User

"From e-mentor Network " Java programmers are envious of Windows ADO, just need new Connection can directly from the database connection pool to return Connection. and ADO Connection is thread-safe, multiple threads can share a single Connection, so the ASP program generally puts getconnection in the Global.asa file and establishes the database connection when IIS starts. The connection and result of ADO are well buffered and easy to use. Learn about the still Silicon Valley JDBC video tutorial .

We can actually write a JDBC database connection pool ourselves.

The considerations for writing JDBC Connection pool are:

1. There is a simple function to get a Connection from the connection pool.

2. The close function must put connection back into the database connection pool.

3. When there is no idle connection in the database connection pool, the database connection pool must be able to automatically increase the number of connection.

4, when the number of connection in the database connection pool becomes very large in a particular time, but after a long time to use only a small part, should be able to automatically turn off the extra connection.

5. If possible, the debug information report should be provided without closing the new Connection.

If you want new Connection to be able to return Connection directly from the database connection pool, you can write (mediator pattern) (Chinese full-width spaces are used in the following code):

It's not that hard. However, this is not recommended, because Java Interface should be avoided as much as possible, and I will write another discussion about the drawbacks of Java Interface. Everyone is concerned about the connection Pool implementation method. Here is an implementation method.

Use:

After you run the test program, print the connection state in the JDBC database connection pool, and you are using the connection information that is not closed. e-Mentor Network has more knowledge of programming language Tutorials for you to learn.

Implementing a JDBC Database connection pool in Java

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.