1 10 11 package org.mule.transaction; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean; 14 15 import org.mule.config.i18n.Message; 16 import org.mule.config.i18n.Messages; 17 import org.mule.umo.TransactionException; 18 19 23 public abstract class AbstractSingleResourceTransaction extends AbstractTransaction 24 { 25 26 protected volatile Object key; 27 protected volatile Object resource; 28 29 protected final AtomicBoolean started = new AtomicBoolean(false); 30 protected final AtomicBoolean committed = new AtomicBoolean(false); 31 protected final AtomicBoolean rolledBack = new AtomicBoolean(false); 32 protected final AtomicBoolean rollbackOnly = new AtomicBoolean(false); 33 34 39 public void begin() throws TransactionException 40 { 41 super.begin(); 42 started.compareAndSet(false, true); 43 } 44 45 50 public void commit() throws TransactionException 51 { 52 super.commit(); 53 committed.compareAndSet(false, true); 54 } 55 56 61 public void rollback() throws TransactionException 62 { 63 super.rollback(); 64 rolledBack.compareAndSet(false, true); 65 } 66 67 72 public int getStatus() throws TransactionStatusException 73 { 74 if (rolledBack.get()) 75 { 76 return STATUS_ROLLEDBACK; 77 } 78 if (committed.get()) 79 { 80 return STATUS_COMMITTED; 81 } 82 if (rollbackOnly.get()) 83 { 84 return STATUS_MARKED_ROLLBACK; 85 } 86 if (started.get()) 87 { 88 return STATUS_ACTIVE; 89 } 90 return STATUS_NO_TRANSACTION; 91 } 92 93 98 public Object getResource(Object key) 99 { 100 return key != null && this.key == key ? this.resource : null; 101 } 102 103 108 public boolean hasResource(Object key) 109 { 110 return key != null && this.key == key; 111 } 112 113 119 public void bindResource(Object key, Object resource) throws TransactionException 120 { 121 if (key == null) 122 { 123 throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_BIND_TO_NULL_KEY)); 124 } 125 if (resource == null) 126 { 127 throw new IllegalTransactionStateException(new Message(Messages.TX_CANT_BIND_NULL_RESOURCE)); 128 } 129 if (this.key != null) 130 { 131 throw new IllegalTransactionStateException(new Message(Messages.TX_SINGLE_RESOURCE_ONLY)); 132 } 133 this.key = key; 134 this.resource = resource; 135 } 136 137 142 public void setRollbackOnly() 143 { 144 rollbackOnly.set(true); 145 } 146 147 public Object getId() 148 { 149 return key; 150 } 151 152 157 protected abstract void doBegin() throws TransactionException; 158 159 164 protected abstract void doCommit() throws TransactionException; 165 166 171 protected abstract void doRollback() throws TransactionException; 172 173 } 174 | Popular Tags |