1 25 26 package com.j2biz.blogunity.util; 27 28 import net.sf.hibernate.HibernateException; 29 import net.sf.hibernate.MappingException; 30 import net.sf.hibernate.Session; 31 import net.sf.hibernate.SessionFactory; 32 import net.sf.hibernate.Transaction; 33 import net.sf.hibernate.cfg.Configuration; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import com.j2biz.blogunity.IConstants; 39 import com.j2biz.blogunity.exception.BlogunityRuntimeException; 40 import com.j2biz.blogunity.i18n.I18N; 41 import com.j2biz.blogunity.i18n.I18NStatusFactory; 42 43 50 public class HibernateUtil { 51 54 private static final Log log = LogFactory.getLog(HibernateUtil.class); 55 56 private static SessionFactory sessionFactory = null; 57 58 private static final ThreadLocal threadSession = new ThreadLocal (); 59 60 private static final ThreadLocal threadTransaction = new ThreadLocal (); 61 62 static { 63 Configuration cfg = new Configuration(); 64 try { 65 66 for (int i = 0; i < IConstants.PERSISTENT_CLASSES.length; i++) { 67 cfg.addClass(IConstants.PERSISTENT_CLASSES[i]); 68 } 69 70 sessionFactory = cfg.buildSessionFactory(); 71 72 } catch (MappingException e) { 73 if (log.isErrorEnabled()) { 74 log.error("Error initializing SessionFactory!", e); 75 } 76 77 } catch (HibernateException e) { 78 if (log.isErrorEnabled()) { 79 log.error("Error initializing SessionFactory!", e); 80 } 81 82 } 83 84 } 85 86 91 public static Session getSession() { 92 if (log.isDebugEnabled()) { 93 log.debug("getSession() - start"); 94 } 95 96 Session s = (Session) threadSession.get(); 97 try { 99 if (s == null) { 100 s = sessionFactory.openSession(); 101 threadSession.set(s); 102 } 103 } catch (HibernateException ex) { 104 log.error("getSession()", ex); 105 throw new BlogunityRuntimeException(I18NStatusFactory.create( 106 I18N.ERRORS.OPEN_HIBERNATE_SESSION_FAILED, ex)); 107 } 108 109 if (log.isDebugEnabled()) { 110 log.debug("getSession() - end"); 111 } 112 return s; 113 } 114 115 public static void closeSession() { 116 if (log.isDebugEnabled()) { 117 log.debug("closeSession() - start"); 118 } 119 120 try { 121 Session s = (Session) threadSession.get(); 122 threadSession.set(null); 123 if (s != null && s.isOpen()) s.close(); 124 } catch (HibernateException ex) { 125 log.error("closeSession()", ex); 126 127 throw new BlogunityRuntimeException(I18NStatusFactory.create( 128 I18N.ERRORS.CLOSE_HIBERNATE_SESSION_FAILED, ex)); 129 } 130 131 if (log.isDebugEnabled()) { 132 log.debug("closeSession() - end"); 133 } 134 } 135 136 public static void beginTransaction() { 137 if (log.isDebugEnabled()) { 138 log.debug("beginTransaction() - start"); 139 } 140 141 Transaction tx = (Transaction) threadTransaction.get(); 142 try { 143 if (tx == null) { 144 tx = getSession().beginTransaction(); 145 threadTransaction.set(tx); 146 } 147 } catch (HibernateException ex) { 148 log.error("beginTransaction()", ex); 149 150 throw new BlogunityRuntimeException(I18NStatusFactory.create( 151 I18N.ERRORS.BEGIN_TRANSACTION_FAILED, ex)); 152 } 153 154 if (log.isDebugEnabled()) { 155 log.debug("beginTransaction() - end"); 156 } 157 } 158 159 public static void commitTransaction() { 160 if (log.isDebugEnabled()) { 161 log.debug("[START] commitTransaction()"); 162 } 163 164 Transaction tx = (Transaction) threadTransaction.get(); 165 try { 166 if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) { 167 168 tx.commit(); 169 } 170 threadTransaction.set(null); 171 } catch (HibernateException ex) { 172 log.error("commitTransaction()", ex); 173 174 rollbackTransaction(); 175 throw new BlogunityRuntimeException(I18NStatusFactory.create( 176 I18N.ERRORS.COMMIT_TRANSACTION_FAILED, ex)); 177 } 178 179 if (log.isDebugEnabled()) { 180 log.debug("[END] commitTransaction()"); 181 } 182 } 183 184 public static void rollbackTransaction() { 185 if (log.isDebugEnabled()) { 186 log.debug("rollbackTransaction() - start"); 187 } 188 189 Transaction tx = (Transaction) threadTransaction.get(); 190 try { 191 threadTransaction.set(null); 192 if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) { 193 194 tx.rollback(); 195 } 196 } catch (HibernateException ex) { 197 log.error("rollbackTransaction()", ex); 198 199 throw new BlogunityRuntimeException(I18NStatusFactory.create( 200 I18N.ERRORS.ROLBACK_TRANSACTION_FAILED, ex)); 201 } finally { 202 203 closeSession(); 204 } 205 206 if (log.isDebugEnabled()) { 207 log.debug("rollbackTransaction() - end"); 208 } 209 } 210 } 211 212 | Popular Tags |