1 18 package org.apache.activemq.ra; 19 20 import javax.jms.JMSException ; 21 import javax.resource.ResourceException ; 22 import javax.resource.spi.LocalTransaction ; 23 import javax.transaction.xa.XAException ; 24 import javax.transaction.xa.XAResource ; 25 import javax.transaction.xa.Xid ; 26 27 import org.apache.activemq.TransactionContext; 28 29 32 public class LocalAndXATransaction implements XAResource , LocalTransaction { 33 34 final private TransactionContext transactionContext; 35 private boolean inManagedTx; 36 37 public LocalAndXATransaction(TransactionContext transactionContext) { 38 this.transactionContext=transactionContext; 39 } 40 41 public void setInManagedTx(boolean inManagedTx) throws JMSException { 42 this.inManagedTx=inManagedTx; 43 if( !inManagedTx ) 44 transactionContext.cleanup(); 45 } 46 47 public void begin() throws ResourceException { 48 try { 49 transactionContext.begin(); 50 setInManagedTx(true); 51 } catch (JMSException e) { 52 throw new ResourceException ("begin failed.", e); 53 } 54 } 55 56 public void commit() throws ResourceException { 57 try { 58 transactionContext.commit(); 59 } catch (JMSException e) { 60 throw new ResourceException ("commit failed.", e); 61 } finally { 62 try { 63 setInManagedTx(false); 64 } catch (JMSException e) { 65 throw new ResourceException ("commit failed.",e); 66 } 67 } 68 } 69 70 public void rollback() throws ResourceException { 71 try { 72 transactionContext.rollback(); 73 } catch (JMSException e) { 74 throw new ResourceException ("rollback failed.", e); 75 } finally { 76 try { 77 setInManagedTx(false); 78 } catch (JMSException e) { 79 throw new ResourceException ("rollback failed.",e); 80 } 81 } 82 } 83 84 public void commit(Xid arg0, boolean arg1) throws XAException { 85 transactionContext.commit(arg0, arg1); 86 } 87 88 public void end(Xid arg0, int arg1) throws XAException { 89 try { 90 transactionContext.end(arg0, arg1); 91 } finally { 92 try { 93 setInManagedTx(false); 94 } catch (JMSException e) { 95 throw (XAException )new XAException (XAException.XAER_PROTO).initCause(e); 96 } 97 } 98 } 99 100 public void forget(Xid arg0) throws XAException { 101 transactionContext.forget(arg0); 102 } 103 104 public int getTransactionTimeout() throws XAException { 105 return transactionContext.getTransactionTimeout(); 106 } 107 108 public boolean isSameRM(XAResource xaresource) throws XAException { 109 if (xaresource == null) 110 return false; 111 if (xaresource instanceof LocalAndXATransaction) { 113 xaresource = ((LocalAndXATransaction)xaresource).transactionContext; 114 } 115 return transactionContext.isSameRM(xaresource); 116 } 117 118 public int prepare(Xid arg0) throws XAException { 119 return transactionContext.prepare(arg0); 120 } 121 122 public Xid [] recover(int arg0) throws XAException { 123 return transactionContext.recover(arg0); 124 } 125 126 public void rollback(Xid arg0) throws XAException { 127 transactionContext.rollback(arg0); 128 } 129 130 public boolean setTransactionTimeout(int arg0) throws XAException { 131 return transactionContext.setTransactionTimeout(arg0); 132 } 133 134 135 public void start(Xid arg0, int arg1) throws XAException { 136 transactionContext.start(arg0, arg1); 137 try { 138 setInManagedTx(true); 139 } catch (JMSException e) { 140 throw (XAException )new XAException (XAException.XAER_PROTO).initCause(e); 141 } 142 } 143 144 public boolean isInManagedTx() { 145 return inManagedTx; 146 } 147 148 public void cleanup() { 149 transactionContext.cleanup(); 150 inManagedTx=false; 151 } 152 } 153 | Popular Tags |