1 46 47 package org.mr.ra; 48 49 import javax.jms.JMSException ; 50 import javax.resource.ResourceException ; 51 import javax.resource.spi.LocalTransaction ; 52 import javax.transaction.xa.XAException ; 53 import javax.transaction.xa.XAResource ; 54 import javax.transaction.xa.Xid ; 55 56 import org.apache.commons.logging.Log; 57 import org.apache.commons.logging.LogFactory; 58 import org.mr.api.jms.TransactionContext; 59 60 61 64 public class LocalAndXATransaction implements XAResource , LocalTransaction { 65 66 static final private Log log = LogFactory.getLog(LocalAndXATransaction.class); 67 final private TransactionContext transactionContext; 68 private boolean inManagedTx = false; 69 70 public LocalAndXATransaction(TransactionContext transactionContext) { 71 this.transactionContext = transactionContext; 72 } 73 74 public void setInManagedTx(boolean inManagedTx) throws JMSException { 75 this.inManagedTx = inManagedTx; 76 } 77 78 public void begin() throws ResourceException { 79 try { 80 transactionContext.begin(); 81 setInManagedTx(true); 82 } catch (JMSException e) { 83 throw new ResourceException ("begin failed.", e); 84 } 85 } 86 87 public void commit() throws ResourceException { 88 try { 89 transactionContext.commit(); 90 } catch (JMSException e) { 91 throw new ResourceException ("commit failed.", e); 92 } finally { 93 try { 94 setInManagedTx(false); 95 } catch (JMSException e) { 96 throw new ResourceException ("commit failed.",e); 97 } 98 } 99 } 100 101 public void rollback() throws ResourceException { 102 try { 103 transactionContext.rollback(); 104 } catch (JMSException e) { 105 throw new ResourceException ("rollback failed.", e); 106 } finally { 107 try { 108 setInManagedTx(false); 109 } catch (JMSException e) { 110 throw new ResourceException ("rollback failed.",e); 111 } 112 } 113 } 114 115 public void commit(Xid arg0, boolean arg1) throws XAException { 116 transactionContext.commit(arg0, arg1); 117 } 118 119 public void end(Xid arg0, int arg1) throws XAException { 120 try { 121 transactionContext.end(arg0, arg1); 122 } finally { 123 try { 124 setInManagedTx(false); 125 } catch (JMSException e) { 126 throw (XAException )new XAException (XAException.XAER_PROTO).initCause(e); 127 } 128 } 129 } 130 131 public void forget(Xid arg0) throws XAException { 132 transactionContext.forget(arg0); 133 } 134 135 public int getTransactionTimeout() throws XAException { 136 return transactionContext.getTransactionTimeout(); 137 } 138 139 public boolean isSameRM(XAResource xaresource) throws XAException { 140 if (xaresource == null) { 141 return false; 142 } 143 if (xaresource instanceof LocalAndXATransaction) { 145 xaresource = ((LocalAndXATransaction)xaresource).transactionContext; 146 } 147 boolean b = transactionContext.isSameRM(xaresource); 148 return b; 149 } 151 152 public int prepare(Xid arg0) throws XAException { 153 return transactionContext.prepare(arg0); 154 } 155 156 public Xid [] recover(int arg0) throws XAException { 157 return transactionContext.recover(arg0); 158 } 159 160 public void rollback(Xid arg0) throws XAException { 161 transactionContext.rollback(arg0); 162 } 163 164 public boolean setTransactionTimeout(int arg0) throws XAException { 165 return transactionContext.setTransactionTimeout(arg0); 166 } 167 168 public void start(Xid arg0, int arg1) throws XAException { 169 transactionContext.start(arg0, arg1); 170 try { 171 setInManagedTx(true); 172 } catch (JMSException e) { 173 throw (XAException )new XAException (XAException.XAER_PROTO).initCause(e); 174 } 175 } 176 177 public boolean isInManagedTx() { 178 return inManagedTx; 179 } 180 } | Popular Tags |