Tutorials to .com

Tutorials to .com » Jsp » Database » A realization method of database connection pool (1)

A realization method of database connection pool (1)

Print View , by: iSee ,Total views: 16 ,Word Count: 1015 ,Date: Sat, 18 Apr 2009 Time: 11:36 PM

database connection pool in the preparation of applications is often necessary to use the module, too frequent service to connect the database is a bottleneck in terms of performance, the use of buffer pool technology to eliminate the bottleneck. We can find a lot of the Internet on the source database connection pool, but found such a common problem: the realization of these methods of connection pooling are increased to varying degrees, and the coupling between users. Many have called the connection pool provided for the user, through its access to the database connection method, we can understand this point, after all, all the application servers the way check database connections are achieved in this way. However, another common problem is that they do not allow users at the same time explicit call Connection.close () method, and its provisions will need a way to close the connection. This approach has two shortcomings:

First: change the habits of users, an increase of the user's use of the difficulty.

First of all, we take a look at a normal operation of the database:

int executeSQL (String sql) throws SQLException
Connection conn = getConnection (); / / through some sort of access to the database connection
PreparedStatement ps = null;
int res = 0;
try (
ps = conn.prepareStatement (sql);
res = ps.executeUpdate ();
) finally (
try (
ps.close ();
) catch (Exception e) ()
try (
conn.close ();//
) catch (Exception e) ()
return res;

Users are usually used after the database connection is a direct call connection close to the release of database resources, if we mentioned earlier, the realization of the connection pool method statement conn.close () will be statements by certain alternative .

Second: The connection pool can not be carried out under the exclusive control of all connections. Since the connection pool does not allow users to connect directly call the close method, once the user in the use of the process used to directly as a result of the closure of a database connection, then connection pooling will not be able to maintain the normal state of all connections, consider the connection pool and application development by different staff to achieve when more prone to this problem.

Integrated the above-mentioned two issues, we discuss how to solve the problem of the two to death.

First of all, we look at first consider theusers to use the database connection pool. Users can access a specific way to connect the database and at the same time this type of connection should be standard java.sql.Connection. Users access to the database connection can be any connection to this operation, including the closure of the connection.

Through the description of users and how to take over our Connection.close method has become the subject of this article.

In order to take over the close database connection, we should have a hook-like mechanism. For example, in Windows programming Hook API we can use to achieve a takeover of the Windows API. In java will also be of such a mechanism. Proxy provides a java class and a InvocationHandler, these two categories in the java.lang.reflect package. Let's take a look at the documentation provided by the company SUN is how to describe these two categories.

public interface InvocationHandler

InvocationHandler is the interface implemented by the invocation handler of a proxy instance.

Each proxy instance has an associated invocation handler.
When a method is invoked on a proxy instance,
the method invocation is encoded and dispatched to the invoke method of its invocation handler.

SUN's API documentation on the description of Proxy many not listed here. InvocationHandler through the document on the interface description, we can see examples of when to invoke a method of Proxy will be triggered when the invoke method Invocationhanlder. Documents from JAVA, we also understand that the dynamic proxy mechanism that can only take over the interface method, and null and void on the general category, taking into account the java.sql.Connection interface itself is a result found a solution on how to take over the close approach way out.

First of all, we first define a database connection pool parameters of categories, the definition of the JDBC database driver class name, connection URL, as well as user name password, etc. Some of the information that such a connection pool for initialization of parameters, the specific definition of as follows:

public class ConnectionParam implements Serializable
private String driver; / / database-driven process
private String url; / / data connection URL
private String user; / / database user name
private String password; / / database password
private int minConnection = 0; / / initialize number of connections
private int maxConnection = 50; / / maximum number of connections
private long timeoutValue = 600000; / / Connect the biggest leisure time
private long waitTime = 30000; / / check connection if there is no available connection when the waiting time for the largest

Jsp And Database Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.