MyBatis官方文档
介绍
PooledConnection实现了InvocationHandler接口
源码
private static final String CLOSE = "close" ;
private static final Class< ? > [ ] IFACES = new Class < ? > [ ] { Connection. class } ;
private final int hashCode;
private final PooledDataSource dataSource;
private final Connection realConnection;
private final Connection proxyConnection;
private long checkoutTimestamp;
private long createdTimestamp;
private long lastUsedTimestamp;
private int connectionTypeCode;
private boolean valid;
public PooledConnection ( Connection connection, PooledDataSource dataSource) {
this . hashCode = connection. hashCode ( ) ;
this . realConnection = connection;
this . dataSource = dataSource;
this . createdTimestamp = System. currentTimeMillis ( ) ;
this . lastUsedTimestamp = System. currentTimeMillis ( ) ;
this . valid = true ;
this . proxyConnection = ( Connection) Proxy. newProxyInstance ( Connection. class . getClassLoader ( ) , IFACES, this ) ;
}
@Override
public boolean equals ( Object obj) {
if ( obj instanceof PooledConnection ) {
return realConnection. hashCode ( ) == ( ( ( PooledConnection) obj) . realConnection. hashCode ( ) ) ;
} else if ( obj instanceof Connection ) {
return hashCode == obj. hashCode ( ) ;
} else {
return false ;
}
}
@Override
public Object invoke ( Object proxy, Method method, Object[ ] args) throws Throwable {
String methodName = method. getName ( ) ;
if ( CLOSE. hashCode ( ) == methodName. hashCode ( ) && CLOSE. equals ( methodName) ) {
dataSource. pushConnection ( this ) ;
return null;
} else {
try {
if ( ! Object. class . equals ( method. getDeclaringClass ( ) ) ) {
checkConnection ( ) ;
}
return method. invoke ( realConnection, args) ;
} catch ( Throwable t) {
throw ExceptionUtil. unwrapThrowable ( t) ;
}
}
}
InvocationHandler源码
package java. lang. reflect;
public interface InvocationHandler {
public Object invoke ( Object proxy, Method method, Object[ ] args)
throws Throwable;
}