1 package org.jboss.cache.transaction; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 6 import javax.transaction.HeuristicMixedException ; 7 import javax.transaction.HeuristicRollbackException ; 8 import javax.transaction.RollbackException ; 9 import javax.transaction.Status ; 10 import javax.transaction.Synchronization ; 11 import javax.transaction.SystemException ; 12 import javax.transaction.Transaction ; 13 import javax.transaction.xa.XAResource ; 14 import java.util.Set ; 15 import java.util.concurrent.CopyOnWriteArraySet ; 16 17 23 public class DummyTransaction implements Transaction 24 { 25 int status = Status.STATUS_UNKNOWN; 26 27 static final Log log = LogFactory.getLog(DummyTransaction.class); 28 29 DummyBaseTransactionManager tm_; 30 31 final Set <Synchronization > participants = new CopyOnWriteArraySet <Synchronization >(); 32 33 public DummyTransaction(DummyBaseTransactionManager tm) 34 { 35 tm_ = tm; 36 status = Status.STATUS_ACTIVE; 37 } 38 39 55 public void commit() 56 throws RollbackException , HeuristicMixedException , HeuristicRollbackException , 57 SecurityException , SystemException 58 { 59 boolean doCommit; 60 status = Status.STATUS_PREPARING; 61 try 62 { 63 boolean outcome = notifyBeforeCompletion(); 64 if (outcome == true && status != Status.STATUS_MARKED_ROLLBACK) 66 { 67 status = Status.STATUS_COMMITTING; 68 doCommit = true; 69 } 70 else 71 { 72 status = Status.STATUS_ROLLING_BACK; 73 doCommit = false; 74 } 75 notifyAfterCompletion(doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK); 76 status = doCommit ? Status.STATUS_COMMITTED : Status.STATUS_MARKED_ROLLBACK; 77 if (doCommit == false) 78 throw new RollbackException ("outcome is " + outcome + " status: " + status); 79 } 80 finally 81 { 82 tm_.setTransaction(null); 84 } 85 } 86 87 97 public void rollback() throws IllegalStateException , SystemException 98 { 99 try 100 { 101 status = Status.STATUS_ROLLEDBACK; 106 notifyAfterCompletion(Status.STATUS_ROLLEDBACK); 107 } 108 catch (Throwable t) 109 { 110 } 111 status = Status.STATUS_ROLLEDBACK; 112 113 tm_.setTransaction(null); 115 } 116 117 125 public void setRollbackOnly() throws IllegalStateException , SystemException 126 { 127 status = Status.STATUS_MARKED_ROLLBACK; 128 } 129 130 138 public int getStatus() throws SystemException 139 { 140 return status; 141 } 142 143 153 public void setTransactionTimeout(int seconds) throws SystemException 154 { 155 throw new SystemException ("not supported"); 156 } 157 158 172 public boolean enlistResource(XAResource xaRes) 173 throws RollbackException , IllegalStateException , SystemException 174 { 175 throw new SystemException ("not supported"); 176 } 177 178 189 public boolean delistResource(XAResource xaRes, int flag) 190 throws IllegalStateException , SystemException 191 { 192 throw new SystemException ("not supported"); 193 } 194 195 208 public void registerSynchronization(Synchronization sync) 209 throws RollbackException , IllegalStateException , SystemException 210 { 211 if (sync == null) 212 throw new IllegalArgumentException ("null synchronization " + this); 213 214 switch (status) 215 { 216 case Status.STATUS_ACTIVE: 217 case Status.STATUS_PREPARING: 218 break; 219 case Status.STATUS_PREPARED: 220 throw new IllegalStateException ("already prepared. " + this); 221 case Status.STATUS_COMMITTING: 222 throw new IllegalStateException ("already started committing. " + this); 223 case Status.STATUS_COMMITTED: 224 throw new IllegalStateException ("already committed. " + this); 225 case Status.STATUS_MARKED_ROLLBACK: 226 throw new RollbackException ("already marked for rollback " + this); 227 case Status.STATUS_ROLLING_BACK: 228 throw new RollbackException ("already started rolling back. " + this); 229 case Status.STATUS_ROLLEDBACK: 230 throw new RollbackException ("already rolled back. " + this); 231 case Status.STATUS_NO_TRANSACTION: 232 throw new IllegalStateException ("no transaction. " + this); 233 case Status.STATUS_UNKNOWN: 234 throw new IllegalStateException ("unknown state " + this); 235 default: 236 throw new IllegalStateException ("illegal status: " + status + " tx=" + this); 237 } 238 239 if (log.isDebugEnabled()) 240 { 241 log.debug("registering synchronization handler " + sync); 242 } 243 participants.add(sync); 244 245 } 246 247 void setStatus(int new_status) 248 { 249 status = new_status; 250 } 251 252 boolean notifyBeforeCompletion() 253 { 254 boolean retval = true; 255 256 for (Synchronization s : participants) 257 { 258 if (log.isDebugEnabled()) 259 { 260 log.debug("processing beforeCompletion for " + s); 261 } 262 try 263 { 264 s.beforeCompletion(); 265 } 266 catch (Throwable t) 267 { 268 retval = false; 269 log.error("beforeCompletion() failed for " + s, t); 270 } 271 } 272 return retval; 273 } 274 275 void notifyAfterCompletion(int status) 276 { 277 for (Synchronization s : participants) 278 { 279 if (log.isDebugEnabled()) 280 { 281 log.debug("processing afterCompletion for " + s); 282 } 283 try 284 { 285 s.afterCompletion(status); 286 } 287 catch (Throwable t) 288 { 289 log.error("afterCompletion() failed for " + s, t); 290 } 291 } 292 participants.clear(); 293 } 294 295 } 296 | Popular Tags |