1 21 22 package com.rift.coad.util.transaction; 24 25 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.transaction.UserTransaction ; 29 import javax.transaction.TransactionManager ; 30 import javax.transaction.Status ; 31 32 import org.apache.log4j.Logger; 34 35 36 41 public class UserTransactionWrapper { 42 43 46 public class TransactionInfo { 47 48 private boolean ownLock = false; 50 private int lockCount = 0; 51 private boolean committed = false; 52 53 56 public TransactionInfo(boolean ownLock) { 57 this.ownLock = ownLock; 58 } 59 60 61 67 public boolean getOwnLock() { 68 return ownLock; 69 } 70 71 72 77 public int lock() { 78 return ++lockCount; 79 } 80 81 82 87 public int unlock() { 88 if (lockCount == 0) { 89 return lockCount; 90 } 91 return --lockCount; 92 } 93 94 95 100 public int getLockCount() { 101 return lockCount; 102 } 103 104 105 110 public boolean getCommited() { 111 return committed; 112 } 113 114 115 120 public void commit() throws TransactionException { 121 try { 122 ut.commit(); 123 } catch (java.lang.NullPointerException ex) { 124 log.error("Failed to commit the changes because of null " + 125 "pointer exception. Assuming cleanup was successfull : " 126 + ex.getMessage(),ex); 127 } catch (Exception ex) { 128 129 log.error("Failed to commit the changes : " 130 + ex.getMessage(),ex); 131 throw new TransactionException("Failed to commit the changes : " 132 + ex.getMessage(),ex); 133 } finally { 134 lockCount--; 135 committed = true; 136 } 137 138 } 139 } 140 141 protected static Logger log = 143 Logger.getLogger(UserTransactionWrapper.class.getName()); 144 145 private Context context = null; 147 private UserTransaction ut = null; 148 private TransactionManager transactionManager = null; 149 private ThreadLocal currentTransaction = new ThreadLocal (); 150 151 154 public UserTransactionWrapper() throws TransactionException { 155 try { 156 context = new InitialContext (); 157 ut = (UserTransaction )context.lookup("java:comp/UserTransaction"); 158 } catch (Exception ex) { 159 throw new TransactionException("Failed to instanciate the " + 160 "UserTransactionWrapper because : " + ex.getMessage(),ex); 161 } 162 163 } 164 165 166 172 public void begin() throws TransactionException { 173 try { 174 TransactionInfo trans = (TransactionInfo)currentTransaction.get(); 175 if (trans == null) { 176 if (ut.getStatus() == Status.STATUS_NO_TRANSACTION) { 177 ut.begin(); 178 trans = new TransactionInfo(true); 179 currentTransaction.set(trans); 180 } else { 181 trans = new TransactionInfo(false); 182 currentTransaction.set(trans); 183 } 184 } 185 trans.lock(); 186 } catch (Exception ex) { 187 throw new TransactionException("Failed to start the transaction : " + 188 ex.getMessage(),ex); 189 } 190 } 191 192 193 198 public void commit() throws TransactionException { 199 try { 200 TransactionInfo trans = (TransactionInfo)currentTransaction.get(); 201 if (trans == null) { 202 throw new TransactionException( 203 "There is no transaction for this thread"); 204 } else if (trans.getOwnLock() == false) { 205 log.info("Commit called on transaction not owned by this object"); 206 return; 207 } else if (trans.getLockCount() != 1) { 208 throw new TransactionException( 209 "This transaction cannot be commit at this point as " + 210 "there are two many recursions. " + 211 "Must be commit at the top."); 212 } 213 trans.commit(); 214 } catch (TransactionException ex) { 215 throw ex; 216 } catch (Exception ex) { 217 throw new TransactionException("Failed to start the transaction : " + 218 ex.getMessage(),ex); 219 } 220 } 221 222 223 229 public void release() { 230 try { 231 TransactionInfo trans = (TransactionInfo)currentTransaction.get(); 232 if (trans == null) { 233 return; 234 } 235 if ((0 == trans.unlock()) && trans.getOwnLock() && 236 !trans.getCommited() && 237 (ut.getStatus() == Status.STATUS_ACTIVE)) { 238 ut.rollback(); 239 } 240 if (trans.getLockCount() == 0) { 241 currentTransaction.set(null); 242 } 243 } catch (Exception ex) { 244 log.error("Failed to release the transaction : " + 245 ex.getMessage(),ex); 246 } 247 } 248 249 250 } 251 | Popular Tags |