1 8 9 package com.sleepycat.je; 10 11 import java.io.File ; 12 13 import javax.transaction.xa.XAException ; 14 import javax.transaction.xa.XAResource ; 15 import javax.transaction.xa.Xid ; 16 17 import com.sleepycat.je.txn.Txn; 18 import com.sleepycat.je.txn.TxnManager; 19 20 24 public class XAEnvironment extends Environment implements XAResource { 25 26 private static final boolean DEBUG = false; 27 28 32 public XAEnvironment(File envHome, EnvironmentConfig configuration) 33 throws DatabaseException { 34 35 super(envHome, configuration); 36 } 37 38 41 public Transaction getXATransaction(Xid xid) 42 throws DatabaseException { 43 44 Txn ret = environmentImpl.getTxnManager().getTxnFromXid(xid); 45 if (ret == null) { 46 return null; 47 } 48 49 return new Transaction(this, ret); 50 } 51 52 55 public void setXATransaction(Xid xid, Transaction txn) 56 throws DatabaseException { 57 58 environmentImpl.getTxnManager(). 59 registerXATxn(xid, txn.getTxn(), false); 60 } 61 62 65 66 70 public void commit(Xid xid, boolean ignore ) 71 throws XAException { 72 73 if (DEBUG) { 74 System.out.println("*** commit called " + xid + "/" + ignore); 75 } 76 77 if (xid == null) { 78 return; 79 } 80 81 try { 82 checkEnv(); 83 Transaction txn = getXATransaction(xid); 84 if (txn == null) { 85 throw new XAException 86 ("No transaction found for " + xid + " during commit."); 87 } 88 removeReferringHandle(txn); 89 if (txn.getTxn().getOnlyAbortable()) { 90 throw new XAException (XAException.XA_RBROLLBACK); 91 } 92 txn.getTxn().commit(xid); 93 } catch (DatabaseException DE) { 94 throwNewXAException(DE); 95 } 96 if (DEBUG) { 97 System.out.println("*** commit finished"); 98 } 99 } 100 101 105 public void end(Xid xid, int flags) 106 throws XAException { 107 108 if (DEBUG) { 109 System.out.println("*** end called " + xid + "/" + flags); 110 } 111 112 113 114 boolean tmFail = (flags & XAResource.TMFAIL) != 0; 115 boolean tmSuccess = (flags & XAResource.TMSUCCESS) != 0; 116 boolean tmSuspend = (flags & XAResource.TMSUSPEND) != 0; 117 if ((tmFail && tmSuccess) || 118 ((tmFail || tmSuccess) && tmSuspend)) { 119 throw new XAException (XAException.XAER_INVAL); 120 } 121 122 try { 123 if (DEBUG) { 124 System.out.println 125 ("Transaction for " + Thread.currentThread() + " is " + 126 environmentImpl.getTxnManager().getTxnForThread()); 127 } 128 129 Transaction txn = 130 environmentImpl.getTxnManager().unsetTxnForThread(); 131 if (txn == null) { 132 txn = getXATransaction(xid); 133 boolean isSuspended = (txn != null) && 134 txn.getTxn().isSuspended(); 135 if (!isSuspended) { 136 throw new XAException (XAException.XAER_NOTA); 137 } 138 } 139 140 if (tmFail) { 141 txn.getTxn().setOnlyAbortable(); 142 } 143 144 if (tmSuspend) { 145 txn.getTxn().setSuspended(true); 146 } 147 148 } catch (DatabaseException DE) { 149 throwNewXAException(DE); 150 } 151 } 152 153 157 public void forget(Xid xid) 158 throws XAException { 159 160 if (DEBUG) { 161 System.out.println("*** forget called"); 162 } 163 164 throw new XAException (XAException.XAER_NOTA); 165 } 166 167 171 public boolean isSameRM(XAResource rm) 172 throws XAException { 173 174 if (DEBUG) { 175 System.out.println("*** isSameRM called"); 176 } 177 178 try { 179 checkEnv(); 180 } catch (DatabaseException DE) { 181 throwNewXAException(DE); 182 } 183 184 if (rm == null) { 185 return false; 186 } 187 188 if (!(rm instanceof XAEnvironment)) { 189 return false; 190 } 191 192 return environmentImpl == 193 DbInternal.envGetEnvironmentImpl((XAEnvironment) rm); 194 } 195 196 200 public int prepare(Xid xid) 201 throws XAException { 202 203 if (DEBUG) { 204 System.out.println("*** prepare called"); 205 } 206 207 try { 208 checkEnv(); 209 Transaction txn = getXATransaction(xid); 210 if (txn == null) { 211 throw new XAException 212 ("No transaction found for " + xid + " during prepare."); 213 } 214 txn.getTxn().prepare(xid); 215 216 if (DEBUG) { 217 System.out.println("*** prepare returning XA_OK"); 218 } 219 220 return XAResource.XA_OK; 221 } catch (DatabaseException DE) { 222 throwNewXAException(DE); 223 } 224 return XAResource.XA_OK; } 226 227 231 public Xid [] recover(int flags) 232 throws XAException { 233 234 if (DEBUG) { 235 System.out.println("*** recover called"); 236 } 237 238 239 240 boolean tmStartRScan = (flags & XAResource.TMSTARTRSCAN) != 0; 241 boolean tmEndRScan = (flags & XAResource.TMENDRSCAN) != 0; 242 if ((tmStartRScan && tmEndRScan) || 243 (!tmStartRScan && !tmEndRScan && flags != TMNOFLAGS)) { 244 throw new XAException (XAException.XAER_INVAL); 245 } 246 247 251 try { 252 checkHandleIsValid(); 253 checkEnv(); 254 255 if (DEBUG) { 256 System.out.println("*** recover returning1"); 257 } 258 259 return environmentImpl.getTxnManager().XARecover(); 260 } catch (DatabaseException DE) { 261 throwNewXAException(DE); 262 } 263 return null; } 265 266 270 public void rollback(Xid xid) 271 throws XAException { 272 273 if (DEBUG) { 274 System.out.println("*** rollback called"); 275 } 276 277 try { 278 checkEnv(); 279 Transaction txn = getXATransaction(xid); 280 if (txn == null) { 281 throw new XAException 282 ("No transaction found for " + xid + " during abort."); 283 } 284 removeReferringHandle(txn); 285 txn.getTxn().abort(xid); 286 } catch (DatabaseException DE) { 287 throwNewXAException(DE); 288 } 289 290 if (DEBUG) { 291 System.out.println("*** rollback returning"); 292 } 293 } 294 295 299 public int getTransactionTimeout() 300 throws XAException { 301 302 try { 303 return (int) ((getConfig().getTxnTimeout() + 999999L) / 1000000L); 304 } catch (Exception DE) { 305 throwNewXAException(DE); 306 } 307 return 0; } 309 310 314 public boolean setTransactionTimeout(int timeout) 315 throws XAException { 316 317 return false; 318 } 319 320 324 public void start(Xid xid, int flags) 325 throws XAException { 326 327 if (DEBUG) { 328 System.out.println("*** start called " + xid + "/" + flags); 329 } 330 331 boolean tmJoin = (flags & XAResource.TMJOIN) != 0; 332 boolean tmResume = (flags & XAResource.TMRESUME) != 0; 333 334 335 if (xid == null || 336 (tmJoin && tmResume) || 337 (!tmJoin && 338 !tmResume && 339 flags != XAResource.TMNOFLAGS)) { 340 throw new XAException (XAException.XAER_INVAL); 341 } 342 343 try { 344 Transaction txn = getXATransaction(xid); 345 TxnManager txnMgr = environmentImpl.getTxnManager(); 346 347 if (flags == XAResource.TMNOFLAGS) { 348 349 353 if (txn == null) { 354 if (DEBUG) { 355 System.out.println 356 ("Transaction for XID " + xid + " being created"); 357 } 358 359 txn = beginTransaction(null, null); 360 setXATransaction(xid, txn); 361 362 } else { 363 throw new XAException (XAException.XAER_DUPID); 364 } 365 } else if (tmJoin) { 366 if (txn == null) { 367 throw new XAException (XAException.XAER_NOTA); 368 } 369 370 if (txnMgr.getTxnForThread() != null) { 371 throw new XAException (XAException.XAER_PROTO); 372 } 373 } else if (tmResume) { 374 if (txn == null) { 375 throw new XAException (XAException.XAER_NOTA); 376 } 377 378 if (!txn.getTxn().isSuspended()) { 379 throw new XAException (XAException.XAER_PROTO); 380 } 381 txn.getTxn().setSuspended(false); 382 } 383 384 if (DEBUG) { 385 System.out.println 386 ("Setting Transaction for " + Thread.currentThread()); 387 } 388 txnMgr.setTxnForThread(txn); 389 } catch (DatabaseException DE) { 390 if (DEBUG) { 391 System.out.println("*** start exception"); 392 } 393 throwNewXAException(DE); 394 } 395 396 if (DEBUG) { 397 System.out.println("*** start finished"); 398 } 399 } 400 401 private void throwNewXAException(Exception E) 402 throws XAException { 403 404 XAException ret = new XAException (E.toString()); 405 ret.initCause(E); 406 throw ret; 407 } 408 } 409 | Popular Tags |