I'm using Struts and the Thread Local Session pattern...
In the begin of the request I open a Transaction, and after in the DAO classes I retrieve the session,
but I never flush or close the session in the DAO...
Then, in the end of request I commit or rollback the Transaction and close the Session.
Is This the right way ? Can I save/update many tables in the same request with the same Hibernate Session ?
Struts Action
Code:
Session session = null;
Transaction trans = null;
try {
ActionForward forward = null;
session = Hibernate2ThreadLocalSession.currentSession();
// Open Transaction
trans = session.beginTransaction();
// method in the subclass
forward = performAction(mapping, form, request, response);
// COMMIT
trans.commit();
return forward;
}
catch (Exception e) {
if (trans != null) {
trans.rollback();
}
throw e;
}
finally {
try {
// CLOSE SESSION
Hibernate2ThreadLocalSession.closeSession();
}
catch (HibernateException e1) {
e1.printStackTrace();
}
}
DAO:
Code:
void test()
{
session = openSession();
// do something
// don't flush or close the Session here...
}