如何检查 select 1 在休眠 C3P0 中是否有效?

How to check if select 1 works or not in hibernate C3P0?

我正在使用 hibernateC3P0,我的目标是测试 select 1; 是否有效,我想在我的控制台中记录 select 1。 我的控制台中有此日志,但我没有执行 select 1:

2020-05-12 13:19:56,365 [http-nio-8080-exec-3] INFO  (AbstractPoolBackedDataSource:462) - Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@fbfe8ac5 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@66460a64 [ acquireIncrement -> 1, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1brb47waa2g67nfppp467|33e7dcd8, idleConnectionTestPeriod -> 60, initialPoolSize -> 3, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 100, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 50, maxStatements -> 100, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@dd22c2a7 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1brb47waa2g67nfppp467|7944ff00, jdbcUrl -> jdbc:mysql://192.168.1.13:3306/jbpm, properties -> {autoReconnect=true, is-connection-validation-required=true, user=******, password=******, autoReconnectForPools=true} ], preferredTestQuery -> select 1;, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> true, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1brb47waa2g67nfppp467|6f2545f6, numHelperThreads -> 3 ]

hibernate.cfg.xml

<property name="hibernate.c3p0.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>                
    <property name="hibernate.c3p0.acquire_increment">1</property> 
    <property name="hibernate.c3p0.idle_test_period">1200</property>
    <property name="hibernate.c3p0.max_size">50</property> 
    <property name="hibernate.c3p0.max_statements">100</property>           
    <property name="hibernate.c3p0.min_size">5</property> 
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.preferredTestQuery">select 1;</property>

如果应用程序正常运行,则测试没有失败。 (您有 testConnectionOnCheckout -> true,因此如果测试失败,您的应用程序将没有连接。)

但是,如果您使用的是最新的 c3p0 和非古老的(JDBC4 或更高版本)JDBC 驱动程序,您可能会完全忽略首选测试查询,并依赖 Connection.isValid() 专为此目的而设计。

如果您将 logger com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool 设置为 TRACE 或您正在使用的任何日志库中的等效项,您可以获得 c3p0 测试的一些日志记录。不幸的是,它不记录测试查询。