A realization method of database connection pool (1)
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;
ps = conn.prepareStatement (sql);
res = ps.executeUpdate ();
) finally (
) catch (Exception e) ()
) catch (Exception e) ()
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
- Example of a connection pool (from JIVE) (2)
- Example of a connection pool (from JIVE) (1)
- Optimize JDBC Programming
- JDBC connection with Oracle database (to resolve this problem, and would like to sum up are welcome to point out that lack of brothers)
- JDBC to connect oracle database top ten tips
- Through jdbc-odbc to Access database OLE types of table to insert data examples
- clob field to deal with the (oracle)
- To deal with a complete example of blob
- Correction method of remote database access! (Young master did not say it!)
- Dynamic use of JAVA agent database connection pool
- Oracle8 a jdbc connection examples (local page can )!!!!
- A realization method of database connection pool (1)
- A realization method of database connection pool (2)
- A realization method of database connection pool (3)
- Learn how to handle null database
- The establishment of object-oriented database
- JSP to connect SQL Server 2000 system configuration
- Access to database information using DatabaseMetaDate
- JAVA database connectivity issues do not have set up a data source
- J2ME visit procedures and Servlet Communication Access database
Can't Find What You're Looking For?
Rating: Not yet rated