1 24 package org.objectweb.jalisto.se.jca; 25 26 import org.objectweb.jalisto.se.impl.trace.Trace; 27 import org.objectweb.jalisto.se.jca.session.ManagedSession; 28 import org.objectweb.jalisto.se.jca.session.ManagedTransactionImpl; 29 30 import javax.transaction.xa.XAException ; 31 import javax.transaction.xa.Xid ; 32 33 public class JalistoXAResource { 34 35 public JalistoXAResource(ManagedSession mpm, Trace trace) { 36 this.managedJalistoSession = mpm; 37 this.trace = trace; 38 this.transaction = (ManagedTransactionImpl) mpm.currentTransaction(); 39 } 40 41 public void setCurrentXid(Xid currentXid) { 42 this.currentXid = currentXid; 43 } 44 45 public Xid getCurrentXid() { 46 return currentXid; 47 } 48 49 public void setManagedJalistoSession(ManagedSession managedJalistoSession) { 50 this.managedJalistoSession = managedJalistoSession; 51 } 52 53 public ManagedSession getManagedJalistoSession() { 54 return managedJalistoSession; 55 } 56 57 public boolean isSameJDOXAR(JalistoXAResource jdoxar) { 58 if ((managedJalistoSession == null) || (jdoxar == null)) { 59 return false; 60 } 61 62 return managedJalistoSession.equals(jdoxar.managedJalistoSession); 63 } 64 65 public void commit(Xid xid, boolean b) throws XAException { 66 getTrace().println(Trace.JCA, 67 TRACE_ID + " commit XAResource : {0} current xid : {1} call with xid : {2}", 68 this, currentXid, xid); 69 getTrace().println(Trace.JCA, TRACE_ID + " hashcodes : {0}, {1}", 70 String.valueOf(currentXid.hashCode()), 71 String.valueOf(xid.hashCode())); 72 73 if (transaction.isActive()) { 74 if ((currentXid == null) || (currentXid.hashCode() != xid.hashCode()) || !xaUsed) { 75 if (currentXid == null) { 76 throw new XAException ("Cannot mix transactions (currentXid == null)"); 77 } 78 if (currentXid.hashCode() != xid.hashCode()) { 79 throw new XAException ("Cannot mix transactions (currentXid.hashCode() != xid.hashCode())"); 80 } 81 if (!xaUsed) { 82 throw new XAException ("Cannot mix transactions (!xaUsed)"); 83 } 84 } 85 86 transaction.commit(); 87 managedJalistoSession.finishTransaction(); 88 JalistoXARPool.releaseXAResource(this); 89 currentXid = null; 90 xaUsed = false; 91 } 92 } 93 94 public void rollback(Xid xid) throws XAException { 95 getTrace().println(Trace.JCA, 96 TRACE_ID + " commit XAResource : {0} current xid : {1} call with xid : {2}", 97 this, currentXid, xid); 98 getTrace().println(Trace.JCA, TRACE_ID + " hashcodes : {0}, {1}", 99 String.valueOf(currentXid.hashCode()), 100 String.valueOf(xid.hashCode())); 101 102 if (transaction.isActive()) { 103 if ((currentXid == null) || (currentXid.hashCode() != xid.hashCode()) || !xaUsed) { 104 if (currentXid == null) { 105 throw new XAException ("Cannot mix transactions (currentXid == null)"); 106 } 107 if (currentXid.hashCode() != xid.hashCode()) { 108 throw new XAException ("Cannot mix transactions (currentXid.hashCode() != xid.hashCode())"); 109 } 110 if (!xaUsed) { 111 throw new XAException ("Cannot mix transactions (!xaUsed)"); 112 } 113 } 114 115 transaction.rollback(); 116 managedJalistoSession.finishTransaction(); 117 JalistoXARPool.releaseXAResource(this); 118 currentXid = null; 119 xaUsed = false; 120 } 121 } 122 123 public void start(Xid xid, JalistoManagedConnection mc) throws XAException { 124 if (xaUsed) { 125 if (currentXid == null) { 126 throw new XAException ("XA START: xaUsed && currentXid == null"); 127 } 128 } 129 xaUsed = true; 130 currentXid = xid; 131 try { 132 mc.setManagedJalistoSession(managedJalistoSession); 133 if (!transaction.isActive()) { 134 transaction.begin(); 135 managedJalistoSession.startTransaction(xid); 136 } 137 } catch (Throwable t) { 138 t.printStackTrace(); 139 throw new XAException ("ERROR during IES association : " + t.getMessage()); 140 } 141 getTrace().println(Trace.JCA, TRACE_ID + " start"); 142 } 143 144 private Trace getTrace() { 145 return trace; 146 } 147 148 149 private ManagedSession managedJalistoSession; 150 private ManagedTransactionImpl transaction; 151 private Xid currentXid = null; 152 private boolean xaUsed = false; 153 int timeout; 154 private Trace trace; 155 156 private static final String TRACE_ID = "[JalistoXAResource]"; 157 158 } 159 | Popular Tags |