sumit

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSession {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
}
catch (HibernateException e)
{
throw new RuntimeException ("SessionFactory Error - " + e.getMessage(), e);
}
}

public static final ThreadLocal<Session> session = new ThreadLocal<Session>();

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();
}
}

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.