-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Guru: How to do >1 sessions-per-application-transaction?
PostPosted: Wed Oct 05, 2005 11:54 pm 
Beginner
Beginner

Joined: Wed Oct 05, 2005 12:29 pm
Posts: 22
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 4:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Have you seen how Seam does this? You could copy the Seam code, or simply use Seam.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 10:01 pm 
Beginner
Beginner

Joined: Wed Oct 05, 2005 12:29 pm
Posts: 22
I took a look at Seam briefly. Will take a close look later. My first impression is that it is overwhelmingly Ejb3, as least as far as the doc is concern. Maybe it would be better marketing to make the whole package more Hibernate/POJO friendly for those of us (majority I suspect) who is not ready to consider Ejb3 yet.

Anyway I am back to using the HibernateUtil from the original CaveatEmptor. It allows me much more explicit control. I have two versions of the HibernateUtil class, and it is completely trivial to store two hibernate sessions in HttpSession, and attach them back to the right version of HiernateUtil.

The system that I am designing is to operate in asp mode - i.e. same program serving multiple companies, each with its own database.

I am working on another version of HibernateUtil that stores a Map of SessionFactories indexed by companyId. The current companyId is stored in its own ThreadLocal, and a session is reattached together with a companyID specified. That will allow the rest of the codes to be completely transparent of the change in company/database context. I think the idea should work. I am doing POC at present.

I think there is too much abstraction in CaveatEmptor3.1alpah3. It is beautiful in a way, but also makes it difficult to work with.

Regards,
Yee


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.