source: ether_eccad/trunk/ECCAD_INTERFACE/WEB-INF/src/org/medias/eccad/persistance/hibernate/HibernateUtil.java @ 68

Last change on this file since 68 was 68, checked in by cbipsl, 14 years ago

commit v1 eccad

  • Property svn:executable set to *
File size: 1.5 KB
Line 
1package org.medias.eccad.persistance.hibernate;
2
3
4import org.hibernate.*;
5import org.hibernate.cfg.*;
6import org.medias.eccad.helpers.LoggerPerso;
7
8/**
9 * Classe permettant la manipulation d'hibernate
10 * @author Jean PINAUD
11 *
12 */
13public class HibernateUtil {
14
15 private static SessionFactory sessionFactory = null;
16
17 public static ThreadLocal<Session> session = new ThreadLocal<Session>();
18
19 /***
20  * Retourne la session courante ou une nouvelle session si le thread n'en a aucune
21  * @return la session
22  * @throws HibernateException
23  */
24 public static Session currentSession()
25                throws HibernateException {
26         LoggerPerso.log(HibernateUtil.class, LoggerPerso.DEBUG, "--  connexion  --");
27  try {
28           if (sessionFactory == null) {
29                   sessionFactory = new Configuration().configure().buildSessionFactory();
30                   }
31                } catch (HibernateException ex) {
32                   throw new RuntimeException("Problème de configuration : "
33                   + ex.getMessage(), ex);
34                }
35   
36                Session s = (Session) session.get();
37   
38                // Ouvre une nouvelle Session, si ce Thread n'en a aucune
39   if (s == null) {
40           s = sessionFactory.openSession();
41           session.set(s);     
42   }
43   return s;
44   }
45
46 /**
47  * Ferme la session courante
48  * @throws HibernateException
49  */
50 public static void closeSession()
51                throws HibernateException {
52         LoggerPerso.log(HibernateUtil.class, LoggerPerso.DEBUG, "--  deconnexion  --");
53   Session s = (Session) session.get();
54   session.set(null);
55   if (s != null)
56           s.close();
57   }
58 
59
60 }
Note: See TracBrowser for help on using the repository browser.