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()?