1 10 11 package org.mule.transaction; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import javax.transaction.HeuristicRollbackException ; 17 import javax.transaction.RollbackException ; 18 import javax.transaction.SystemException ; 19 import javax.transaction.Transaction ; 20 import javax.transaction.TransactionManager ; 21 22 import org.mule.MuleManager; 23 import org.mule.config.i18n.Message; 24 import org.mule.config.i18n.Messages; 25 import org.mule.umo.TransactionException; 26 27 30 public class XaTransaction extends AbstractTransaction 31 { 32 35 private Transaction transaction = null; 36 37 40 private Map resources = null; 41 42 45 public XaTransaction() 46 { 47 super(); 48 } 49 50 55 protected void doBegin() throws TransactionException 56 { 57 TransactionManager txManager = MuleManager.getInstance().getTransactionManager(); 58 59 if (txManager == null) 60 { 61 throw new IllegalStateException (new Message(Messages.X_NOT_REGISTERED_WITH_MANAGER, 62 "Transaction Manager").getMessage()); 63 } 64 65 try 66 { 67 txManager.begin(); 68 synchronized (this) 69 { 70 transaction = txManager.getTransaction(); 71 } 72 } 73 catch (Exception e) 74 { 75 throw new TransactionException(new Message(Messages.TX_CANT_START_X_TRANSACTION, "XA"), e); 76 } 77 } 78 79 84 protected void doCommit() throws TransactionException 85 { 86 try 87 { 88 synchronized (this) 89 { 90 transaction.commit(); 91 } 92 } 93 catch (RollbackException e) 94 { 95 throw new TransactionRollbackException(new Message(Messages.TX_MARKED_FOR_ROLLBACK), e); 96 } 97 catch (HeuristicRollbackException e) 98 { 99 throw new TransactionRollbackException(new Message(Messages.TX_MARKED_FOR_ROLLBACK), e); 100 } 101 catch (Exception e) 102 { 103 throw new IllegalTransactionStateException(new Message(Messages.TX_COMMIT_FAILED), e); 104 } 105 } 106 107 112 protected void doRollback() throws TransactionRollbackException 113 { 114 try 115 { 116 synchronized (this) 117 { 118 transaction.rollback(); 119 } 120 } 121 catch (SystemException e) 122 { 123 throw new TransactionRollbackException(e); 124 } 125 126 } 127 128 133 public int getStatus() throws TransactionStatusException 134 { 135 synchronized (this) 136 { 137 if (transaction == null) 138 { 139 return STATUS_NO_TRANSACTION; 140 } 141 142 try 143 { 144 return transaction.getStatus(); 145 } 146 catch (SystemException e) 147 { 148 throw new TransactionStatusException(e); 149 } 150 } 151 } 152 153 158 public void setRollbackOnly() 159 { 160 try 161 { 162 synchronized (this) 163 { 164 transaction.setRollbackOnly(); 165 } 166 } 167 catch (SystemException e) 168 { 169 throw new IllegalStateException ("Failed to set transaction to rollback only: " + e.getMessage()); 170 } 171 } 172 173 178 public Object getResource(Object key) 179 { 180 synchronized (this) 181 { 182 return resources == null ? null : resources.get(key); 183 } 184 } 185 186 191 public boolean hasResource(Object key) 192 { 193 synchronized (this) 194 { 195 return resources != null && resources.containsKey(key); 196 } 197 } 198 199 205 public void bindResource(Object key, Object resource) throws TransactionException 206 { 207 synchronized (this) 208 { 209 if (resources == null) 210 { 211 resources = new HashMap (); 212 } 213 214 if (resources.containsKey(key)) 215 { 216 throw new IllegalTransactionStateException(new Message( 217 Messages.TX_RESOURCE_ALREADY_LISTED_FOR_KEY_X, key)); 218 } 219 220 resources.put(key, resource); 221 } 222 } 223 } 224 | Popular Tags |