XFlameBR
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
static {
try {
sessionFactory = new Configuration().configure("pacote/hibernate.cfg.xml").buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Session getSession() {
Session s = (Session) threadSession.get();
try {
if (s == null) {
s = sessionFactory.openSession();
threadSession.set(s);
}
}catch (HibernateException ex) {
throw new InfrastructureException(ex);
}
return s;
}
}