Hi!
I'm looking into an old application that uses methods like the following when saving objects to the database.
Code:
public void saveMyObject(MyObject myObject) {
Session session = sessionFactory.openSession();
try{
session.saveOrUpdate("MyObject", myObject);
} finally {
session.flush();
session.close();
}
}
When looking at it I started wondering, what if session.flush() throws an exception? Then close will never be called and the connection remains open?
Or does flush() handle closing the session in case of an exception?
Would it be better to do the flush in a try-catch-ignore kind of clause?
Fetching objects are done like:
Code:
public Collection getSomeOtherObjects() {
Session session = sessionFactory.openSession();
try{
Query listQ = session.createQuery("from MyOtherObject as MyOtherObject");
List items = listQ.list();
return items;
} finally {
session.flush();
session.close();
}
}
Is the flush really needed there?
Best,
E