-->
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.  [ 7 posts ] 
Author Message
 Post subject: session lost between two thread , bad design?
PostPosted: Tue Feb 09, 2010 6:57 am 
Newbie

Joined: Thu May 14, 2009 9:13 am
Posts: 3
Hi,
I try to work on a legacy application and turn it into a hibernate+jpa driven project from a pure jdbc one. But I have some questions right now,

i use hibernate.current_session_context_class=thread as session manager

but the session goes out between two thread, and I can't not pass bean between then. One solution i can think is that i detach all objects when 1st thread end and reload them in 2nd thread. but i wonder if there is a better solution.

Or in general, shall i change the design a bit to avoid cross thread boundaries with state?

Any ideas will be grateful!
Thx!


Top
 Profile  
 
 Post subject: Re: session lost between two thread , bad design?
PostPosted: Wed Feb 17, 2010 5:46 am 
Newbie

Joined: Thu May 14, 2009 9:13 am
Posts: 3
no one reply?? please..


Top
 Profile  
 
 Post subject: Re: session lost between two thread , bad design?
PostPosted: Wed Feb 17, 2010 6:24 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Or in general, shall i change the design a bit to avoid cross thread boundaries with state?

I personally prefer to use
Code:
sf.openSession(); // or  emf.getEntityManager() with JPA

instead to
Code:
sf.getCurrentSession();

which corresponds to manual persistence-propagation.
Obviously you then have to pass the returned session between the threads,
I don't understand if you can do this and if it is what you mean with 'cannot pass bean between then'.
Quote:
I can't not pass bean between then.


There are different ways to implement Conversations with Hibernate
(also depending if you use JPA or not, Java SE or J2EE container, etc.).
The book 'Java Persistence with Hibernate' spends a whole chapter (11.2) in this topic,
so it is hard to explain solutions here in few lines.


Top
 Profile  
 
 Post subject: Re: session lost between two thread , bad design?
PostPosted: Wed Feb 17, 2010 7:09 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
I am confused between sf.openSession() and sf.getCurrentSession().

If we use sf.openSession() within the same thread twice, do we get two different sessions with in that thread?
If it returns the same session within the same thread for any number of calls, how hibernate keep track of the session it supplied initially?

I know in sf.getCurrentSession, first time it binds the session to current thread, so every subsequent request with in that thread will return the same.

Thanks in advance


Top
 Profile  
 
 Post subject: Re: session lost between two thread , bad design?
PostPosted: Wed Feb 17, 2010 8:52 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
If we use sf.openSession() within the same thread twice, do we get two different sessions with in that thread?


YES
It is you job to save the returned session into a variable and use it until your unit of work terminates.


Top
 Profile  
 
 Post subject: Re: session lost between two thread , bad design?
PostPosted: Wed Feb 17, 2010 8:55 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Correction:
It is your job to keep the returned session into a variable, use this variable and close the session explicitly once you not need it anymore.


Top
 Profile  
 
 Post subject: Re: session lost between two thread , bad design?
PostPosted: Wed Feb 17, 2010 9:49 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:02 am
Posts: 48
Thanks, it clears my confusion


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.