1 package util;
2
3 import org.hibernate.HibernateException;
4 import org.hibernate.Session;
5 import org.hibernate.cfg.Configuration;
6
7 /**
8 * Configures and provides access to Hibernate sessions, tied to the
9 * current thread of execution. Follows the Thread Local Session
10 * pattern, see {@link http://hibernate.org/42.html }.
11 */
12 public class HibernateUtil {
13
14 /**
15 * Location of hibernate.cfg.xml file.
16 * Location should be on the classpath as Hibernate uses
17 * #resourceAsStream style lookup for its configuration file.
18 * The default classpath location of the hibernate config file is
19 * in the default package. Use #setConfigFile() to update
20 * the location of the configuration file for the current session.
21 */
22 private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
23 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
24 private static Configuration configuration = new Configuration();
25 private static org.hibernate.SessionFactory sessionFactory;
26 private static String configFile = CONFIG_FILE_LOCATION;
27
28 static {
29 try {
30 configuration.configure(configFile);
31 sessionFactory = configuration.buildSessionFactory();
32 } catch (Exception e) {
33 System.err
34 .println("%%%% Error Creating SessionFactory %%%%");
35 e.printStackTrace();
36 }
37 }
38 private HibernateUtil() {
39 }
40
41 /**
42 * Returns the ThreadLocal Session instance. Lazy initialize
43 * the <code>SessionFactory</code> if needed.
44 *
45 * @return Session
46 * @throws HibernateException
47 */
48 public static Session getSession() throws HibernateException {
49 Session session = (Session) threadLocal.get();
50
51 if (session == null || !session.isOpen()) {
52 if (sessionFactory == null) {
53 rebuildSessionFactory();
54 }
55 session = (sessionFactory != null) ? sessionFactory.openSession()
56 : null;
57 threadLocal.set(session);
58 }
59
60 return session;
61 }
62
63 /**
64 * Rebuild hibernate session factory
65 *
66 */
67 public static void rebuildSessionFactory() {
68 try {
69 configuration.configure(configFile);
70 sessionFactory = configuration.buildSessionFactory();
71 } catch (Exception e) {
72 System.err
73 .println("%%%% Error Creating SessionFactory %%%%");
74 e.printStackTrace();
75 }
76 }
77
78 /**
79 * Close the single hibernate session instance.
80 *
81 * @throws HibernateException
82 */
83 public static void closeSession() throws HibernateException {
84 Session session = (Session) threadLocal.get();
85 threadLocal.set(null);
86
87 if (session != null) {
88 session.close();
89 }
90 }
91
92 /**
93 * return session factory
94 *
95 */
96 public static org.hibernate.SessionFactory getSessionFactory() {
97 return sessionFactory;
98 }
99
100 /**
101 * return session factory
102 *
103 * session factory will be rebuilded in the next call
104 */
105 public static void setConfigFile(String configFile) {
106 HibernateUtil.configFile = configFile;
107 sessionFactory = null;
108 }
109
110 /**
111 * return hibernate configuration
112 *
113 */
114 public static Configuration getConfiguration() {
115 return configuration;
116 }
117
118 }