1 18 package org.ofbiz.entity.transaction; 19 20 import org.ofbiz.base.util.Debug; 21 22 import javax.transaction.xa.XAResource ; 23 import javax.transaction.xa.Xid ; 24 import javax.transaction.xa.XAException ; 25 import javax.transaction.*; 26 27 34 public abstract class GenericXaResource implements XAResource { 35 36 public static final String module = GenericXaResource.class.getName(); 37 38 protected boolean active = false; 39 protected int timeout = 0; 40 protected Xid xid = null; 41 42 46 public void enlist() throws XAException { 47 TransactionManager tm = TransactionFactory.getTransactionManager(); 48 try { 49 if (tm != null && tm.getStatus() == Status.STATUS_ACTIVE) { 50 Transaction tx = tm.getTransaction(); 51 if (tx != null) { 52 tx.enlistResource(this); 53 } else { 54 throw new XAException (XAException.XAER_NOTA); 55 } 56 } else { 57 throw new XAException ("No transaction manager or invalid status"); 58 } 59 } catch (SystemException e) { 60 throw new XAException ("Unable to get transaction status"); 61 } catch (RollbackException e) { 62 throw new XAException ("Unable to enlist resource with transaction"); 63 } 64 } 65 66 69 public void start(Xid xid, int flag) throws XAException { 70 if (this.active) { 71 if (this.xid != null && this.xid.equals(xid)) { 72 throw new XAException (XAException.XAER_DUPID); 73 } else { 74 throw new XAException (XAException.XAER_PROTO); 75 } 76 } 77 if (this.xid != null && !this.xid.equals(xid)) { 78 throw new XAException (XAException.XAER_NOTA); 79 } 80 81 this.xid = xid; 82 this.active = true; 83 } 84 85 88 public void end(Xid xid, int flag) throws XAException { 89 if (!this.active) { 90 throw new XAException (XAException.XAER_PROTO); 91 } 92 93 if (this.xid == null || !this.xid.equals(xid)) { 94 throw new XAException (XAException.XAER_NOTA); 95 } 96 this.active = false; 97 } 98 99 102 public void forget(Xid xid) throws XAException { 103 if (this.xid == null || !this.xid.equals(xid)) { 104 throw new XAException (XAException.XAER_NOTA); 105 } 106 this.xid = null; 107 if (active) { 108 Debug.logWarning("forget() called without end()", module); 110 } 111 } 112 113 116 public int prepare(Xid xid) throws XAException { 117 if (this.xid == null || !this.xid.equals(xid)) { 118 throw new XAException (XAException.XAER_NOTA); 119 } 120 return XA_OK; 121 } 122 123 126 public Xid [] recover(int flag) throws XAException { 127 if (this.xid == null) { 128 return new Xid [0]; 129 } else { 130 return new Xid [] {this.xid}; 131 } 132 } 133 134 137 public boolean isSameRM(XAResource xaResource) throws XAException { 138 return xaResource == this; 139 } 140 141 144 public int getTransactionTimeout() throws XAException { 145 return this.timeout; 146 } 147 148 152 public boolean setTransactionTimeout(int seconds) throws XAException { 153 this.timeout = seconds; 154 return true; 155 } 156 157 public Xid getXid() { 158 return this.xid; 159 } 160 161 164 public abstract void commit(Xid xid, boolean onePhase) throws XAException ; 165 166 169 public abstract void rollback(Xid xid) throws XAException ; 170 } 171 | Popular Tags |