I am handling some old application code, and there seems to be several concepts involved, so I am looking to make sure I can improve them into one solid and strict practice.
Basically, the whole code is wrapped with a HibernateSessionRequestFilter like this
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
sf.getCurrentSession().beginTransaction();
chain.doFilter(request, response);
sf.getCurrentSession().clear();
} catch (...) {
//...
} finally {
sf.getCurrentSession().close();
}
}
Then, there is an interceptor, doing something like this
private String loadStaff(...) {
//...
try {
dbSession = //...;
dbSession.beginTransaction();
// some logic
dbSession.getTransaction().rollback();
} catch (RuntimeException e) {
//..
}
finally {
if (dbSession != null && dbSession.isOpen()) {
dbSession.clear();
}
}
}
And then there is even more business logic code with some more begintransactions and session clear etc.
So, questions:
- What happens when beginTransaction called more than once on same session?
- Sometimes, calling "clear()" throws exception, I think it happens after a "rollback()" was called. How to fix that?
- In general, what is the best proposed practice to combine session.clear() with transaction begin/rollback/commit ?
Thank you