• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question about using localthread in hibernate?

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, I've been told to use threadlocal to store a hibernate session used in a stateless session bean, like below:

public class HibernateSessionFactory {

public static final ThreadLocal session = new ThreadLocal();
private static SessionFactory sessionFactory;

public static void init() throws HibernateException {
sessionFactory = new Configuration().configure().buildSessionFactory();
}

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

if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

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

session.set(null);
if (s != null)
s.close();
}

the thing I don't understand is that hibernate session is stored in ThreadLocal using Singleton, so all the threads can access to the static variable, how could the hibernate session object be stored in the local thread of a stateless session bean respectively?

any help appreciated
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic