c3p0 getConnection()抛出异常:ResourcePool无法从其主工厂或源获取资源

时间:2012-11-20 21:49:22

标签: java mysql jdbc connection-pooling c3p0

我们的项目有[Hibernate + c3p0 + MySQL]配置。我们遇到以下异常:

com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319) ~[c3p0-0.9.1.2.jar:0.9.1.2]

    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557) ~[c3p0-0.9.1.2.jar:0.9.1.2]

    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477) ~[c3p0-0.9.1.2.jar:0.9.1.2]

    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525) ~[c3p0-0.9.1.2.jar:0.9.1.2]

Wrapped by: java.sql.SQLException: Connections could not be acquired from the underlying database!

    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106) ~[c3p0-0.9.1.2.jar:0.9.1.2]

    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529) ~[c3p0-0.9.1.2.jar:0.9.1.2]

    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128) ~[c3p0-0.9.1.2.jar:0.9.1.2]

    at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:276) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

Wrapped by: org.hibernate.exception.GenericJDBCException: Could not open connection

    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:52) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1263) ~[hibernate-core-4.0.1.Final.jar:4.0.1.Final]

    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:399) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]

Wrapped by: org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection

    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:440) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]

    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]

    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:336) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]

    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]

    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]

    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]

    at $Proxy28.messageExists(Unknown Source) ~[na:na]

我们的c3p0配置:

<bean id="provDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
…
        <property name="acquireIncrement" value="1" />
        <property name="idleConnectionTestPeriod" value="100"/>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="50" />
        <property name="maxIdleTime" value="1800" />
</bean>

请注意,我们还在此MySQL实例之上配置了另一个类似的c3p0数据源。

有人可以给我一些关于如何调试它的建议吗?

2 个答案:

答案 0 :(得分:0)

虽然没有严格回答你的问题,但我不想在评论中添加代码,因为评论中的代码是不可读的。

以下是我的项目中带有C3P0的MySQL的示例配置。记下所有额外的参数,以确保连接不会死亡。 (最后我和Postgres一起去了,因为它没有杀死我的连接,一般情况下更稳定,也不得不重新测试与select 1的连接;效率不高)

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
     destroy-method="close">
  <property name="driverClass" value="${jdbc.driverClassName}"/>
  <property name="jdbcUrl" value="${jdbc.url}"/>
  <property name="properties">
    <props>
        <prop key="c3p0.minPoolSize">0</prop>
        <prop key="hc3p0.maxPoolSize">1000</prop>
        <prop key="hc3p0.timeout">3000</prop>
        <prop key="c3p0.acquire_increment">10</prop>
        <prop key="c3p0.max_statement">50</prop>

        <prop key="hibernate.c3p0.idle_test_period">60</prop>
        <prop key="c3p0.preferredTestQuery">SELECT 1;</prop>
        <prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
        <prop key="user">${jdbc.username}</prop>
        <prop key="password">${jdbc.password}</prop>
    </props>
  </property>
</bean>

答案 1 :(得分:0)

我找到了以下帮助我解决问题的原因和解决方案列表。也可能帮助他人:

  1. 您输入的数据库配置详细信息不正确或已更改。例如,您的 Confluence 数据库的数据库名称或数据库用户名/密码。确认 /confluence.cfg.xml 中的数据库用户名、密码和连接 URL 不正确,如果不正确,请相应更新。
  2. 数据库的 URL 不正确。例如,如果数据库连接是jdbc:mysql://yma/confluence?autoReconnect=true,那么主机'yma'是否存在,数据库服务器是否安装在'yma'上?检查 /confluence.cfg.xml 以找到此条目。
  3. 数据库服务器未运行。如果您的数据库服务器已关闭,请重新启动它并检查日志以了解它失败或停止的原因。
  4. 网络中断(或 Confluence 与其数据库之间存在防火墙)并且无法建立与数据库的连接。如果您的网络出现故障或安装了新的防火墙,请咨询您的系统管理员以进一步排除故障。
  5. 您的数据库密码已过期。如果您的密码已过期(只有当您尝试通过数据库客户端或 DbVisualizer 等外部数据库工具连接到您的数据库时,您才会发现这一点),更新您的密码。您可能希望为您的 Confluence 数据库用户停用过期密码,以防止问题再次发生。请务必就此咨询您的 DBA。
  6. Postgres 特定问题:在迁移或升级 Confluence、环境等时 Postgres 加密方法已被更改或更改。检查 pg_hba.conf 文件。如果方法设置为 ident 将其更改为 md5。
  7. MySQL 特定问题:有太多连接请求触发 MySQL 服务器阻止 Confluence 发出任何请求。请参阅这篇 MySQL 文章以获取解决方案。

来源:https://confluence.atlassian.com/confkb/connections-could-not-be-acquired-from-the-underlying-database-104825087.html