Java implementation summary of database connection pool

Source: Internet
Author: User
Because the work needs to use the connection pool, I read the articles of many predecessors on the Internet and learned a lot of experience. Here I want to make a summary and add my own ideas together, hope to help you.
Purpose:
Eliminate the overhead and bottlenecks caused by frequent database connections.
Solution:
However, many restrictions on the use of the user, neither too many require the user to obtain and use the database connection according to the prescribed method
Maintain user habits as much as possible
Currently, many methods require that you only use connections as required, rather than directly close data connections. The solution is to use the proxy class for intermediate solutions. See http://www-900.ibm.com/developerWorks/cn/java/l-connpoolproxy/index.shtml
Maintain normal connection status
Because resources created for database connections cannot be released in time, the usage of the next data connection will be affected. For example, in SQL 2 K, multiple Statement entries are created for one connection. Otherwise, an exception occurs when the data connection is busy. Therefore, these resources must be released after the connection is returned.
// The createStatement statement if (CREATESTATE. equals (method. getName () {obj = method. invoke (conn, args); statRef = (Statement) obj; // record Statement return obj ;}
// Determine whether the close method is called. if the close method is called, the connection is set to useless if (CLOSE. equals (method. getName () {// set the flag not to use setIsFree (false); // Check whether there is any subsequent work and clear useless resources for this connection if (statRef! = Null) statRef. close (); if (prestatRef! = Null) prestatRef. close (); return null ;}
Correct protection class is not used in violation
One consideration is that users cannot use proxy classes at will, but can only use them by themselves. One is to use internal private classes, and the other is to use the flag that can be called only by specified classes. My implementation is to adopt the latter.
/*** Create a connection factory and only allow the factory to call * @ param factory to call the factory, the * @ param Connection parameter * @ return Connection */static public _ Connection getConnection (ConnectionFactory factory, ConnectionParam param) {if (factory. isCreate () // Determine whether the factory is correctly initialized {_ Connection _ conn = new _ Connection (param); return _ conn;} else return null ;}

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.