Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2058 | es | 1 | package pt.estgp.es.exemplos.hibernate; |
2 | |||
3 | |||
4 | import org.apache.log4j.Logger; |
||
5 | import org.hibernate.Session; |
||
6 | import org.hibernate.SessionFactory; |
||
7 | import org.hibernate.cfg.Configuration; |
||
8 | |||
9 | public class HibernateUtils { |
||
10 | |||
11 | // private static Session session; |
||
12 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(HibernateUtils.class); |
||
13 | private static SessionFactory sessionFactory; |
||
14 | private static SessionFactory migrationSessionFactory; |
||
15 | |||
16 | static |
||
17 | { |
||
18 | try |
||
19 | { |
||
20 | createFactory(); |
||
21 | |||
22 | // session = sessionFactory.openSession(); |
||
23 | } |
||
24 | catch (1.5.0/docs/api/java/lang/Throwable.html">Throwable e) |
||
25 | { |
||
26 | logger.fatal(e,e); |
||
27 | throw new 1.5.0/docs/api/java/lang/ExceptionInInitializerError.html">ExceptionInInitializerError(e); |
||
28 | } |
||
29 | } |
||
30 | |||
31 | private static void createFactory() |
||
32 | { |
||
33 | 1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration config = new 1.5.0/docs/api/javax/security/auth/login/Configuration.html">Configuration(); |
||
34 | config.configure(1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader().getResource("pt/estgp/es/exemplos/hibernate/hibernate.cfg.xml")); |
||
35 | //config.setInterceptor(new DirtyCheckInterceptor()); |
||
36 | sessionFactory = config.buildSessionFactory(); |
||
37 | |||
38 | } |
||
39 | |||
40 | |||
41 | |||
42 | public static SessionFactory getSessionFactory() |
||
43 | { |
||
44 | if(sessionFactory.isClosed()) |
||
45 | createFactory(); |
||
46 | return sessionFactory; |
||
47 | } |
||
48 | |||
49 | public static synchronized Session openSession() |
||
50 | { |
||
51 | // if(!session.isOpen()) |
||
52 | // return sessionFactory.openSession(); |
||
53 | // return session; |
||
54 | return getCurrentSession(); |
||
55 | } |
||
56 | public static synchronized Session getCurrentSession() throws 1.5.0/docs/api/java/lang/IllegalStateException.html">IllegalStateException |
||
57 | { |
||
58 | // if(!examples.isOpen()) |
||
59 | // examples = sessionFactory.openSession(); |
||
60 | // return examples; |
||
61 | return getSessionFactory().getCurrentSession(); |
||
62 | } |
||
63 | |||
64 | } |