1 | package com.medias.utils.hibernate; |
---|
2 | |
---|
3 | import org.hibernate.HibernateException; |
---|
4 | import org.hibernate.Session; |
---|
5 | import org.hibernate.SessionFactory; |
---|
6 | import org.hibernate.cfg.Configuration; |
---|
7 | |
---|
8 | |
---|
9 | /** |
---|
10 | * Configures and provides access to Hibernate sessions, tied to the |
---|
11 | * current thread of execution. Follows the Thread Local Session |
---|
12 | * pattern, see {@link http://hibernate.org/42.html}. |
---|
13 | */ |
---|
14 | public class SessionManager { |
---|
15 | |
---|
16 | /** |
---|
17 | * Location of hibernate.cfg.xml file. |
---|
18 | * NOTICE: Location should be on the classpath as Hibernate uses |
---|
19 | * #resourceAsStream style lookup for its configuration file. That |
---|
20 | * is place the config file in a Java package - the default location |
---|
21 | * is the default Java package.<br><br> |
---|
22 | * Examples: <br> |
---|
23 | * <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml". |
---|
24 | * CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code> |
---|
25 | */ |
---|
26 | private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; |
---|
27 | |
---|
28 | /** Holds a single instance of Session */ |
---|
29 | private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>(); |
---|
30 | |
---|
31 | /** The single instance of hibernate configuration */ |
---|
32 | private static final Configuration cfg = new Configuration(); |
---|
33 | |
---|
34 | /** The single instance of hibernate SessionFactory */ |
---|
35 | private static SessionFactory sessionFactory; |
---|
36 | |
---|
37 | /** |
---|
38 | * Returns the ThreadLocal Session instance. Lazy initialize |
---|
39 | * the <code>SessionFactory</code> if needed. |
---|
40 | * |
---|
41 | * @return Session |
---|
42 | * @throws HibernateException |
---|
43 | */ |
---|
44 | public static Session currentSession() throws HibernateException { |
---|
45 | Session session = (Session) threadLocal.get(); |
---|
46 | |
---|
47 | if (session == null) { |
---|
48 | if (sessionFactory == null) { |
---|
49 | try { |
---|
50 | cfg.configure(CONFIG_FILE_LOCATION); |
---|
51 | sessionFactory = cfg.buildSessionFactory(); |
---|
52 | } |
---|
53 | catch (Exception e) { |
---|
54 | System.err.println("%%%% Error Creating SessionFactory %%%%"); |
---|
55 | e.printStackTrace(); |
---|
56 | } |
---|
57 | } |
---|
58 | session = sessionFactory.openSession(); |
---|
59 | threadLocal.set(session); |
---|
60 | } |
---|
61 | |
---|
62 | return session; |
---|
63 | } |
---|
64 | |
---|
65 | /** |
---|
66 | * Close the single hibernate session instance. |
---|
67 | * |
---|
68 | * @throws HibernateException |
---|
69 | */ |
---|
70 | public static void closeSession() throws HibernateException { |
---|
71 | Session session = (Session) threadLocal.get(); |
---|
72 | threadLocal.set(null); |
---|
73 | |
---|
74 | if (session != null) { |
---|
75 | session.close(); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Default constructor. |
---|
81 | */ |
---|
82 | private SessionManager() { |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|