1 26 package org.objectweb.jonas.jdbc; 27 28 import java.sql.SQLException ; 29 30 import javax.resource.spi.ConnectionEvent ; 31 import javax.transaction.xa.XAException ; 32 import javax.transaction.xa.XAResource ; 33 import javax.transaction.xa.Xid ; 34 35 import org.objectweb.util.monolog.api.BasicLevel; 36 37 43 public class XAResourceImpl implements XAResource { 44 45 private Xid currentXid = null; 47 48 boolean started = false; 50 boolean ended = true; 51 52 protected ManagedConnectionImpl mc = null; 53 private XAResource xares = null; 54 55 60 public XAResourceImpl(ManagedConnectionImpl mc, XAResource xares) { 61 this.mc = mc; 62 this.xares = xares; 63 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 64 mc.trace.log(BasicLevel.DEBUG, "" + mc + "," + xares); 65 } 66 } 67 68 71 public void commit(Xid xid, boolean onePhase) throws XAException { 72 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 73 mc.trace.log(BasicLevel.DEBUG, "" + xid + "," + onePhase); 74 } 75 76 try { 78 if (xares != null) { 79 xares.commit(xid, onePhase); 80 if (xid.equals(currentXid)) { 81 started = false; 82 } 83 } else { 84 if (currentXid == null || !currentXid.equals(xid) || !started) { 86 mc.trace.log(BasicLevel.ERROR, "passed xid(" + xid 87 + "),current Xid(" 88 + currentXid 89 + "),started(" + started + ")"); 90 throw new XAException ("Commit: Must call correct XAResource for its started XID"); 91 } 92 mc.connection.commit(); 93 started = false; 94 } 95 } catch (XAException xe) { 96 mc.trace.log(BasicLevel.ERROR, xe.getMessage()); 97 throw xe; 98 } catch (SQLException e) { 99 mc.trace.log(BasicLevel.ERROR, e.getMessage()); 100 try { 101 mc.signalEvent(ConnectionEvent.CONNECTION_ERROR_OCCURRED, null, e); 102 } catch (Exception ex) {} 103 throw new XAException ("Error on commit"); 104 } 105 106 try { 107 if (!mc.connection.getAutoCommit()) { 108 mc.connection.setAutoCommit(true); 109 } 110 } catch (Exception exc) { 111 if (xares == null) { 112 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 113 mc.trace.log(BasicLevel.DEBUG, 114 "Unable to set autoCommit to true:", exc); 115 } 116 } 117 } 118 } 119 120 123 public void end(Xid xid, int flags) throws XAException { 124 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 125 mc.trace.log(BasicLevel.DEBUG, "" + xid + "," + flags); 126 } 127 if (currentXid == null || !currentXid.equals(xid)) { 129 throw new XAException (XAException.XA_RBPROTO); 130 } 131 if (!started && ended) { 132 throw new XAException (XAException.XA_RBPROTO); 133 } 134 ended = true; 135 if (xares != null) { 136 xares.end(xid, flags); 137 } 138 } 139 140 143 public void forget(Xid xid) throws XAException { 144 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 145 mc.trace.log(BasicLevel.DEBUG, "" + xid); 146 } 147 if (xares != null) { 148 xares.forget(xid); 149 } 150 } 151 152 155 public int getTransactionTimeout() throws XAException { 156 if (xares != null) { 157 return xares.getTransactionTimeout(); 158 } 159 return 0; 160 } 161 162 166 public boolean isSameRM(XAResource xaRes) throws XAException { 167 168 boolean ret = false; 169 if (xaRes.equals(this)) { 170 ret = true; 171 } else if (!(xaRes instanceof XAResourceImpl)) { 172 ret = false; 173 } else { 174 XAResourceImpl xaResImpl = (XAResourceImpl) xaRes; 175 if (mc == xaResImpl.mc) { 176 ret = true; 177 } 178 } 179 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 180 mc.trace.log(BasicLevel.DEBUG, "" + xaRes + "," + this + " is " + ret); 181 } 182 return ret; 183 } 184 185 188 public int prepare(Xid xid) throws XAException { 189 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 190 mc.trace.log(BasicLevel.DEBUG, "" + xid); 191 } 192 if (xares != null) { 193 int ret = xares.prepare(xid); 194 if (ret == XA_RDONLY) { 196 started = false; 197 try { 198 if (!mc.connection.getAutoCommit()) { 199 mc.connection.setAutoCommit(true); 200 } 201 } catch (Exception exc) { } 202 } 203 return ret; 204 } 205 return XA_OK; 207 } 208 209 212 public Xid [] recover(int flag) throws XAException { 213 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 214 mc.trace.log(BasicLevel.DEBUG, "" + flag); 215 } 216 if (xares != null) { 217 return xares.recover(flag); 218 } 219 return null; 221 } 222 223 226 public void rollback(Xid xid) throws XAException { 227 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 228 mc.trace.log(BasicLevel.DEBUG, "" + xid); 229 } 230 231 try { 233 if (xares != null) { 234 xares.rollback(xid); 235 if (xid.equals(currentXid)) { 236 started = false; 237 } 238 } else { 239 if (currentXid == null || !currentXid.equals(xid) || !started) { 241 mc.trace.log(BasicLevel.ERROR, "passed xid(" + xid 242 + "),current Xid(" + currentXid 243 + "),started(" + started + ")"); 244 throw new XAException ("Rollback: Must call correct XAResource for its started XID"); 245 } 246 mc.connection.rollback(); 247 started = false; 248 } 249 } catch (XAException xe) { 250 mc.trace.log(BasicLevel.ERROR, xe.getMessage()); 251 throw xe; 252 } catch (SQLException e) { 253 try { 254 mc.trace.log(BasicLevel.ERROR, e.getMessage()); 255 mc.signalEvent( 256 ConnectionEvent.CONNECTION_ERROR_OCCURRED, null, e); 257 } catch (Exception ex) {} 258 throw(new XAException ("Error on rollback")); 259 } 260 try { 261 if (!mc.connection.getAutoCommit()) { 262 mc.connection.setAutoCommit(true); 263 } 264 } catch (Exception exc) { 265 if (xares == null) { 266 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 267 mc.trace.log(BasicLevel.DEBUG, 268 "Unable to set autoCommit to true:", exc); 269 } 270 } 271 } 272 } 273 274 277 public boolean setTransactionTimeout(int seconds) throws XAException { 278 if (xares != null) { 279 return xares.setTransactionTimeout(seconds); 280 } 281 return false; 282 } 283 284 288 public void start(Xid xid, int flags) throws XAException { 289 if (mc.trace.isLoggable(BasicLevel.DEBUG)) { 290 mc.trace.log(BasicLevel.DEBUG, "" + xid + "," + flags); 291 } 292 if (started && (currentXid == null || !currentXid.equals(xid))) { 293 mc.trace.log(BasicLevel.ERROR, 294 "Must call correct XAResource for its started XID"); 295 throw new XAException ("XAResourceImpl.start : Must call correct XAResource for its started XID"); 296 } 297 currentXid = xid; 298 started = true; 299 ended = false; 300 try { 301 if (mc.connection.getAutoCommit()) { 302 mc.connection.setAutoCommit(false); 303 } 304 } catch (Exception ex) { 305 ex.printStackTrace(); 306 mc.trace.log(BasicLevel.ERROR, 307 "Unable to set autoCommit to false:" + ex.getMessage()); 308 throw(new XAException ( 309 "Error : Unable to set autoCommit to false in start")); 310 } 311 if (xares != null) { 312 xares.start(xid, flags); 313 } 314 } 315 } 316 | Popular Tags |