1 4 package org.enhydra.shark.scriptmappersistence; 5 6 import net.sf.hibernate.HibernateException; 7 import net.sf.hibernate.Session; 8 9 import org.enhydra.shark.api.ScriptMappingTransaction; 10 import org.enhydra.shark.api.TransactionException; 11 12 17 public class HibernateScriptMappingTransaction implements ScriptMappingTransaction { 18 19 private static int noOfCreations = 0; 20 private static int noOfCommits = 0; 21 private static int noOfRollbacks = 0; 22 private static int noOfReleases=0; 23 24 private net.sf.hibernate.Transaction transactionDB; 25 26 private Session session; 27 28 public HibernateScriptMappingTransaction( 29 net.sf.hibernate.Transaction transaction) { 30 if (HibernateScriptMappingMgr._debug_) { 31 synchronized (HibernateScriptMappingTransaction.class) { 32 noOfCreations++; 33 System.out.println("CREATING T No" + noOfCreations); 34 } 35 } 36 this.transactionDB = transaction; 37 } 38 39 public net.sf.hibernate.Transaction getHibernateTransaction() { 40 return transactionDB; 41 } 42 43 public void commit() throws TransactionException { 44 if (HibernateScriptMappingMgr._debug_) { 45 synchronized (HibernateScriptMappingTransaction.class) { 46 System.out.println("COMMITING T "); 47 } 48 } 49 try { 50 transactionDB.commit(); 51 ThreadLocalSession.closeSession(); 52 transactionDB = null; 53 if (HibernateScriptMappingMgr._debug_) { 54 synchronized (HibernateScriptMappingTransaction.class) { 55 noOfCommits++; 56 System.out.println("COMMITED T No" + noOfCommits); 57 } 58 } 59 } catch (Exception ex) { 60 throw new TransactionException(ex); 61 } 62 } 63 64 public void rollback() throws TransactionException { 65 try { 66 transactionDB.rollback(); 67 ThreadLocalSession.closeSession(); 68 transactionDB = null; 69 if (HibernateScriptMappingMgr._debug_) { 70 synchronized (HibernateScriptMappingTransaction.class) { 71 noOfRollbacks++; 72 System.out.println("ROLLING BACK T " + noOfRollbacks); 73 } 74 } 75 } catch (Exception ex) { 76 if (HibernateScriptMappingMgr._debug_) { 77 System.out.println("ROLLING BACK FAILED"); 78 } 79 throw new TransactionException(ex); 80 } 81 } 82 83 public static synchronized void info() { 84 if (noOfCommits + noOfRollbacks != noOfCreations) { 85 System.err.println("PANIC!!!\nI've lost transcaton counts."); 86 } 87 System.err.println( 88 "CRE=" 89 + noOfCreations 90 + ", COMM=" 91 + noOfCommits 92 + ", ROLL=" 93 + noOfRollbacks); 94 } 95 96 public Session getSession() throws HibernateException { 97 if (transactionDB != null){ 98 return ThreadLocalSession.currentSession(); 99 } else { 100 System.err.println("BE CAREFUL, YOU ARE TRYING TO USE AN ALREADY COMMITED OR ROLLBACKED TRANSACTION!"); 101 return null; 102 } 103 } 104 105 106 public void release() throws TransactionException { 107 try { 108 ThreadLocalSession.closeSession(); 109 transactionDB = null; 110 111 if (HibernateScriptMappingMgr._debug_) { 112 synchronized (HibernateScriptMappingTransaction.class) { 113 noOfReleases++; 114 System.out.println("RELEASE T "+noOfReleases); 115 } 116 } 117 } catch (Exception ex) { 118 if (HibernateScriptMappingMgr._debug_) { 119 System.out.println("RELEASE FAILED"); 120 } 121 throw new TransactionException(ex); 122 } 123 } 124 125 } | Popular Tags |