????DataSourceImpl???????getConnection???????????????????????????????μ???????ж?????п??е???????????е???ж??????????????????????????????Щ??????????????????????DriverManager???????????????????????????????????????_Connection?????н???£???????_Connection.getConnection???????????????????????н????JAVA?е?Proxy?????????????????????????а????Connection.close??????

???????????????????????????????_Connection???????????????????????????Connection.close???????????????????????????????????????????

/**
* ?????????????????????close????
* @author Liudong
*/
class _Connection implements InvocationHandler
{
private final static String CLOSE_METHOD_NAME = "close";
private Connection conn = null;
//?????????
private boolean inUse = false;
//???????η????????????????
private long lastAccessTime = System.currentTimeMillis();
_Connection(Connection conn?? boolean inUse){
this.conn = conn;
this.inUse = inUse;
}
/**
* Returns the conn.
* @return Connection
*/
public Connection getConnection() {
//?????????????conn???????????close????
Connection conn2 = (Connection)Proxy.newProxyInstance(
conn.getClass().getClassLoader()??
conn.getClass().getInterfaces()??this);
return conn2;
}
/**
* ?÷???????????????????????
* @throws SQLException
*/
void close() throws SQLException{
//??????????conn????б????????????????????close???????????????
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;
//?ж?????????close??????????????close????????????????????
if(CLOSE_METHOD_NAME.equals(m.getName()))
setInUse(false);
else
obj = m.invoke(conn?? args);
//???ú???η??????????????????????
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;
}
}