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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate - org.hibernate.exception.GenericJDBCException:无法打开连接相关的知识,希望对你有一定的参考价值。

为了解决与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.
答案

您没有数据库连接。

这意味着两件事:

  1. 由于查询速度慢,您的交易需要花费太多时间,因此连接时间过长
  2. 您的流量需求非常高,无法容纳20个连接池大小。

我建议你阅读以下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。此设置留待向后兼容,但强烈建议不要使用它。

请尝试使用“自动”。

另一答案

这有时会因超时而产生。增加超时,它将有望解决问题。

c3p0.timeout=6000

以上是关于Hibernate - org.hibernate.exception.GenericJDBCException:无法打开连接的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate:org.hibernate.loader.MultipleBagFetchException:不能同时获取多个包

原因:org.hibernate.QueryException:节点没有数据类型:org.hibernate.hql.internal.ast.tree.MethodNode

org.hibernate.LazyInitializationException:懒惰初始化角色集合失败(Hibernate + Spring)

org.hibernate.MappingException:无法确定类型:java.util.List,在表:用户,列:[org.hibernate.mapping.Column(事件)]

hibernate - “org.hibernate.LazyInitializationException:无法初始化代理 - 没有会话”[重复]

org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.brid