Hi,
I am designing a web application that requires opening more than 1 databases. I have to use the session-per-application-transaction pattern and I am wondering how to save more than 1 sessions in HttpSession and access them via the ThreadLocal.
Referring to the following snippets from CaveatEmptor 3.1alpha 3, the HibernateFilterLong class:
Code:
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Try to get a Hibernate Session from the HttpSession
HttpSession userSession =
((HttpServletRequest) request).getSession();
Session hibernateSession =
(Session) userSession.getAttribute(HTTPSESSIONKEY);
if (hibernateSession != null) {
log.debug("Reconnecting stored Session.");
ThreadLocalSessionContext.bind(hibernateSession);
}
And the HibernateUtil class:
Code:
protected Session getCurrentSession() {
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
currentSession.beginTransaction();
return HibernateUtil.getSessionFactory().getCurrentSession();
}
----------------------------------------------------------------
There are two problems. Firstly the sesison pull out from HttpSession and is bound to the ThreadLocalSessionContext object - so how to specify the the program is using more than one ThreadLocalSessionContext?
And secondly, SessionFactory.getCurrentSession() obtains the session I presumed is bound to ThreadLocalSessionContext. If there is more than one ThreadLocalSessionContext, how to get SessionFactory to use the right one?
BTW - i think the CaveartEmptor 3.1alpha 3 is quite an eye opener.
Many thanks in advance.
Yee