1 25 26 27 package org.objectweb.jonas.jdbc_xa; 28 29 import java.sql.Connection ; 30 import java.sql.SQLException ; 31 import javax.transaction.xa.*; 32 import org.objectweb.jonas.common.Log; 33 import org.objectweb.util.monolog.api.Logger; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 36 49 public class XAResourceImpl implements XAResource { 50 51 private static Logger logger = null; 52 53 private Connection actConn = null; 54 private XAConnectionImpl xac = null; 55 private int timeout; 56 private Xid currXid = null; 57 private boolean xaStarted = false; 58 private String rmid = null; 59 60 64 70 public XAResourceImpl(XAConnectionImpl xac, Connection actual, XADataSourceImpl ds) { 71 this.xac = xac; 72 this.actConn = actual; 73 this.rmid = ds.getDataSourceName(); 74 logger = Log.getLogger(Log.JONAS_JDBCXA_PREFIX); 75 if (logger.isLoggable(BasicLevel.DEBUG)) { 76 logger.log(BasicLevel.DEBUG, "new object for " + this); 77 } 78 } 79 80 84 90 public synchronized void commit(Xid xid, boolean onePhase) throws XAException { 91 if (logger.isLoggable(BasicLevel.DEBUG)) { 92 logger.log(BasicLevel.DEBUG, "XA-COMMIT for " + this); 93 logger.log(BasicLevel.DEBUG, "Xid = " + xid); 94 } 95 96 if (currXid != null && !currXid.equals(xid)) { 100 if (logger.isLoggable(BasicLevel.DEBUG)) { 101 logger.log(BasicLevel.DEBUG, "mixed xids: " + xid + " != " + currXid); 102 } 103 XAException ex = new XAException("cannot mix transactions"); 104 throw(ex); 105 } 106 currXid = null; if (xaStarted) { 108 logger.log(BasicLevel.WARN, "XA START without XA END"); 112 113 xaStarted = false; 114 } 115 116 try { 118 actConn.commit(); 119 } catch (SQLException e) { 120 logger.log(BasicLevel.ERROR, "Cannot commit transaction:" + e); 121 xac.notifyError(e); 122 throw(new XAException("Error on commit")); 123 } 124 } 125 126 132 public synchronized void end(Xid xid, int flags) throws XAException { 133 if (logger.isLoggable(BasicLevel.DEBUG)) { 134 logger.log(BasicLevel.DEBUG, "XA-END for " + this); 135 logger.log(BasicLevel.DEBUG, "Xid = " + xid); 136 } 137 138 if (currXid == null || !currXid.equals(xid)) { 140 logger.log(BasicLevel.WARN, "END without START or mixed xids: " + xid + " != " + currXid); 141 XAException ex = new XAException(XAException.XA_RBPROTO); 142 throw(ex); 143 } 144 xaStarted = false; 145 } 146 147 153 public synchronized void forget(Xid xid) throws XAException { 154 155 if (logger.isLoggable(BasicLevel.DEBUG)) { 157 logger.log(BasicLevel.DEBUG, "XA-FORGET for " + this); 158 logger.log(BasicLevel.DEBUG, "Xid = " + xid); 159 } 160 currXid = null; 161 } 162 163 169 public synchronized int getTransactionTimeout() throws XAException { 170 if (logger.isLoggable(BasicLevel.DEBUG)) { 171 logger.log(BasicLevel.DEBUG, "getTransactionTimeout for " + this); 172 } 173 return timeout; 174 } 175 176 183 public boolean isSameRM(XAResource xares) throws XAException { 184 185 186 if (xares.equals(this)) { 192 if (logger.isLoggable(BasicLevel.DEBUG)) { 193 logger.log(BasicLevel.DEBUG, "isSameRM = true " + this); 194 } 195 return true; 196 } 197 if (logger.isLoggable(BasicLevel.DEBUG)) { 198 logger.log(BasicLevel.DEBUG, "isSameRM = false " + this); 199 } 200 return false; 201 } 202 203 204 210 public synchronized int prepare(Xid xid) throws XAException { 211 212 if (logger.isLoggable(BasicLevel.DEBUG)) { 213 logger.log(BasicLevel.DEBUG, "XA-PREPARE for " + this); 214 logger.log(BasicLevel.DEBUG, "Xid = " + xid); 215 } 216 return XA_OK; 218 } 219 220 226 public synchronized Xid[] recover(int flag) throws XAException { 227 228 if (logger.isLoggable(BasicLevel.DEBUG)) { 229 logger.log(BasicLevel.DEBUG, "XA-RECOVER for " + this); 230 } 231 return null; 233 } 234 235 241 public synchronized void rollback(Xid xid) throws XAException { 242 243 if (logger.isLoggable(BasicLevel.DEBUG)) { 244 logger.log(BasicLevel.DEBUG, "XA-ROLLBACK for " + this); 245 logger.log(BasicLevel.DEBUG, "Xid = " + xid); 246 } 247 248 if (currXid != null && !currXid.equals(xid)) { 252 if (logger.isLoggable(BasicLevel.DEBUG)) { 253 logger.log(BasicLevel.DEBUG, "mixed xids: " + xid + " != " + currXid); 254 } 255 XAException ex = new XAException("Cannot mix transactions"); 256 throw(ex); 257 } 258 currXid = null; if (xaStarted) { 260 logger.log(BasicLevel.WARN, "XA START without XA END"); 264 xaStarted = false; 265 } 266 267 try { 269 if (actConn.getAutoCommit() == true) { 270 logger.log(BasicLevel.ERROR, "Rollback called on XAResource with AutoCommit set"); 271 throw (new XAException(XAException.XA_HEURCOM)); 272 } 273 } catch (SQLException e) { 274 logger.log(BasicLevel.ERROR, "Cannot getAutoCommit:" + e); 275 xac.notifyError(e); 276 throw(new XAException("Error on getAutoCommit")); 277 } 278 279 try { 281 actConn.rollback(); 282 } catch (SQLException e) { 283 logger.log(BasicLevel.ERROR, "Cannot rollback transaction:" + e); 284 xac.notifyError(e); 285 throw(new XAException("Error on rollback")); 286 } 287 } 288 289 296 public synchronized boolean setTransactionTimeout(int seconds) throws XAException { 297 298 if (logger.isLoggable(BasicLevel.DEBUG)) { 299 logger.log(BasicLevel.DEBUG, "setTransactionTimeout " + this); 300 } 301 timeout = seconds; 302 return true; 303 } 304 305 310 public synchronized void start(Xid xid, int flags) throws XAException { 311 312 if (logger.isLoggable(BasicLevel.DEBUG)) { 313 logger.log(BasicLevel.DEBUG, "XA-START for " + this); 314 logger.log(BasicLevel.DEBUG, "Xid = " + xid); 315 } 316 if (xaStarted) { 317 if (logger.isLoggable(BasicLevel.DEBUG)) { 318 logger.log(BasicLevel.DEBUG, "mixed xids: " + xid + " != " + currXid); 319 } 320 XAException ex = new XAException("XA START: mixed transactions"); 321 throw ex; 322 } 323 currXid = xid; 324 xaStarted = true; 325 } 326 327 331 335 public String getRMID() { 336 return rmid; 337 } 338 } 339 | Popular Tags |