-->
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.  [ 6 posts ] 
Author Message
 Post subject: Thread Local Session pattern
PostPosted: Thu Jun 30, 2005 4:36 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:14 pm
Posts: 25
Hi, Developers,

I am reading Hibernate in Action book. page 300-304.
in this part, the book is about thread local seesion pattern.
From HibernateUtil code:

private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();

looks like all request will share the same seesion.
This is what I am confuesed, if 2 different users place bid at the same time, they will use the same threadSession, right?

So if my thought is right, The problem will be here.

From void dofilter() { // (page 304)
.......
......
finally {
HibernateUtil.closeSession();
}


}
After the first user rendered the view, the session will be closed. then the second user has not rendered the view, the newbid.getBidder().getItems().iterator() is unfetched at this point because of items association is lazy.


How to fix this issue?

if ThreadLocal threadSession is not static, so the different will have different session? right.

Thank you much for your help!

Edgy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 4:40 pm 
Regular
Regular

Joined: Tue Mar 22, 2005 2:27 am
Posts: 62
ThreadLocals explained:
http://www-128.ibm.com/developerworks/j ... eads3.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 4:02 pm 
Beginner
Beginner

Joined: Thu Jun 30, 2005 4:14 pm
Posts: 25
Thanks, Frankgrimes,

Sorry I am not sure your name.
I am still confused.
From this code, because threadSession is static, so all threads will share the same session. I don't know where I am worng. Please help me, Thank you.

Edgy

public class HibernateUtil {
...
private static final ThreadLocal threadSession = new ThreadLocal();
..

public static Session getSession()
throws InfrastructureException {
Session s = (Session) threadSession.get();
try { if (s == null) {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
}
return s;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 05, 2005 4:23 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No they won't. You obviously haven't read the link Frank provided.


Top
 Profile  
 
 Post subject: Re: Thread Local Session pattern
PostPosted: Tue May 14, 2013 2:33 pm 
Newbie

Joined: Tue May 14, 2013 2:17 pm
Posts: 1
private static final ThreadLocal threadSession = new ThreadLocal();
..

public static Session getSession()
throws InfrastructureException {
Session s = (Session) threadSession.get();
try { if (s == null) {
s = getSessionFactory().openSession();
}
threadSession.set(s);
}
}
return s;
}

threadSession is a ThreadLocal, ThreadLocal has a feature of store value in the thread which r u accessing means

From Thread1 I am calling like this

run(){
Util.getSession();
}

Here Thread1 is calling the getSession(), First it will call the threadSession.get() means nothing but in the Thread1's threadlocalmap is containing session or not, if it is not contain session it will give null, then we are creating the new session, the created session we are storing Thread1's threadlocalmap.

In your case Thread1 and Thread2 are calling getsession means each Thread1 and Thread2 have their session only when their calling the getsession().


Top
 Profile  
 
 Post subject: Re: Thread Local Session pattern
PostPosted: Thu May 16, 2013 7:59 am 
Newbie

Joined: Sun Oct 02, 2005 12:49 am
Posts: 15
Alternatively, the two users hit the server starting two separate threads of execution.


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