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