• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Importance of ThreadLocal in hibernate

 
liliane fahmy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please I want to know the importance of ThreadLocal in generaly and also in this following code ,
what 's the purpose of getting session by get() of ThreadLocal ?



public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();

Please reply me
Thanks you
 
jatan bhavsar
Ranch Hand
Posts: 299
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Liliane,

Threadlocal is used to create session for everythread.It will make sure that everyuser accessing web page will get its own session. So here threadlocal is used basically to provide different session to different thread.

Regards
Jatan
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a note you should most likely not be writing code like that in your application anyway. Hibernate comes with a ThreadLocalSessionContext where sessions are tracked by thread of execution. This should be used for that use case rather than attempting to roll your own. This can be used by simply setting a hibernate property. Please see the reference documentation for more details.

http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#architecture-current-session
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic