本文介绍了org.hibernate.exception.GenericJDBCException:无法执行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用hibernate 3.6.7做映射。我将连接部分设置为静态。
我需要的服务程序永远运行,另一个服务会调用这个服务的一些方法来查询数据库。
当我离开服务运行时,第一天它运行良好,但当我第二天打电话给它时,它给出:

  org.hibernate.exception.GenericJDBCException:无法执行查询
在org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
在org.hibernate.exception.SQLStateConverter.convert (SQLStateConverter.java:128)
在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
在org.hibernate.loader.Loader.doList(Loader.java:2545)
在org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
在org.hibernate.loader.Loader.list(Loader.java:2271)
在org.hibernate。 loader.hql.QueryLoader.list(QueryLoader.java:459)
在org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
在org.hibernate.engine.query。 HQLQueryPlan.performList(HQLQueryPlan.java:196)
在org.hibernate.impl.SessionImp l.list(SessionImpl.java:1268)
在org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
在com.myCompany.Query.myMethod(Query.java:99)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
在java.lang.reflect.Method.invoke(Method.java:597)
在com.sun.jersey .spi.container.JavaMethodInvokerFactory $ 1.invoke(JavaMethodInvokerFactory.java:60)
在com.sun.jersey.server.impl.model.method.dispatch.Abs​​tractResourceMethodDispatchProvider $ TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
在com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
在com.sun.jersey.server.impl.uri.rules.HttpMethodRule .accept(HttpMethodRule.java:288)
在com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
在com.sun.jersey.server .impl.uri.rules.RightHandPathRule.acce pt(RightHandPathRule.java:147)
在com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
在com.sun.jersey.server。 impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
在com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
在com.sun。 jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
在com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
在com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
在javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
在org.ebayopensource .ginger.server.internal.container.MessageProcessor.processRequest(MessageProcessor.java:93)
在org.ebayopensource.ginger.server.container.GingerServletShell.service(GingerServletShell.java:80)
在org .apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
在org.apache.catalina .core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
在org.apache.catalina.core.StandardHostValve .invoke(StandardHostValve.java:128)
在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve .java:568)
在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
在org.apache.catalina.co nnector.CoyoteAdapter.service(CoyoteAdapter.java:286)
在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:583)
在org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:447)
在java.lang.Thread.run( Thread.java:662)
导致:java.sql.SQLException:关闭连接
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc .driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection .java:864)
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:783)
在org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
在org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
在org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
在org.hibernate.loader.Loader。 prepareQueryStatement(Loader.java:1700)
在org.hibernate.loader.Loader.doQuery(Loader.java:801)
在org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
在org.hibernate.loader.Loader.doList(Loader.java:2542)

看来连接被关闭了。
任何人都可以给我一些建议吗?



非常感谢:)

解决方案

听起来像数据库已关闭连接或某些网络设备已终止套接字。有很多方法可以解决这个问题:


  1. 您可以发出某种保持活着的查询类型(例如:SELECT 1)在连接上经常保持活着。

  2. 您可以每隔一段时间重新打开连接。

  3. 如果连接关闭例外,你可以重新打开连接。 Duh。

  4. 您可以使用可以进行保持活动和重新连接的连接池。 是许多人的最爱。

我推荐最后一个。您可以使用,例如:

  BasicDataSource ds = new BasicDataSource(); 
ds.setDriverClassName(oracle.jdbc.driver.OracleDriver);
ds.setUsername(scott);
ds.setPassword(tiger);
ds.setValidationQuery(SELECT 1); //这是数据库特定的
ds.setTestWhileIdle(true); //经常测试连接
ds.setUrl(connectURI);
...
while(!shutdown){
连接conn = dataSource.getConnection();
语句stmt = conn.createStatement();
...
stmt.close();
//这将连接返回到池,而不是真正关闭
//连接
conn.close();
}

所以,而不是传递一个 Connection 你需要连接和$ $ c $时,通过 BasicDataSource 并调用 getConnection() c> conn.close()当你完成它。池中的所有验证工作等等。它还允许多个线程使用数据库。



希望这有帮助。






编辑:你也不能持有打开了很长时间。为了向您展示会话应该是多么短暂,我将从文档中引用:

会话持有一个数据库连接,所以你永远不会想要保留任何长度的时间。您应该执行几个数据库请求(例如单个具有查询和更新的Web请求),然后关闭会话,以便将底层数据库连接返回到池。


I am using hibernate 3.6.7 to do mapping. I set the connection part as static.I need the service program runs forever, and another service will call some methods of this service to query database.When I leave the service running, the first day it works well but when I call it the next day, it gives:

org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2545)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
    at org.hibernate.loader.Loader.list(Loader.java:2271)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:459)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:365)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1268)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at com.myCompany.Query.myMethod(Query.java:99)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.ebayopensource.ginger.server.internal.container.MessageProcessor.processRequest(MessageProcessor.java:93)
    at org.ebayopensource.ginger.server.container.GingerServletShell.service(GingerServletShell.java:80)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:568)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.sql.SQLException: Closed Connection
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
    at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:864)
    at oracle.jdbc.driver.PhysicalConnection.prepareStatement(PhysicalConnection.java:783)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1700)
    at org.hibernate.loader.Loader.doQuery(Loader.java:801)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
    at org.hibernate.loader.Loader.doList(Loader.java:2542)

It seems the connection gets closed.Could anyone please give me some suggestion?

Thank you very much:)

解决方案

Sounds like either the database has closed the connection or some network device has terminated the socket. There are many ways you can work around this problem:

  1. You can issue some sort of "keep alive" type of query (ex: SELECT 1) on the connection every so often to keep it alive. This assumes that it got closed because it was idle.
  2. You can re-open the connection every so often.
  3. If you get a connect closed exception then you can just reopen the connection. Duh.
  4. You can use a connection pool which can do the keep-alive and the reconnection for you. Apache's DBCP is a favorite of many.

I recommend the last one. You would use DBCP something like:

BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
ds.setUsername("scott");
ds.setPassword("tiger");
ds.setValidationQuery("SELECT 1"); // this is database specific
ds.setTestWhileIdle(true); // test the connections every so often
ds.setUrl(connectURI);
...
while (!shutdown) {
    Connection conn = dataSource.getConnection();
    Statement stmt = conn.createStatement();
    ...
    stmt.close();
    // this returns the connection back to the pool instead of really closing
    // the connection
    conn.close();
}

So instead of passing around a Connection you pass around the BasicDataSource and call getConnection() when you need the connection and conn.close() when you are done with it. The pool does all of the validation work, etc. It also will allow multiple threads to use the database.

Hope this helps.


Edit: You also cannot hold a hibernate session open for a long time. To show you how short lived the session is supposed to be, I'll quote from the docs:

A session holds a database connection so you never want to keep it around for any length of time. You should do a couple of database requests (like a single web request with queries and updates) and then close the session so the underlying database connection can be returned to the pool.

这篇关于org.hibernate.exception.GenericJDBCException:无法执行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:12