一尘不染

c3p0挂起等待状态可与hibernate一起使用

hibernate

我有在执行过程中挂起的控制台应用程序。这是我的配置:

    cfg.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    cfg.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/db?user=db&password=db");
    cfg.setProperty("hibernate.connection.username", "db");
    cfg.setProperty("hibernate.connection.password", "db");
    cfg.setProperty("hibernate.connection.pool_size", "5");
    cfg.setProperty("hibernate.connection.autocommit", "false");
    cfg.setProperty("hibernate.c3p0.min_size", "5");
    cfg.setProperty("hibernate.c3p0.max_size", "20");
    cfg.setProperty("hibernate.c3p0.timeout", "300");
    cfg.setProperty("hibernate.c3p0.max_statements", "50");
    cfg.setProperty("hibernate.c3p0.idle_test_period", "3000");

这是我的堆栈跟踪:

"main" prio=10 tid=0x000000000168f800 nid=0x1c37 in Object.wait() [0x00007fa60d0ad000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000007400f4c68> (a com.mchange.v2.resourcepool.BasicResourcePool)
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1315)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        - locked <0x00000007400f4c68> (a com.mchange.v2.resourcepool.BasicResourcePool)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:84)
        at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:281)
        at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
        at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)
        at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
        at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
        at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1392)
        at org.kriyak.parser.IndexArchiveRapid.indexFile(IndexArchiveRapid.java:70)
        at org.kriyak.parser.IndexArchiveRapid.main(IndexArchiveRapid.java:53)

我只打开了一个锥形杯,但似乎没有泄漏。而且我也使用一个线程。除了内存使用率,我没有调整任何mysql设置。Mysql从控制台正常工作。为什么会发生这种情况?这是c3p0错误吗?


阅读 257

收藏
2020-06-20

共1个答案

一尘不染

立即发生还是在一段时间后发生?也就是说,结帐最初是否成功,但随后却像这样挂起?如果是这样,它看起来像是连接泄漏。请尝试将c3p0参数设置为unreturnedConnectionTimeout和debugUnreturnedConnectionStackTraces,以查看是否存在泄漏。见
http://www.mchange.com/projects/c3p0/#configuring_to_debug_and_workaround_broken_clientshttp://www.mchange.com/projects/c3p0/#unreturnedConnectionTimeouthttp://www.mchange.com/projects/c3p0/#
debugUnreturnedConnectionStackTraces。

如果立即发生这种情况,如果没有成功检出任何连接,则问题是池是否曾经成功获取连接。默认情况下,如果它永远不会成功,则大约30秒后,您的线程应该会因失败而中断。(这看起来好像没有完成,但是例如,如果您将acquireRetryAttempts设置为零,则c3p0可能会无限期挂起,以等待Connections。)

要调试c3p0问题,捕获池初始化时c3p0转储到INFO级别的日志的版本和配置信息会很有帮助。

祝好运!

2020-06-20