Hibernate - org.hibernate.exception.GenericJDBCException:无法打开连接

时间:2014-05-22 12:43:16

标签: java hibernate orm connection c3p0

为了解决与DB和空闲连接的连接过多的问题,我们对配置文件进行了以下更改 -

c3p0.min_size=1
c3p0.max_size=20
c3p0.timeout=1800
c3p0.max_statements=50
connection.release_mode=on_close

之后,在尝试运行应用程序时,我们遇到以下异常。请告知为什么会发生这种情况&如何纠正这个问题 -


HTTP Status 500 - org.hibernate.exception.GenericJDBCException: Cannot open connection

输入例外报告

message org.hibernate.exception.GenericJDBCException: Cannot open connection

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: org.hibernate.exception.GenericJDBCException: Cannot open connection
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:286)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    com.alci.filters.ParamFilter.doFilter(ParamFilter.java:27)
root cause

org.hibernate.exception.GenericJDBCException: Cannot open connection
    org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
    org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
    org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
    org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
    org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
    org.hibernate.loader.Loader.doQuery(Loader.java:696)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    org.hibernate.loader.Loader.doList(Loader.java:2232)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    org.hibernate.loader.Loader.list(Loader.java:2124)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    com.alci.common.LoginAction.login(LoginAction.java:127)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    com.alci.filters.ParamFilter.doFilter(ParamFilter.java:27)
root cause

java.sql.SQLException: Connections could not be acquired from the underlying database!
    com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
    com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
    org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
    org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
    org.hibernate.loader.Loader.doQuery(Loader.java:696)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    org.hibernate.loader.Loader.doList(Loader.java:2232)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    org.hibernate.loader.Loader.list(Loader.java:2124)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    com.alci.common.LoginAction.login(LoginAction.java:127)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    com.alci.filters.ParamFilter.doFilter(ParamFilter.java:27)
root cause

com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
    com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
    com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
    com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
    com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
    org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)
    org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1577)
    org.hibernate.loader.Loader.doQuery(Loader.java:696)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    org.hibernate.loader.Loader.doList(Loader.java:2232)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    org.hibernate.loader.Loader.list(Loader.java:2124)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    com.alci.common.LoginAction.login(LoginAction.java:127)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
    org.apache.struts.chain.commands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
    org.apache.struts.chain.commands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
    org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:51)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
    org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
    org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:283)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    com.alci.filters.ParamFilter.doFilter(ParamFilter.java:27)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.36 logs.

2 个答案:

答案 0 :(得分:1)

您用完了数据库连接。

这意味着两件事:

  1. 由于查询速度慢,您的交易需要花费太多时间,因此连接时间太长
  2. 您的流量需求非常高,无法适应20个连接池大小。
  3. 我建议您阅读以下read my answer of this SO question

    I don't think this settings is recommended

    connection.release_mode=on_close
    
      

    ON_CLOSE - 基本上是上面描述的遗留行为。该   Hibernate会话在首次需要执行时获取连接   一些JDBC访问并保持该连接直到会话为止   关闭。

         

    on_close - 说要使用ConnectionReleaseMode.ON_CLOSE。这个设置是   为了向后兼容而离开了,但是非常不鼓励使用它。

    尝试使用" auto"代替。

答案 1 :(得分:0)

有时由于超时而出现。增加超时时间,有望解决该问题。

c3p0.timeout=6000