View Javadoc

1   package uk.co.concise.maven.hdc.dao;
2   
3   import java.util.Properties;
4   
5   import net.sf.hibernate.HibernateException;
6   import net.sf.hibernate.MappingException;
7   import net.sf.hibernate.Session;
8   import net.sf.hibernate.SessionFactory;
9   import net.sf.hibernate.Transaction;
10  import net.sf.hibernate.cfg.Configuration;
11  import uk.co.concise.maven.hdc.model.Chart;
12  import uk.co.concise.maven.hdc.model.Label;
13  import uk.co.concise.maven.hdc.model.Point;
14  
15  /***
16   * Super class for all our data accessor objects.
17   * @author martenssonb
18   */
19  public abstract class HibernateUtil {
20  
21      private static SessionFactory sessionFactory;
22      
23      /***
24       * Configures hibernate programmatically. 
25       * This method initialises the session factory exactly once.
26       * @param dbDriverClass the JDBC driver used for persistance.
27       * @param dbUrl the URL to the database.
28       * @param dbUser the user used to login to the database.
29       * @param dbPass the password used to login to the database.
30       * @param dbHibernateDialect the database dialect that Hibernate will use.
31       */
32      public static void configure(String dbDriverClass,
33              String dbUrl, String dbUser, String dbPass,
34              String dbHibernateDialect) {
35          if (sessionFactory == null) {
36              Configuration conf = programmaticConf(dbDriverClass, dbUrl, dbUser,
37                      dbPass, dbHibernateDialect);
38              try {
39                  sessionFactory = conf.buildSessionFactory();
40              } catch (HibernateException e) {
41                  throw new RuntimeException(e);
42              }
43          }
44      }
45  
46      private static Configuration programmaticConf(String dbDriverClass,
47              String dbUrl, String dbUser, String dbPass,
48              String dbHibernateDialect) {
49          Configuration conf = null;
50          try {
51              Properties props = new Properties();
52              props.put("hibernate.connection.driver_class",dbDriverClass);
53              props.put("hibernate.connection.url",dbUrl);
54              props.put("hibernate.connection.username",dbUser);
55              props.put("hibernate.connection.password",dbPass);
56              props.put("show_sql","false");
57              props.put("hibernate.dialect",dbHibernateDialect);
58              props.put("hibernate.transaction.factory_class",
59                      "net.sf.hibernate.transaction.JDBCTransactionFactory");
60              props.put("hibernate.cache.provider_class",
61                      "net.sf.hibernate.cache.HashtableCacheProvider");
62              props.put("hibernate.hbm2ddl.auto","update");
63  
64              conf = new Configuration().addInputStream(
65                      Chart.class.getResourceAsStream("Chart.hbm.xml"))
66                      .addInputStream(
67                              Label.class.getResourceAsStream("Label.hbm.xml"))
68                      .addInputStream(
69                              Point.class.getResourceAsStream("Point.hbm.xml"))
70                      .setProperties(props);
71          } catch (MappingException e) {
72              throw new RuntimeException(e);
73          }
74          return conf;
75      }
76      
77      public static final ThreadLocal session = new ThreadLocal();
78  
79      /***
80       * Returns a session for the calling thread. Creates a session if none
81       * exists.
82       * @return a session for the calling thread.
83       */
84      public static Session currentSession() {
85          Session s = (Session) session.get();
86          // Open a new Session, if this Thread has none yet
87          if (s == null) {
88              if (sessionFactory == null) {
89                  throw new RuntimeException(
90                          "SessionFactory has to be initialized.");
91              }
92              try {
93                  s = sessionFactory.openSession();
94              } catch (HibernateException e) {
95                  throw new RuntimeException(e);
96              }
97              session.set(s);
98          }
99          return s;
100     }
101 
102     /***
103      * Closes the session for the running thread.
104      */
105     public static void closeSession() {
106         Session s = (Session) session.get();
107         session.set(null);
108         if (s != null) {
109             try {
110                 s.close();
111             } catch (HibernateException e) {
112                 throw new RuntimeException(e);
113             }
114         }
115     }
116 
117     /***
118      * Handles if there is a hibernate exception during a transaction.
119      * @param tx the transaction.
120      * @param e the hibernate exception.
121      */
122     public static void handleHibernateException(Transaction tx,
123             HibernateException e) {
124         if (tx != null) {
125             try {
126                 tx.rollback();
127             } catch (HibernateException e1) {
128                 // Do nothing since we're already handling an exception.
129             }
130         }
131         throw new RuntimeException(e);
132     }
133     
134 }