View Javadoc

1   package hu.elte.tribus.sf;
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   * @deprecated it was very nice but it is gone an we are not missing it at all :)))
13   */
14  public class HibernateSessionFactory {
15  
16      /*** 
17       * Location of hibernate.cfg.xml file.
18       * Location should be on the classpath as Hibernate uses  
19       * #resourceAsStream style lookup for its configuration file. 
20       * The default classpath location of the hibernate config file is 
21       * in the default package. Use #setConfigFile() to update 
22       * the location of the configuration file for the current session.   
23       */
24      private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
25  	private static final ThreadLocal threadLocal = new ThreadLocal();
26      private  static Configuration configuration = new Configuration();
27      private static org.hibernate.SessionFactory sessionFactory;
28      private static String configFile = CONFIG_FILE_LOCATION;
29  
30      private HibernateSessionFactory() {
31      }
32  	
33  	/***
34       * Returns the ThreadLocal Session instance.  Lazy initialize
35       * the <code>SessionFactory</code> if needed.
36       *
37       *  @return Session
38       *  @throws HibernateException
39       */
40      public static Session getSession() throws HibernateException {
41          Session session = (Session) threadLocal.get();
42  
43  		if (session == null || !session.isOpen()) {
44  			if (sessionFactory == null) {
45  				rebuildSessionFactory();
46  			}
47  			session = (sessionFactory != null) ? sessionFactory.openSession()
48  					: null;
49  			threadLocal.set(session);
50  		}
51  
52          return session;
53      }
54  
55  	/***
56       *  Rebuild hibernate session factory
57       *
58       */
59  	public static void rebuildSessionFactory() {
60  		try {
61  			configuration.configure(configFile);
62  			sessionFactory = configuration.buildSessionFactory();
63  		} catch (Exception e) {
64  			System.err
65  					.println("%%%% Error Creating SessionFactory %%%%");
66  			e.printStackTrace();
67  		}
68  	}
69  
70  	/***
71       *  Close the single hibernate session instance.
72       *
73       *  @throws HibernateException
74       */
75      public static void closeSession() throws HibernateException {
76          Session session = (Session) threadLocal.get();
77          threadLocal.set(null);
78  
79          if (session != null) {
80              session.close();
81          }
82      }
83  
84  	/***
85       *  return session factory
86       *
87       */
88  	public static org.hibernate.SessionFactory getSessionFactory() {
89  		return sessionFactory;
90  	}
91  
92  	/***
93       *  return session factory
94       *
95       *	session factory will be rebuilded in the next call
96       */
97  	public static void setConfigFile(String configFile) {
98  		HibernateSessionFactory.configFile = configFile;
99  		sessionFactory = null;
100 	}
101 
102 	/***
103      *  return hibernate configuration
104      *
105      */
106 	public static Configuration getConfiguration() {
107 		return configuration;
108 	}
109 
110 }