• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Importance of ThreadLocal in hibernate

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 300
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
No holds barred. And no bars holed. Except this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic