Hi Chrisjan,
Thanks for answering my post. I'm using the example given in the Hibernate docs. I posted it below, but you can also see it here:
http://www.hibernate.org/hib_docs/refer ... ngwithcats
Also, I've used this class before, except that I set sessionFactory to a JNDI data source from JBoss. It worked fine. If I do like you suggest by declaring and setting sessionFactory all at once, then the other procedures won't have access to it.
I can't try it until tomorrow anyway because I'm no longer at work.
Itchy
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
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();
}
}