View Javadoc
1 package org.smartcomps.twister.common.persistence; 2 3 import net.sf.hibernate.FlushMode; 4 import net.sf.hibernate.HibernateException; 5 import net.sf.hibernate.Session; 6 import net.sf.hibernate.SessionFactory; 7 import net.sf.hibernate.Transaction; 8 import net.sf.hibernate.tool.hbm2ddl.SchemaExport; 9 import net.sf.hibernate.cfg.Configuration; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 14 import org.smartcomps.twister.common.transaction.TransactionalResource; 15 import org.smartcomps.twister.common.transaction.TransactionException; 16 17 import java.util.List; 18 19 /*** 20 * Manages transactions and sessions explicitly and makes the persistence 21 * accessible from any execution point. Sessions and transactions are 22 * bound to the current thread using the ThreadLocal class. 23 * @see ThreadLocal 24 */ 25 public class DBSessionManager implements TransactionalResource { 26 27 private static Log log = LogFactory.getLog(DBSessionManager.class); 28 29 private static SessionFactory sessionFactory; 30 31 private static ThreadLocal localSession; 32 private static ThreadLocal localTransaction; 33 34 public void initialize() throws TransactionException { 35 if (sessionFactory == null) { 36 try { 37 sessionFactory = new Configuration().configure().buildSessionFactory(); 38 } catch (HibernateException e) { 39 log.error("Could not initialize or configure the persistence factory.", e); 40 throw new TransactionException(e); 41 } 42 } 43 44 } 45 46 public void beginTransaction() throws TransactionException { 47 localSession = new ThreadLocal(); 48 localTransaction = new ThreadLocal(); 49 if (sessionFactory == null) { 50 throw new TransactionException("This transaction resource has not been initialized, please " + 51 "initialize it first."); 52 } 53 if (localSession.get() != null) { 54 throw new TransactionException("A transaction has already been started"); 55 } 56 57 Session session = null; 58 Transaction tx = null; 59 try { 60 session = sessionFactory.openSession(); 61 session.setFlushMode(FlushMode.AUTO); 62 63 tx = session.beginTransaction(); 64 } catch (HibernateException e) { 65 log.error("Could not initialize the persistence.", e); 66 throw new TransactionException(e); 67 } 68 69 localSession.set(session); 70 localTransaction.set(tx); 71 } 72 73 public void commitTransaction() throws TransactionException { 74 Transaction tx = (Transaction) localTransaction.get(); 75 Session session = (Session) localSession.get(); 76 try { 77 tx.commit(); 78 } catch (HibernateException e) { 79 log.error("Error closing the persistence when commiting.", e); 80 throw new TransactionException(e); 81 } finally { 82 try { 83 session.close(); 84 } catch (HibernateException e) { 85 log.fatal("Session could not be closed !!!", e); 86 } 87 localSession.set(null); 88 localTransaction.set(null); 89 } 90 } 91 92 public void rollbackTransaction() throws TransactionException { 93 Transaction tx = (Transaction) localTransaction.get(); 94 Session session = (Session) localSession.get(); 95 try { 96 tx.rollback(); 97 } catch (HibernateException e) { 98 log.error("Error closing the persistence when rollbacking.", e); 99 throw new TransactionException(e); 100 } finally { 101 try { 102 session.close(); 103 } catch (HibernateException e) { 104 log.fatal("Session could not be closed !!!", e); 105 } 106 localSession.set(null); 107 localTransaction.set(null); 108 } 109 } 110 111 public static Session getActiveSession() throws DBSessionException { 112 if (localSession == null) 113 throw new DBSessionException("No active persistence, the transaction has probably not been initialized."); 114 Session session = (Session) localSession.get(); 115 if (session == null) 116 throw new DBSessionException("No active persistence, the transaction has probably not been initialized."); 117 return session; 118 } 119 120 }

This page was automatically generated by Maven