File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes question about using localthread in hibernate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "question about using localthread in hibernate?" Watch "question about using localthread in hibernate?" New topic
Author

question about using localthread in hibernate?

Robert Strong
Ranch Hand

Joined: Sep 10, 2002
Posts: 84
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about using localthread in hibernate?
 
Similar Threads
struts1.3 with hibernate
Multiple Database Connections with Hibernate 3
First hibernate experiement..got questions...
ThreadLocal Type Safety "Generics"
Session is closed