1 28 29 package com.caucho.sql.spy; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import javax.transaction.xa.XAException ; 35 import javax.transaction.xa.XAResource ; 36 import javax.transaction.xa.Xid ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 40 43 public class SpyXAResource implements XAResource { 44 protected final static Logger log = Log.open(SpyXAResource.class); 45 protected final static L10N L = new L10N(SpyXAResource.class); 46 47 private XAResource _xaResource; 49 50 private int _id; 51 52 55 public SpyXAResource(int id, XAResource resource) 56 { 57 _xaResource = resource; 58 _id = id; 59 } 60 61 64 public XAResource getXAResource() 65 { 66 return _xaResource; 67 } 68 69 72 public boolean setTransactionTimeout(int seconds) 73 throws XAException 74 { 75 try { 76 boolean ok = _xaResource.setTransactionTimeout(seconds); 77 log.info(_id + ":set-transaction-timeout(" + seconds + ")->" + ok); 78 79 return ok; 80 } catch (XAException e) { 81 log.log(Level.INFO, e.toString(), e); 82 throw e; 83 } catch (RuntimeException e) { 84 log.log(Level.INFO, e.toString(), e); 85 throw e; 86 } 87 } 88 89 92 public int getTransactionTimeout() 93 throws XAException 94 { 95 try { 96 int seconds = _xaResource.getTransactionTimeout(); 97 98 log.info(_id + ":transaction-timeout()->" + seconds); 99 100 return seconds; 101 } catch (XAException e) { 102 log.log(Level.INFO, e.toString(), e); 103 throw e; 104 } catch (RuntimeException e) { 105 log.log(Level.INFO, e.toString(), e); 106 throw e; 107 } 108 } 109 110 113 public boolean isSameRM(XAResource resource) 114 throws XAException 115 { 116 try { 117 if (resource instanceof SpyXAResource) 118 resource = ((SpyXAResource) resource).getXAResource(); 119 120 boolean same = _xaResource.isSameRM(resource); 121 122 log.info(_id + ":is-same-rm(resource=" + resource + ")->" + same); 123 124 return same; 125 } catch (XAException e) { 126 log.log(Level.INFO, e.toString(), e); 127 throw e; 128 } catch (RuntimeException e) { 129 log.log(Level.INFO, e.toString(), e); 130 throw e; 131 } 132 } 133 134 137 public void start(Xid xid, int flags) 138 throws XAException 139 { 140 try { 141 String flagName = ""; 142 143 if ((flags & TMJOIN) != 0) 144 flagName += ",join"; 145 if ((flags & TMRESUME) != 0) 146 flagName += ",resume"; 147 148 log.info(_id + ":start(xid=" + xid + flagName + ")"); 149 150 _xaResource.start(xid, flags); 151 } catch (XAException e) { 152 log.log(Level.INFO, e.toString(), e); 153 throw e; 154 } catch (RuntimeException e) { 155 log.log(Level.INFO, e.toString(), e); 156 throw e; 157 } 158 } 159 160 163 public void end(Xid xid, int flags) 164 throws XAException 165 { 166 try { 167 String flagName = ""; 168 169 if ((flags & TMFAIL) != 0) 170 flagName += ",fail"; 171 if ((flags & TMSUSPEND) != 0) 172 flagName += ",suspend"; 173 174 log.info(_id + ":end(xid=" + xid + flagName + ")"); 175 176 _xaResource.end(xid, flags); 177 } catch (XAException e) { 178 log.log(Level.INFO, e.toString(), e); 179 throw e; 180 } catch (RuntimeException e) { 181 log.log(Level.INFO, e.toString(), e); 182 throw e; 183 } 184 } 185 186 189 public int prepare(Xid xid) 190 throws XAException 191 { 192 try { 193 int value = _xaResource.prepare(xid); 194 log.info(_id + ":prepare(xid=" + xid + ")->" + value); 195 196 return value; 197 } catch (XAException e) { 198 log.log(Level.INFO, e.toString(), e); 199 throw e; 200 } catch (RuntimeException e) { 201 log.log(Level.INFO, e.toString(), e); 202 throw e; 203 } 204 } 205 206 209 public void commit(Xid xid, boolean onePhase) 210 throws XAException 211 { 212 try { 213 log.info(_id + ":commit(xid=" + xid + (onePhase ? ",1P)" : ",2P)")); 214 215 _xaResource.commit(xid, onePhase); 216 } catch (XAException e) { 217 log.log(Level.INFO, e.toString(), e); 218 throw e; 219 } catch (RuntimeException e) { 220 log.log(Level.INFO, e.toString(), e); 221 throw e; 222 } 223 } 224 225 228 public void rollback(Xid xid) 229 throws XAException 230 { 231 try { 232 log.info(_id + ":rollback(xid=" + xid + ")"); 233 234 _xaResource.rollback(xid); 235 } catch (XAException e) { 236 log.log(Level.INFO, e.toString(), e); 237 throw e; 238 } catch (RuntimeException e) { 239 log.log(Level.INFO, e.toString(), e); 240 throw e; 241 } 242 } 243 244 247 public Xid []recover(int flags) 248 throws XAException 249 { 250 try { 251 String flagString = ""; 252 253 if ((flags & XAResource.TMSTARTRSCAN) != 0) 254 flagString += "start"; 255 256 if ((flags & XAResource.TMENDRSCAN) != 0) { 257 if (! flagString.equals("")) 258 flagString += ","; 259 260 flagString += "end"; 261 } 262 263 log.info(_id + ":recover(flags=" + flagString + ")"); 264 265 return _xaResource.recover(flags); 266 } catch (XAException e) { 267 log.info(e.toString()); 268 throw e; 269 } catch (RuntimeException e) { 270 log.log(Level.INFO, e.toString(), e); 271 throw e; 272 } 273 } 274 275 278 public void forget(Xid xid) 279 throws XAException 280 { 281 try { 282 log.info(_id + ":forget(xid=" + xid + ")"); 283 284 _xaResource.forget(xid); 285 } catch (XAException e) { 286 log.log(Level.INFO, e.toString(), e); 287 throw e; 288 } catch (RuntimeException e) { 289 log.log(Level.INFO, e.toString(), e); 290 throw e; 291 } 292 } 293 294 public String toString() 295 { 296 return "SpyXAResource[id=" + _id + ",resource=" + _xaResource + "]"; 297 } 298 } 299 | Popular Tags |