1 55 package org.lateralnz.simpletrans; 56 57 import java.io.Serializable ; 58 import javax.naming.Reference ; 59 import javax.naming.Referenceable ; 60 import javax.transaction.HeuristicMixedException ; 61 import javax.transaction.HeuristicRollbackException ; 62 import javax.transaction.RollbackException ; 63 import javax.transaction.Status ; 64 import javax.transaction.SystemException ; 65 import javax.transaction.Transaction ; 66 import javax.transaction.TransactionManager ; 67 import javax.transaction.UserTransaction ; 68 69 import org.apache.log4j.Logger; 70 71 77 public class SimpleTransactionManager implements TransactionManager , UserTransaction , Serializable , Referenceable { 78 private static final Logger log = Logger.getLogger(SimpleTransactionManager.class.getName()); 79 private static Reference REF = new Reference (SimpleTransactionManager.class.getName()); 80 private static final SimpleTransactionManager stm = new SimpleTransactionManager(); 81 82 private static final Boolean FALSE = new Boolean (false); 83 private static final Boolean TRUE = new Boolean (true); 84 85 private static ThreadLocal IN_TRANS = new ThreadLocal () { 86 protected Object initialValue() { 87 return FALSE; 88 } 89 }; 90 91 private static ThreadLocal TRANS = new ThreadLocal () { 92 protected Object initialValue() { 93 return new SimpleTransaction(); 94 } 95 }; 96 97 public static final SimpleTransactionManager getTransactionManager() { 98 return stm; 99 } 100 101 public Reference getReference() { 102 return REF; 103 } 104 105 111 public void begin() { 112 if (!isInTransaction()) { 113 IN_TRANS.set(TRUE); 114 Transaction t = new SimpleTransaction(); 115 TRANS.set(t); 116 if (log.isDebugEnabled()) { 117 log.debug("started new transaction " + t); 118 } 119 } 120 else if (log.isDebugEnabled()) { 121 log.debug("new transaction not started, already in trans"); 122 } 123 } 124 125 129 public void commit() throws RollbackException , HeuristicMixedException , HeuristicRollbackException , SystemException { 130 if (isInTransaction()) { 131 Transaction trans = (Transaction )TRANS.get(); 132 trans.commit(); 133 TRANS.set(null); 134 IN_TRANS.set(FALSE); 135 if (log.isDebugEnabled()) { 136 log.debug("committed transaction " + trans); 137 } 138 } 139 else if (log.isDebugEnabled()) { 140 log.debug("not in transaction, no commit"); 141 } 142 } 143 144 147 public int getStatus() throws SystemException { 148 if (isInTransaction()) { 149 Transaction trans = (Transaction )TRANS.get(); 150 return trans.getStatus(); 151 } 152 else { 153 return Status.STATUS_NO_TRANSACTION; 154 } 155 } 156 157 160 public Transaction getTransaction() { 161 if (isInTransaction()) { 162 return (Transaction )TRANS.get(); 163 } 164 else { 165 return null; 166 } 167 } 168 169 172 public void resume(Transaction transaction) { 173 throw new UnsupportedOperationException (); 174 } 175 176 180 public void rollback() throws SystemException { 181 if (isInTransaction()) { 182 Transaction trans = (Transaction )TRANS.get(); 183 trans.rollback(); 184 TRANS.set(null); 185 IN_TRANS.set(FALSE); 186 if (log.isDebugEnabled()) { 187 log.debug("rolled back transaction"); 188 } 189 } 190 else if (log.isDebugEnabled()) { 191 log.debug("not in transaction, no commit"); 192 } 193 } 194 195 199 public void setRollbackOnly() throws SystemException { 200 if (isInTransaction()) { 201 Transaction trans = (Transaction )TRANS.get(); 202 trans.setRollbackOnly(); 203 } 204 } 205 206 209 public void setTransactionTimeout(int param) { 210 throw new UnsupportedOperationException (); 211 } 212 213 216 public Transaction suspend() { 217 throw new UnsupportedOperationException (); 218 } 219 220 223 protected boolean isInTransaction() { 224 Boolean b = (Boolean )IN_TRANS.get(); 225 if (b.booleanValue()) { 226 Transaction trans = (Transaction )TRANS.get(); 227 try { 228 if (trans != null && trans.getStatus() != Status.STATUS_NO_TRANSACTION 229 && trans.getStatus() != Status.STATUS_UNKNOWN) { 230 return true; 231 } 232 } 233 catch (Exception e) { 234 e.printStackTrace(); 235 } 236 } 237 return false; 238 } 239 240 241 } | Popular Tags |