c3p0连接测试的源码分析

本文详细分析了C3p0连接池中三种检测连接有效性的方法:idleConnectionTestPeriod(异步)、testConnectionOnCheckout(同步)和testConnectionOnCheckin(异步)。所有方法最终都通过testPooledConnection方法实现,该方法会根据设定的测试查询或自动测试表进行连接有效性验证。此外,还介绍了如何自定义连接测试类以及测试查询的优先级规则。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值