-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Using Query.list() will close connection?
PostPosted: Fri Mar 10, 2006 2:16 am 
Newbie

Joined: Wed Dec 14, 2005 10:47 pm
Posts: 11
I am trying to do a summation computation with results retrieved from the database and storing that result back in the database on a different table. I am not using any transaction to handle my connections and am manually setting the session and connection properties. Below are the summary of the codes for that method:

Code:
if (session != null) {
  List l = session.getNamedQuery("GetData").list();
  Iterator iter = l.iteration();
  int summation = 0;

  while (iter.hasNext())  {
    // do summation
  }

  Connection conn = null;
  PreparedStatement preStmt = null;
  try  {
    session.setAutoFlush(Flush.NEVER);
    conn = session.connection();
    conn.setAutoCommit(false);

    preStmt = conn.prepareStatement(session.getNamedQuery("InsertData").getQueryString());
    preStmt.setInt(1, summation);

    if (preStmt.executeUpdate() > 0)  {
      session.flush();
      conn.commit();
      return true;
    }
    else  {
      conn.rollback();
      session.clear();
      return false;
    }
  }
  catch (Exception e)   {
    if (conn != null)
      conn.rollback();

    return false;
  }
  finally  {
    session.close();

    if (conn != null)  {
      conn.close();
      conn = null;
    }

    if (preStmt != null)  {
      preStmt.close();
      preStmt = null;
    }
  }

}


I get an SQLException thrown at the line "preStmt.setInt(1, summation);", saying the prepared statement has been closed. I did not specify any close() methods prior to that line and am curious if Hibernate automatically closes the connection about executing a SELECT query or a query.list(). Anybody knows how Hibernate works with Query.list()?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.