1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public class HibernateUtil {
private static final SessionFactory sessionFactoryProd;
static {
try {
URL configFileURL1 = HibernateUtil.class
.getResource("hibernateProd.cfg.xml");
sessionFactoryProd = new AnnotationConfiguration().configure(configFileURL1)
.buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: "
+ ex.getMessage(), ex);
}
}
public static final ThreadLocal sessionProd = new ThreadLocal();
public static Session getSessionProd() throws HibernateException {
Session s = (Session) sessionProd.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
System.out.println("Ouverture de session");
s = sessionFactoryProd.openSession();
sessionProd.set(s);
}
return s;
}
public static void closeSessionProd() throws HibernateException {
Session s = (Session) sessionProd.get();
sessionProd.set(null);
if (s != null) {
s.close();
}
}
} |