lucky_start_izumi lucky_start_izumi - 1 year ago 199
Java Question

org.hibernate.exception.GenericJDBCException: could not execute query

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(
at org.hibernate.exception.SQLStateConverter.convert(
at org.hibernate.exception.JDBCExceptionHelper.convert(
at org.hibernate.loader.Loader.doList(
at org.hibernate.loader.Loader.listIgnoreQueryCache(
at org.hibernate.loader.Loader.list(
at org.hibernate.loader.hql.QueryLoader.list(
at org.hibernate.hql.ast.QueryTranslatorImpl.list(
at org.hibernate.engine.query.HQLQueryPlan.performList(
at org.hibernate.impl.SessionImpl.list(
at org.hibernate.impl.QueryImpl.list(
at com.myCompany.Query.myMethod(
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
at java.lang.reflect.Method.invoke(
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(
at com.sun.jersey.spi.container.servlet.WebComponent.service(
at com.sun.jersey.spi.container.servlet.ServletContainer.service(
at com.sun.jersey.spi.container.servlet.ServletContainer.service(
at javax.servlet.http.HttpServlet.service(
at org.ebayopensource.ginger.server.internal.container.MessageProcessor.processRequest(
at org.ebayopensource.ginger.server.container.GingerServletShell.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.valves.AccessLogValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.Http11Processor.process(
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(
Caused by: java.sql.SQLException: Closed Connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(
at oracle.jdbc.driver.DatabaseError.throwSqlException(
at oracle.jdbc.driver.DatabaseError.throwSqlException(
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(
at oracle.jdbc.driver.PhysicalConnection.prepareStatement(
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(
at org.hibernate.loader.Loader.prepareQueryStatement(
at org.hibernate.loader.Loader.doQuery(
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(
at org.hibernate.loader.Loader.doList(

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

Thank you very much:)

Answer Source

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.setValidationQuery("SELECT 1"); // this is database specific
ds.setTestWhileIdle(true); // test the connections every so often
while (!shutdown) {
    Connection conn = dataSource.getConnection();
    Statement stmt = conn.createStatement();
    // this returns the connection back to the pool instead of really closing
    // the connection

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:

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download