1 21 22 package com.rift.coad.hibernate.util; 23 24 import java.util.StringTokenizer ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.concurrent.ConcurrentHashMap ; 29 import javax.naming.Context ; 30 import javax.naming.InitialContext ; 31 import javax.transaction.Transaction ; 32 import javax.transaction.TransactionManager ; 33 import javax.transaction.xa.XAException ; 34 import javax.transaction.xa.XAResource ; 35 import javax.transaction.xa.Xid ; 36 37 38 import org.apache.log4j.Logger; 40 41 import org.hibernate.*; 43 import org.hibernate.cfg.*; 44 45 48 public class HibernateUtil implements XAResource { 49 50 public final static String RESOURCES_CONFIG = "hibernate_resource_config"; 52 public final static String HIBERNATE_SQL = "hibernate_sql"; 53 public final static String DB_SOURCE = "db_datasource"; 54 public final static String HBM2DDL = "hibernate_hbm2ddl_auto"; 55 public final static String TRANSACTION_TIMEOUT = "transaction_timeout"; 56 public final static int DEFAULT_TRANSACTION_TIMEOUT = 180000; 57 58 private static Map singletons = new HashMap (); 60 61 protected static Logger log = 63 Logger.getLogger(HibernateUtil.class.getName()); 64 65 private int timeout = 0; 67 private Context context = null; 68 private SessionFactory sessionFactory = null; 69 private Map sessions = new ConcurrentHashMap (); 70 private ThreadLocal currentSession = new ThreadLocal (); 71 72 73 79 private HibernateUtil(Class configId) throws HibernateUtilException { 80 try { 81 context = new InitialContext (); 83 84 com.rift.coad.lib.configuration.Configuration coadConfig = 86 com.rift.coad.lib.configuration.ConfigurationFactory. 87 getInstance() 88 .getConfig(configId); 89 90 timeout = (int)coadConfig.getLong(TRANSACTION_TIMEOUT, 92 DEFAULT_TRANSACTION_TIMEOUT); 93 94 org.hibernate.cfg.Configuration config = new 96 org.hibernate.cfg.Configuration() 97 .configure(coadConfig.getString(RESOURCES_CONFIG)) 98 .setProperty("hibernate.dialect", 99 coadConfig.getString("db_dialect")) 100 .setProperty("hibernate.connection.datasource", 101 coadConfig.getString(DB_SOURCE)) 102 .setProperty("hibernate.current_session_context_class","jta") 103 .setProperty("hibernate.transaction.factory_class", 104 "org.hibernate.transaction.JTATransactionFactory") 105 .setProperty("hibernate.transaction.manager_lookup_class", 106 "org.hibernate.transaction.JOTMTransactionManagerLookup") 107 .setProperty("hibernate.connection.autocommit","false") 108 .setProperty("hibernate.cache.provider_class", 109 "org.hibernate.cache.NoCacheProvider") 110 .setProperty("hibernate.show_sql", 111 coadConfig.getString(HIBERNATE_SQL,"false")) 112 .setProperty("hibernate.hbm2ddl.auto", 113 coadConfig.getString(HBM2DDL,"update")); 114 115 sessionFactory = config.buildSessionFactory(); 116 } catch (Throwable ex) { 117 log.error("Initial SessionFactory " + 118 "creation failed: " + ex.getMessage(),ex); 119 throw new HibernateUtilException("Initial SessionFactory " + 120 "creation failed: " + ex.getMessage(),ex); 121 } 122 } 123 124 125 133 public synchronized static HibernateUtil getInstance(Class configId) throws 134 HibernateUtilException { 135 HibernateUtil singleton = null; 136 if (!singletons.containsKey(configId)) { 137 singleton = new HibernateUtil(configId); 138 singletons.put(configId,singleton); 139 } else { 140 singleton = (HibernateUtil)singletons.get(configId); 141 } 142 return singleton; 143 } 144 145 146 152 public Session getSession() throws HibernateUtilException { 153 try { 154 Transaction transaction = getTransaction(); 155 transaction.enlistResource(this); 156 return (Session)currentSession.get(); 157 } catch (HibernateUtilException ex) { 158 throw ex; 159 } catch (Exception ex) { 160 log.error("Failed to retrieve the current session for this thread : " 161 + ex.getMessage(),ex); 162 throw new HibernateUtilException( 163 "Failed to retrieve the current session for this thread : " 164 + ex.getMessage(),ex); 165 } 166 } 167 168 169 177 public void commit(Xid xid, boolean b) throws XAException { 178 if (this.sessions.containsKey(xid)) { 179 Session session = (Session)sessions.get(xid); 180 sessions.remove(xid); 181 try { 182 session.connection().setAutoCommit(true); 183 } catch (Exception ex) { 184 log.error("Failed to reset the auto commit flag on the " + 185 "connection : " + ex.getMessage(),ex); 186 } 187 } 188 } 189 190 191 198 public void end(Xid xid, int flags) throws XAException { 199 } 200 201 202 208 public void forget(Xid xid) throws XAException { 209 if (this.sessions.containsKey(xid)) { 210 Session session = (Session)sessions.get(xid); 211 sessions.remove(xid); 212 try { 213 session.connection().setAutoCommit(true); 214 } catch (Exception ex) { 215 log.error("Failed to reset the auto commit flag on the " + 216 "connection : " + ex.getMessage(),ex); 217 } 218 } 219 } 220 221 222 228 public int getTransactionTimeout() throws XAException { 229 return timeout; 230 } 231 232 233 241 public boolean isSameRM(XAResource xAResource) throws XAException { 242 return this == xAResource; 243 } 244 245 246 253 public int prepare(Xid xid) throws XAException { 254 return XAResource.XA_OK; 255 } 256 257 258 266 public Xid [] recover(int flags) throws XAException { 267 return null; 268 } 269 270 271 277 public void rollback(Xid xid) throws XAException { 278 if (this.sessions.containsKey(xid)) { 279 Session session = (Session)sessions.get(xid); 280 sessions.remove(xid); 281 try { 282 session.connection().setAutoCommit(true); 283 } catch (Exception ex) { 284 log.error("Failed to reset the auto commit flag on the " + 285 "connection : " + ex.getMessage(),ex); 286 } 287 288 } 289 } 290 291 292 299 public boolean setTransactionTimeout(int transactionTimeout) throws 300 XAException { 301 timeout = transactionTimeout; 302 return true; 303 } 304 305 306 313 public void start(Xid xid, int i) throws XAException { 314 Session session = null; 315 if (this.sessions.containsKey(xid)) { 316 session = (Session)sessions.get(xid); 317 } else { 318 try { 319 session = sessionFactory.getCurrentSession(); 320 session.connection().setAutoCommit(false); 321 sessions.put(xid,session); 322 } catch (Exception ex) { 323 log.error("Failed to start the transaction : " 324 + ex.getMessage(),ex); 325 throw new XAException ( 326 "Failed to start the transaction : " + ex.getMessage()); 327 } 328 } 329 this.currentSession.set(session); 330 } 331 332 333 339 private Transaction getTransaction() throws HibernateUtilException { 340 try { 341 TransactionManager transactionManager = (TransactionManager ) 342 context.lookup("java:comp/TransactionManager"); 343 return transactionManager.getTransaction(); 344 } catch (Exception ex) { 345 log.error("Failed to retrieve the current transaction because : " 346 + ex.getMessage(),ex); 347 throw new HibernateUtilException( 349 "Failed to retrieve the current transaction because : " 350 + ex.getMessage(),ex); 351 } 352 } 353 } | Popular Tags |