View Javadoc

1   package org.lsst.ccs.config.dao.hibernate;
2   
3   import org.hibernate.HibernateException;
4   import org.hibernate.Session;
5   import org.hibernate.SessionFactory;
6   import org.hibernate.cfg.Configuration;
7   import org.hibernate.service.ServiceRegistry;
8   import org.hibernate.service.ServiceRegistryBuilder;
9   import org.lsst.ccs.config.HqlDAO;
10  
11  import java.util.List;
12  import java.util.logging.Level;
13  
14  import static org.lsst.ccs.config.PackCst.CURLOG;
15  
16  /**
17   * @author bamade
18   */
19  // Date: 12/03/13
20  
21  public class HibernateDAO extends HqlDAO {
22      class SessionDelegate implements AbstractSession {
23          Session session ;
24  
25          SessionDelegate(Session session) {
26              this.session = session;
27          }
28  
29          @Override
30          public void save(Object o) throws HibernateException {
31               session.save(o);
32          }
33  
34          @Override
35          public void saveOrUpdate(Object o) throws HibernateException {
36              session.saveOrUpdate(o);
37          }
38  
39          @Override
40          public void delete(Object o) throws HibernateException {
41              session.delete(o);
42          }
43  
44          @Override
45          public void flush() {
46              session.flush();
47          }
48  
49          @Override
50          public AbstractQuery createQuery(final String s) {
51              return new AbstractQuery() {
52                  org.hibernate.Query query = session.createQuery(s) ;
53                  @Override
54                  public List list() {
55                      return query.list() ;
56                  }
57              } ;
58          }
59      }
60      protected SessionFactory sessionFactory;
61      org.hibernate.Session session ;
62      org.hibernate.Transaction currentTransaction ;
63      private boolean exceptionFired ;
64  
65      public HibernateDAO() {
66          initSession() ;
67          session = openSession();
68      }
69  
70      /*
71      dangerous: method to be specialized (to get different service)
72       */
73      protected void initSession() {
74          try {
75              /**/
76              Configuration configuration = new Configuration().configure() ;
77              // Create the SessionFactory from standard (hibernate.cfg.xml)
78              ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
79                      applySettings(configuration.getProperties()).buildServiceRegistry();
80              sessionFactory = configuration.buildSessionFactory(serviceRegistry);
81              /**/
82              //sessionFactory = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
83  
84  
85              // config file.
86          } catch (Throwable ex) {
87              // Log the exception.
88              CURLOG.log(Level.SEVERE, "Initial SessionFactory creation failed.", ex);
89              throw new ExceptionInInitializerError(ex);
90          }
91      }
92      public  SessionFactory getSessionFactory() {
93          return sessionFactory;
94      }
95      @Override
96      public void begin() {
97          CURLOG.fine("begin transaction");
98          session = getCurrentSession();
99          exceptionFired = false ;
100         currentTransaction = session.beginTransaction() ;
101         //throw new UnsupportedOperationException("Not supported yet.");
102     }
103 
104     @Override
105     public void end() {
106         // use of wasrollback not sure ...
107         CURLOG.fine("end transaction");
108         if(!exceptionFired) {
109             session.flush();
110             currentTransaction.commit() ;
111             currentTransaction = null ;
112         }
113     }
114 
115     @Override
116     public void fail() {
117         if(currentTransaction != null) {
118             currentTransaction.rollback();
119         }
120     }
121 
122     @Override
123     public void fail(Throwable th) {
124         CURLOG.log(Level.SEVERE,"",th);
125         fail() ;
126     }
127 
128 
129     //TODO : write a "pumping" query
130 
131     @Override
132     public void close() {
133         session.close();
134         sessionFactory.close();
135     }
136 
137     protected Session getCurrentSession() {
138         Session res = sessionFactory.getCurrentSession() ;
139         setSession(new SessionDelegate(res));
140         return res;
141 
142     }
143 
144     protected Session openSession() {
145         Session res = sessionFactory.openSession() ;
146         setSession(new SessionDelegate(res));
147         return res;
148     }
149 
150 }