Use Dynamic proxy in JAVA to implement database connection pool

Source: Internet
Author: User

The database connection pool is frequently used in writing application services. Too frequent connection to the database is a bottleneck for service performance. The buffer pool technology can be used to eliminate this bottleneck. We can find a lot of source programs about the database connection pool on the Internet, but we all find such a common problem: the implementation methods of these connection pools increase the Coupling Degree with users to varying degrees. Many connection pools require users to obtain database connections through the methods specified by them. We can understand that, after all, all application servers currently use this method to connect to databases. However, the other common problem is that they do not allow users to explicitly call the Connection. close () method at the same time, and they need to use a prescribed method to close the Connection. This approach has two disadvantages:
First, it changes users' usage habits and increases users' difficulty.
First, let's look at a normal database operation process:
Int executeSQL (String SQL) throws SQLException
{
Connection conn = getConnection (); // obtain the database Connection in a certain way
PreparedStatement ps = null;
Int res = 0;
Try {
Ps = conn. prepareStatement (SQL );
Res = ps.exe cuteUpdate ();
} Finally {
Try {
Ps. close ();
} Catch (Exception e ){}
Try {
Conn. close ();//
} Catch (Exception e ){}
}
Return res;
}
After using the database connection, the user usually directly calls the connection method close to release the database resources. If we use the implementation method of the connection pool we mentioned earlier, the statement conn. close () will be replaced by some specific statements.
Second, the connection pool cannot exclusively control all connections in the pool. Because the connection pool does not allow users to directly call the close method of the connection, once the user closes the database connection due to habits during use, the connection pool will not be able to maintain the status of all connections normally, this problem is more likely to occur when the connection pool and application are implemented by different developers.
Based on the two problems mentioned above, let's discuss how to solve these two terrible problems.
First, let's consider how the user wants to use the database connection pool. You can use a specific method to obtain the database Connection, and the Connection type should be standard java. SQL. Connection. After obtaining the database connection, you can perform any operations on the connection, including closing the connection.
Through the description of the user, how to take over the Connection. close method becomes the topic of our article.
To take over the database connection close method, we should have a mechanism similar to hook. For example, in Windows programming, we can use the Hook API to take over a Windows API. There is also a mechanism in JAVA. JAVA provides a Proxy class and an InvocationHandler, both of which are in the java. lang. reflect package. Let's take a look at how SUN's documents describe these two classes.

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.