scabbage
scabbage

Reputation: 1502

c3p0 getConnection() throws exception: A ResourcePool could not acquire a resource from its primary factory or source

We have a [Hibernate + c3p0 + MySQL] config for our project. We are hitting the following exception:

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]

Our c3p0 config:

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

Note that we also have another similar c3p0 datasource configured on top of this MySQL instance.

Can anyone give me some suggestions on how to debug this?

Upvotes: 14

Views: 15467

Answers (2)

Alexandru Severin
Alexandru Severin

Reputation: 6228

I found the following list of causes and resolutions which helped me solve the problem. Might also help others:

  1. The database configuration details you entered are incorrect or have changed. For example, the database name or database username/password for your Confluence database. Confirm that the database username, password, and connection URL in /confluence.cfg.xml are incorrect, and if not update them accordingly.
  2. The URL for the database is incorrect. For example, if the database connection is jdbc:mysql://yma/confluence?autoReconnect=true, does the host 'yma' exist and is the database server installed on 'yma'? Check /confluence.cfg.xml to find this entry.
  3. The database server is not running. If your database server is down, restart it and check the logs to see why it had failed or stopped.
  4. The network is down (or there is a firewall in between Confluence and its database) and the connection to the database cannot be established. If your network is down, or a new firewall is installed, consult with your System Administrator for further troubleshooting.
  5. Your database password has expired. If your password has expired (and you will only discover this if you try to connect to your database via your database client or an external database tool such as DbVisualizer), update your password. You may want to deactivate expiring passwords for your Confluence database user to prevent the problem from happening again. Be sure to consult with your DBA about this.
  6. Postgres specific issue: the Postgres encryption method has been altered or changed when migrating or upgrading Confluence, the environment, etc. Check the pg_hba.conf file. If the method is set to ident change it to md5.
  7. MySQL specific issue: there are too many connection requests that triggered MySQL server to block Confluence from making any requests. See this MySQL article for resolution.

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

Upvotes: 0

Brian
Brian

Reputation: 412

Although not strictly answering your question, I didn't want to add code in the comments as code in comments is unreadable.

Here is an example config of MySQL with C3P0 from my project. note all the extra parameters to make sure the connections don't die. (In the end I went with Postgres because it didn't kill my connections and was more stable generally, also having to retest connections with select 1; isn't very efficient)

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

Upvotes: 0

Related Questions