Tutorials to .com

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

A realization method of database connection pool (3)

Print View , by: iSee ,Total views: 14 ,Word Count: 1370 ,Date: Sat, 18 Apr 2009 Time: 10:37 PM

DataSourceImpl is a realization of the javax.sql.DataSource interface class, such a connection pool to maintain the target. As a result of such is a protected category, so it exposed only to users of the methods defined in the DataSource interface methods, all the way to other users are not visible. We are concerned about the users first visit to a method getConnection


/ **
* @ See javax.sql.DataSource # getConnection (String, String)
* /
public Connection getConnection (String user, String password) throws SQLException
(
/ / First of all, free from the connection pool to find the target
Connection conn = getFreeConnection (0);
if (conn == null) (
/ / Determine whether the number of connections exceeds the maximum, if the connection exceeds the maximum number of
/ / Then wait for a certain period of time to see if there is free connection, or thrown to tell the user can be connected without
if (getConnectionCount ()> = connParam.getMaxConnection ())
conn = getFreeConnection (connParam.getWaitTime ());
else (/ / does not exceed the number of connections, re-access to a database connection
connParam.setUser (user);
connParam.setPassword (password);
Connection conn2 = DriverManager.getConnection (connParam.getUrl (),
user, password);
/ / Proxy will be returned to the connection object
_Connection _conn = New _Connection (conn2, true);
synchronized (conns) (
conns.add (_conn);
)
conn = _conn.getConnection ();
)
)
return conn;
)
/ **
* From the connection pool for a free connection
* @ Param nTimeout If the parameter value of 0 when not connected only to return a null
* Otherwise nTimeout milliseconds to wait to see if there are idle connections out if there is no abnormal
* @ Return Connection
* @ Throws SQLException
* /
protected synchronized Connection getFreeConnection (long nTimeout)
throws SQLException
(
Connection conn = null;
Iterator iter = conns.iterator ();
while (iter.hasNext ()) (
_Connection _conn = (_Connection) Iter.next ();
if (! _conn.isInUse ()) (
conn = _conn.getConnection ();
_conn.setInUse (true);
break;
)
)
if (conn == null & & nTimeout> 0) (
/ / Wait ms nTimeout to see if there is free connection
try (
Thread.sleep (nTimeout);
) catch (Exception e) ()
conn = getFreeConnection (0);
if (conn == null)
throw new SQLException ( "No database connection");
)
return conn;
)




DataSourceImpl class getConnection method to achieve with a normal database connection pool is the same logic, first of all determine whether there is any idle connection, if not determine whether the number of connections exceeds the maximum number of connections has some logic and so on. But there is one point difference is obtained through the DriverManager is not timely return the database connection, but through an intermediary called _Connection class and then call back to the _Connection.getConnection. If we do not through an intermediary that is java to take over the Proxy to return to the interface object, then there is no way we stopped Connection.close method.

Finally to the core, we first take a look at is how to achieve the _Connection, and then introduce the client to call methods Connection.close What follows is a process, why has not really shut down the connection.


/ **
* Since the data connection package and method of shielding a close
* @ Author Liudong
* /
class _Connection implements InvocationHandler
(
private final static String CLOSE_METHOD_NAME = "close";
private Connection conn = null;
/ / Busy status of the database
private boolean inUse = false;
/ / Users to visit the last time connection method
private long lastAccessTime = System.currentTimeMillis ();

_Connection (Connection conn, boolean inUse) (
this.conn = conn;
this.inUse = inUse;
)
/ **
* Returns the conn.
* @ Return Connection
* /
public Connection getConnection () (
/ / Return the database connection conn-type receiver in order to stop close method
Connection conn2 = (Connection) Proxy.newProxyInstance (
conn.getClass (). getClassLoader (),
conn.getClass (). getInterfaces (), this);
return conn2;
)
/ **
* This method is a real closure of a database connection
* @ Throws SQLException
* /
void close () throws SQLException (
/ / Conn is due to type of property has not been taken over the connection, so call the close method as soon after the closure of the direct connection
conn.close ();
)
/ **
* Returns the inUse.
* @ Return boolean
* /
public boolean isInUse () (
return inUse;
)

/ **
* @ See java.lang.reflect.InvocationHandler # invoke (java.lang.Object, java.lang.reflect.Method, java.lang.Object)
* /
public Object invoke (Object proxy, Method m, Object [] args)
throws Throwable
(
Object obj = null;
/ / Determine whether to call the close method, if the method call while connected home close to useless state
if (CLOSE_METHOD_NAME.equals (m.getName ()))
setInUse (false);
else
obj = m.invoke (conn, args);
/ / Set the last access time in order to timely clear connection timeout
lastAccessTime = System.currentTimeMillis ();
return obj;
)

/ **
* Returns the lastAccessTime.
* @ Return long
* /
public long getLastAccessTime () (
return lastAccessTime;
)

/ **
* Sets the inUse.
* @ Param inUse The inUse to set
* /
public void setInUse (boolean inUse) (
this.inUse = inUse;
)
)




Once users get connected to call the close method, as a result of the user's connection object is the object after the takeover, so the opportunity to java virtual _Connection.invoke call first method, the method in the first method to determine whether to close, if not will be code has not been transferred to the real object to take over the connection conn. Otherwise, simply state that the connection is set to available. This you may understand the whole process of taking over, but at the same time there is a doubt: this is the case is not such a connection has been established yet no solution has been a real turn off? The answer is yes. We take a look at ConnectionFactory.unbind method to find the name of the corresponding first connection pool object, and then close the connection pool and all connections removed connection pool. DataSourceImpl the definition of class in a close method to close all the connection code details are as follows:


/ **
* Close the connection all the database connection pool
Return * @ return int the number of connections have been closed
* @ Throws SQLException
* /
public int close () throws SQLException
(
int C++ = 0;
SQLException excp = null;
Iterator iter = conns.iterator ();
while (iter.hasNext ()) (
try (
((_Connection) Iter.next ()). Close ();
cc + +;
) catch (Exception e) (
if (e instanceof SQLException)
excp = (SQLException) e;
)
)
if (excp! = null)
throw excp;
return cc;
)




This method is called one by one for each connection pool object close approach, the close method is _Connection corresponding to the realization of the close, in the definition of _Connection time to close the database connection is not directly call for closure of the object of takeover approach , so the way to really close the database resources released.

Text only description of the above methods of the interface to take over the specific connection pool of a practical module also need to connect to the free and timely release of the monitor connected to a detailed code please refer to the annex.


Jsp And Database Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.