c3p0在hwaitnate的awaitAvailable中挂起

时间:2012-12-31 20:45:22

标签: java mysql hibernate jdbc c3p0

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

    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");

这是我的stacktrace:

"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)

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

3 个答案:

答案 0 :(得分:13)

这会立即发生,还是一段时间后发生?也就是说,收银台最初是否成功,但是就这样挂了?如果是这样,它看起来像一个连接泄漏。请尝试设置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

如果立即发生这种情况,如果没有成功检出Connections,问题是池是否成功获取Connections。默认情况下,如果它永远不会成功,大约30秒后你的线程就会因为失败而中断。 (看起来你不是这样做的,但是如果你将acquireRetryAttempts设置为零,c3p0可能无限期地挂起等待Connections。)

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

祝你好运!

答案 1 :(得分:3)

此外,您似乎没有初始化c3p0的checkoutTime参数,该参数指定客户端应等待从连接池获取连接的时间。

请参阅http://www.mchange.com/projects/c3p0/#checkoutTimeout

答案 2 :(得分:0)

您可能需要增加 c3p0.numHelperThreads。辅助线程负责创建新的数据库连接并将它们添加到池中。如果没有足够的线程来满足需求,那么应用程序线程将在 awaitAvailable() 中等待。要确认是这种情况,请查看所有 HelperThread 并查看它们是否都在使用中以及是否正在连接到数据库。