1 4 package org.ofbiz.minerva.pool.jdbc.xa.wrapper; 5 6 import javax.transaction.xa.XAException ; 7 import javax.transaction.xa.XAResource ; 8 import javax.transaction.xa.Xid ; 9 import java.sql.Connection ; 10 import java.sql.SQLException ; 11 12 import org.apache.log4j.Logger; 13 14 28 public class XAResourceImpl implements XAResource { 29 30 private Connection con; 31 private XAConnectionImpl xaCon; 32 private Xid current; 33 private boolean active = false; 34 private int timeout_ignored = 0; 35 private Logger log = Logger.getLogger(XAResourceImpl.class); 36 37 41 public XAResourceImpl(Connection con) { 42 this.con = con; 43 } 44 45 52 void setXAConnection(XAConnectionImpl xaCon) { 53 if (this.xaCon != null) 54 throw new IllegalStateException (); 55 this.xaCon = xaCon; 56 } 57 58 public XAConnectionImpl getXAConnection() { 59 return xaCon; 60 } 61 62 68 public boolean isTransaction() { 69 return current != null; 70 } 71 72 75 public void close() { 76 con = null; 77 current = null; 78 xaCon = null; 79 } 80 81 89 public void commit(Xid id, boolean twoPhase) throws XAException { 90 if (active && !twoPhase) System.err.println("WARNING: Connection not closed before transaction commit.\nConnection will not participate in any future transactions.\nAre you sure you want to be doing this?"); 93 if (current == null || !id.equals(current)) { 95 throwXAException(XAException.XAER_NOTA); 96 } 97 98 try { 99 if (con.getAutoCommit()) { 100 throwXAException(XAException.XA_HEURCOM); 101 } 102 } catch (SQLException e) { 103 log.error(e); 104 } 105 106 try { 107 con.commit(); 108 } catch (SQLException e) { 109 log.error(e); 110 try { 111 con.rollback(); 112 if (!twoPhase) { 113 throwXAException(XAException.XA_RBROLLBACK); 114 } 115 } catch (SQLException e2) { 116 } 117 if (twoPhase) { 118 throwXAException(XAException.XA_HEURRB); } else { 120 throwXAException(XAException.XA_RBOTHER); } 122 } 124 current = null; 125 if (active) { 126 active = false; } else { 128 xaCon.transactionFinished(); } 130 } 131 132 138 public void end(Xid id, int flags) throws XAException { 139 if (!active) { 142 throwXAException(XAException.XAER_PROTO); 143 } 144 if (current == null || !id.equals(current)) { 145 throwXAException(XAException.XAER_NOTA); 146 } 147 active = false; 148 } 149 150 158 public void forget(Xid id) throws XAException { 159 if (current == null || !id.equals(current)) { 160 throwXAException(XAException.XAER_NOTA); 161 } 162 current = null; 163 xaCon.transactionFailed(); 164 if (active) System.err.println("WARNING: Connection not closed before transaction forget.\nConnection will not participate in any future transactions.\nAre you sure you want to be doing this?"); 166 } 167 168 171 public int getTransactionTimeout() throws XAException { 172 return timeout_ignored; 173 } 174 175 180 public boolean isSameRM(XAResource res) throws XAException { 181 return res == this; 182 } 183 184 192 public int prepare(Xid id) throws XAException { 193 if (active) System.err.println("WARNING: Connection not closed before transaction commit.\nConnection will not participate in any future transactions.\nAre you sure you want to be doing this?"); 196 if (current == null || !id.equals(current)) { 198 throwXAException(XAException.XAER_NOTA); 199 } 200 201 try { 202 if (con.getAutoCommit()) { 203 throwXAException(XAException.XA_HEURCOM); 204 } 205 } catch (SQLException e) { 206 log.error(e); 207 } 208 209 return XA_OK; 210 } 211 212 217 public Xid [] recover(int flag) throws javax.transaction.xa.XAException { 218 if (current == null) 219 return new Xid [0]; 220 else 221 return new Xid []{current}; 222 } 223 224 233 public void rollback(Xid id) throws XAException { 234 if (active) log.error("WARNING: Connection not closed before transaction rollback. Connection will not participate in any future transactions. Are you sure you want to be doing this?"); 237 if (current == null || !id.equals(current)) { throwXAException(XAException.XAER_NOTA); 239 } 240 try { 241 if (con.getAutoCommit()) { 242 throwXAException(XAException.XA_HEURCOM); 243 } 244 } catch (SQLException e) { 245 log.error(e); 246 } 247 248 try { 249 con.rollback(); 250 } catch (SQLException e) { 251 log.error(e); 252 throwXAException("Rollback failed: " + e.getMessage()); 253 } 254 current = null; 255 if (active) { 256 active = false; } else { 258 xaCon.transactionFinished(); } 260 } 261 262 266 public boolean setTransactionTimeout(int timeout) throws XAException { 267 timeout_ignored = timeout; 268 return true; 269 } 270 271 284 public void start(Xid id, int flags) throws XAException { 285 if (active) { if (current != null && id.equals(current)) { 288 throwXAException(XAException.XAER_DUPID); 289 } else { 290 throwXAException(XAException.XAER_PROTO); 291 } 292 } 293 if (current != null && !id.equals(current)) { 294 throwXAException(XAException.XAER_NOTA); 296 } 297 if (con == null) { 298 throwXAException(XAException.XA_RBOTHER); 299 } 300 current = id; 301 active = true; 302 } 303 304 protected void throwXAException(int code) throws XAException { 305 xaCon.setConnectionError(new SQLException ("XAException occured with code: " + code)); 306 throw new XAException (code); 307 } 308 309 protected void throwXAException(String msg) throws XAException { 310 xaCon.setConnectionError(new SQLException ("XAException occured: " + msg)); 311 throw new XAException (msg); 312 } 313 } 314 | Popular Tags |