1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//On stockera la session courante dans le Thread
public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
public static Session currentSession() throws HibernateException {
Session s = session.get();
// Ouvre une nouvelle Session, si ce Thread n'en a aucune
if (s == null || !s.isOpen()) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = session.get();
session.set(null);
if (s != null)
s.close();
} |
Partager