[英]org.hibernate.exception.GenericJDBCException: could not execute query
I am using hibernate 3.6.7 to do mapping.我正在使用 hibernate 3.6.7 进行映射。 I set the connection part as static.我将连接部分设置为 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: 有很多方法可以解决这个问题:
I recommend the last one. 我推荐最后一个。 You would use DBCP something like: 你会使用DBCP之类的东西:
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. 因此,当您需要连接时,不要传递Connection
传递BasicDataSource
并调用getConnection()
当您完成连接时调用conn.close()
。 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: 为了向您展示会话应该有多短暂,我将引用文档:
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. 会话的生命周期受逻辑事务的开始和结束的限制。 (Long transactions might span several database transactions.) (长事务可能跨越多个数据库事务。)
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. 您应该执行几个数据库请求(如带有查询和更新的单个Web请求),然后关闭会话,以便可以将基础数据库连接返回到池中。
That maybe the structure of the JAVA Model is changed,but the DB table update incorrect! 也许JAVA模型的结构发生了变化,但DB表更新不正确! what make the Model and the DB table are not correspondence completely. 是什么让Model和DB表完全不对应。 You can do like this: 你可以这样做:
Recently I encountered this error in SpringBoot project.最近在SpringBoot项目中遇到了这个错误。
Reason: Entity class and their respective data in Table had mismatched types.原因:表中的实体 class 及其各自的数据类型不匹配。 In my case one of the fields which was LocalDate type had value "0000-00-00".在我的例子中,LocalDate 类型的字段之一的值为“0000-00-00”。
Solution:解决方案:
In my case, I was getting exactly same error when using native Update query.就我而言,在使用本机更新查询时,我遇到了完全相同的错误。
I resolved this error by using @Modifying (pkg: org.springframework.data.jpa.repository.Modifying) as well as @Transactional annotation (pkg: javax.transaction.Transactional) in repository method.我通过在存储库方法中使用@Modifying (pkg: org.springframework.data.jpa.repository.Modifying) 以及@Transactional 注释(pkg: javax.transaction.Transactional) 解决了这个错误。 If we are using Insert, Update, Delete query then @Modifying is required in addition to @Transactional.如果我们使用插入、更新、删除查询,那么除了@Transactional 之外还需要@Modifying。
@Modifying
@Transactional
@Query(value="Update TBL_EMPLOYEE SET STATUS = :status where ID = :id", nativeQuery = true)
void updateEmployee(@Param("status") String status, @Param("id") String id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.