-->
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.  [ 5 posts ] 
Author Message
 Post subject: Multiple open sessions and thread local pattern.
PostPosted: Tue Mar 22, 2005 2:27 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
I use the thread local pattern to manage session in my application. But from time to time, from with in a EJB with one active transaction/open session, I have the needto invoke antoher EJB that processes its request in a whole new transaction. Here is the sequence of things that could happen:

1. EJB A ,in Transaction A, opens Session A (associated with threadLocal)
2. as part of processing the request, it loads some persistent object graph
3. It may or maynot make changes to the loaded objects
4. then - invokes EJB - B and passes in the loaded object
5. EJB B, in transaction B, saves the current open session from the threadLocal into a local variable
6. creates a new session B and attachs it to the thread local variable
7. process the request..............................
8. After processing, close the Session B
9. put the original session (Session A) back in the threadLocal and
10. roll back if necessary and return to the caller - EJB A
11. EJB A continues processing the request in Transaction A using SEssion A.

Somewhere while processing the request in the second EJB B, I get
"Illegal attempt to associate a collection with two open sessions".


What am I doing wrong? How do I avoid this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 25, 2005 11:08 am 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
Never mind. I figured it out.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 6:32 pm 
Newbie

Joined: Thu May 19, 2005 5:06 am
Posts: 11
bshaw wrote:
Never mind. I figured it out.


It would have been great if you could tell us what you did wrong. I have the same problem and can't figure it out.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 2:28 pm 
Beginner
Beginner

Joined: Mon Feb 23, 2004 5:11 pm
Posts: 39
Well, a collection simply cannot be associated with 2 open sessions simultaneously. You just have to avoid this from happening. You may want to evict (session.evict) the relevant object (graph) from the first session before passing it to the second session. If need be, after the second session is closed, you could re-attach the object to session 1.

Hope this helps. Please remeber to rate the posting if this is helpful.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 2:07 am 
Newbie

Joined: Thu May 19, 2005 5:06 am
Posts: 11
bshaw wrote:
Well, a collection simply cannot be associated with 2 open sessions simultaneously. You just have to avoid this from happening. You may want to evict (session.evict) the relevant object (graph) from the first session before passing it to the second session. If need be, after the second session is closed, you could re-attach the object to session 1.

Hope this helps. Please remeber to rate the posting if this is helpful.


I figured it out too. :) My problem was caused by me starting a thread and sending in an instance from another session. I solved this by sending the ID to the new thread, opening it in a new session.

Code:
new Runnable() {
    public void run() {
        HibernateUtil.beginTransaction();
        org.hibernate.Query q = HibernateUtil.getSession().createQuery("from CronJob job where job.id = :id");
        q.setLong("id", jobPK);
        ((CronJob) q.list().get(0)).run();
        HibernateUtil.closeSession();
    }
}


Feels a bit quick and dirty, but this was an OK solution for me. I'm sure there are plenty of examples when you don't want to do this. You might have made changes to the instance without committing and need these changes in the new thread. (Perhaps Hibernate take care of that, I don't know.)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.