01: package auction.persistence;
02:
03: import org.hibernate.*;
04: import org.hibernate.cfg.*;
05: import org.apache.commons.logging.*;
06:
07: import javax.naming.*;
08:
09: public class HibernateUtil {
10:
11: private static Configuration configuration;
12: private static SessionFactory sessionFactory;
13:
14: static {
15: try {
16: configuration = new Configuration();
17: configuration.configure();
18: rebuildSessionFactory(configuration);
19: } catch (Throwable ex) {
20: throw new ExceptionInInitializerError(ex);
21: }
22: }
23:
24: public static SessionFactory getSessionFactory() {
25: String sfName = configuration
26: .getProperty(Environment.SESSION_FACTORY_NAME);
27: if (sfName != null) {
28: try {
29: return (SessionFactory) new InitialContext()
30: .lookup(sfName);
31: } catch (NamingException ex) {
32: throw new RuntimeException(ex);
33: }
34: } else if (sessionFactory == null) {
35: rebuildSessionFactory();
36: }
37: return sessionFactory;
38: }
39:
40: public static void rebuildSessionFactory() {
41: rebuildSessionFactory(configuration);
42: }
43:
44: public static void rebuildSessionFactory(Configuration cfg) {
45: if (sessionFactory != null && !sessionFactory.isClosed())
46: sessionFactory.close();
47: if (cfg.getProperty(Environment.SESSION_FACTORY_NAME) != null) {
48: cfg.buildSessionFactory();
49: } else {
50: sessionFactory = cfg.buildSessionFactory();
51: }
52: configuration = cfg;
53: }
54:
55: }
|