1.1 C3p0有三种方式检测连接的有效性
1:idleConnectionTestPeriod(异步)
每多少秒检查所有连接池中的空闲连接。Default: 0
C3P0PooledConnectionPool$PooledConnectionResourcePoolManager.refurbishIdleResource ()
public void refurbishIdleResource( Object resc ) throws Exception
{
if ( logger.isLoggable( MLevel.FINER ) )
finerLoggingTestPooledConnection( resc, "IDLE CHECK" );
else
//测试连接
testPooledConnection(resc );
}
2:testConnectionOnCheckout(同步)
从池中取得可用的连接,同时检查有效性
C3P0PooledConnectionPool$PooledConnectionResourcePoolManager.refurbishResourceOnCheckout()
if ( testConnectionOnCheckout )
{
if ( logger.isLoggable(MLevel.FINER ) )
finerLoggingTestPooledConnection( resc, "CHECKOUT" );
else
//测试连接
testPooledConnection( resc );
}
if ( connectionCustomizer != null )
{
ConnectionphysicalConnection = null;
try
{
physicalConnection= ((AbstractC3P0PooledConnection)resc).getPhysicalConnection();
connectionCustomizer.onCheckOut( physicalConnection,parentDataSourceIdentityToken );
}
catch(ClassCastException e)
{
throw SqlUtils.toSQLException("Cannot use a ConnectionCustomizer with a non-c3p0PooledConnection." +
" PooledConnection: " + resc +
"; ConnectionPoolDataSource: " + cpds.getClass().getName(), e);
}
}
3:testConnectionOnCheckin(异步)
释放连接到连接池,同时检查有效性
C3P0PooledConnectionPool$PooledConnectionResourcePoolManager.refurbishResourceOnCheckin ()
if ( connectionCustomizer != null )
{
ConnectionphysicalConnection = null;
try
{
physicalConnection= ((AbstractC3P0PooledConnection)resc).getPhysicalConnection();
connectionCustomizer.onCheckIn( physicalConnection,parentDataSourceIdentityToken );
SQLWarnings.logAndClearWarnings(physicalConnection );
}
catch(ClassCastException e)
{
throw