1 23 24 28 29 package com.sun.jts.jta; 30 31 import java.util.*; 32 import javax.transaction.*; 33 import org.omg.CosTransactions.*; 34 import org.omg.CORBA.*; 35 import org.omg.CORBA.ORBPackage.InvalidName ; 36 37 import com.sun.jts.CosTransactions.*; 38 import com.sun.jts.otsidl.*; 39 40 import javax.transaction.SystemException ; 41 import javax.transaction.Synchronization ; 42 import org.omg.CosTransactions.Status; 43 import org.omg.CosTransactions.Current; 44 import org.omg.CosTransactions.NoTransaction; 45 import org.omg.CosTransactions.HeuristicMixed; 46 import org.omg.CosTransactions.HeuristicHazard; 47 import com.sun.jts.CosTransactions.GlobalTID; 48 49 import javax.transaction.xa.Xid ; 50 import javax.resource.spi.work.WorkException ; 51 import javax.resource.spi.work.WorkCompletedException ; 52 53 import javax.transaction.xa.XAException ; 54 55 import java.util.logging.Logger ; 56 import java.util.logging.Level ; 57 import com.sun.logging.LogDomains; 58 import com.sun.jts.utils.LogFormatter; 59 66 public class TransactionManagerImpl implements TransactionManager { 67 68 71 static private TransactionManagerImpl tm = null; 72 73 76 private Current current; 77 78 81 83 86 static private HashMap statusMap; 87 static private int[] directLookup; 88 static final int maxStatus; 89 90 93 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 94 95 97 100 static private int xaTimeOut = 0; 101 103 static private Status CosTransactionStatus[] = 104 { 105 org.omg.CosTransactions.Status.StatusActive, 106 org.omg.CosTransactions.Status.StatusMarkedRollback, 107 org.omg.CosTransactions.Status.StatusPrepared, 108 org.omg.CosTransactions.Status.StatusCommitted, 109 org.omg.CosTransactions.Status.StatusRolledBack, 110 org.omg.CosTransactions.Status.StatusUnknown, 111 org.omg.CosTransactions.Status.StatusNoTransaction, 112 org.omg.CosTransactions.Status.StatusPreparing, 113 org.omg.CosTransactions.Status.StatusCommitting, 114 org.omg.CosTransactions.Status.StatusRollingBack 115 }; 116 117 static private int JTAStatus[] = 118 { 119 javax.transaction.Status.STATUS_ACTIVE, 120 javax.transaction.Status.STATUS_MARKED_ROLLBACK, 121 javax.transaction.Status.STATUS_PREPARED, 122 javax.transaction.Status.STATUS_COMMITTED, 123 javax.transaction.Status.STATUS_ROLLEDBACK, 124 javax.transaction.Status.STATUS_UNKNOWN, 125 javax.transaction.Status.STATUS_NO_TRANSACTION, 126 javax.transaction.Status.STATUS_PREPARING, 127 javax.transaction.Status.STATUS_COMMITTING, 128 javax.transaction.Status.STATUS_ROLLING_BACK 129 }; 130 131 static { 133 statusMap = new HashMap(); 134 int calcMaxStatus = 0; 135 for (int i=0; i<CosTransactionStatus.length; i++) { 136 statusMap.put(CosTransactionStatus[i], 137 new Integer (JTAStatus[i])); 138 calcMaxStatus = Math.max(calcMaxStatus, CosTransactionStatus[i].value()); 139 } 140 maxStatus = calcMaxStatus; 141 directLookup = new int[maxStatus + 1]; 142 for (int i=0; i < directLookup.length; i++) { 143 directLookup[i] = javax.transaction.Status.STATUS_UNKNOWN; 145 } 146 for (int i=0; i < CosTransactionStatus.length; i++) { 147 int statusVal = CosTransactionStatus[i].value(); 148 if (statusVal < 0) { 149 _logger.log(Level.SEVERE, "A negative CosTransaction Status value was detected."); 150 } else { 151 directLookup[statusVal] = JTAStatus[i]; 152 } 153 } 154 155 } 156 157 160 static synchronized 161 public TransactionManagerImpl getTransactionManagerImpl() { 162 if (tm == null) { 163 tm = new TransactionManagerImpl(); 164 } 165 return tm; 166 } 167 168 171 private TransactionManagerImpl() { 172 try { 173 ORB orb = Configuration.getORB(); 174 current = org.omg.CosTransactions.CurrentHelper. 175 narrow(orb.resolve_initial_references("TransactionCurrent")); 176 } catch (InvalidName inex) { 178 _logger.log(Level.SEVERE, 179 "jts.unexpected_error_in_create_transaction_manager",inex); 180 } catch (Exception ex) { 181 _logger.log(Level.SEVERE, 182 "jts.unexpected_error_in_create_transaction_manager",ex); 183 } 184 } 185 186 197 static public void initJTSProperties(Properties props, String logDir, 198 boolean trace, String traceDir) { 199 if (traceDir == null) traceDir = "."; 200 if (logDir == null) logDir = "."; 201 202 props.put("com.sun.corba.se.CosTransactions.ORBJTSClass", 203 "com.sun.jts.CosTransactions.DefaultTransactionService"); 204 props.put("com.sun.jts.traceDirectory", traceDir); 205 props.put("com.sun.jts.logDirectory", logDir); 206 if (trace) { 207 props.put("com.sun.jts.trace", "true"); 208 } 209 } 210 211 215 static public int mapStatus(Status status) { 216 int statusVal = status.value(); 217 if (statusVal < 0 || statusVal > maxStatus) { 218 return javax.transaction.Status.STATUS_UNKNOWN; 219 } else { 220 return directLookup[statusVal]; 221 } 222 } 223 224 230 public void begin() 231 throws NotSupportedException, SystemException { 232 233 try { 234 if (current.get_control() != null) { 236 throw new NotSupportedException(); 237 } 238 current.begin(); 239 } catch (TRANSACTION_ROLLEDBACK ex) { 240 throw new NotSupportedException(); 241 } catch (SubtransactionsUnavailable ex) { 242 throw new SystemException (); 243 } 244 } 245 246 254 public void begin(int timeout) 255 throws NotSupportedException, SystemException { 256 try { 257 if (current.get_control() != null) { 259 throw new NotSupportedException(); 260 } 261 ((com.sun.jts.CosTransactions.CurrentImpl)current).begin(timeout); 262 } catch (TRANSACTION_ROLLEDBACK ex) { 263 throw new NotSupportedException(); 264 } catch (SubtransactionsUnavailable ex) { 265 throw new SystemException (); 266 } 267 } 268 270 291 public void commit() throws RollbackException, 292 HeuristicMixedException, HeuristicRollbackException, SecurityException , 293 IllegalStateException , SystemException { 294 295 try { 296 current.commit(true); 297 } catch (TRANSACTION_ROLLEDBACK ex) { 298 throw new RollbackException(); 299 } catch (NoTransaction ex) { 300 throw new IllegalStateException (); 301 } catch (NO_PERMISSION ex) { 302 throw new SecurityException (); 303 } catch (HeuristicMixed ex) { 304 throw new HeuristicMixedException(); 305 } catch (HeuristicHazard ex) { 306 throw new HeuristicRollbackException(); 307 } catch (Exception ex) { 308 throw new SystemException (ex.toString()); 309 } 310 315 } 316 317 327 public void rollback() 328 throws IllegalStateException , SecurityException , SystemException { 329 330 try { 331 current.rollback(); 332 } catch (NoTransaction ex) { 333 throw new IllegalStateException (); 334 } catch (NO_PERMISSION ex) { 335 throw new SecurityException (); 336 } catch (Exception ex) { 337 throw new SystemException (ex.toString()); 338 } 339 340 345 } 346 347 355 public void setRollbackOnly() 356 throws IllegalStateException , SystemException { 357 358 try { 359 current.rollback_only(); 360 } catch (NoTransaction ex) { 361 throw new IllegalStateException (); 362 } catch (Exception ex) { 363 throw new SystemException (ex.toString()); 364 } 365 } 366 367 374 public int getStatus() throws SystemException { 375 try { 376 Status status = current.get_status(); 377 return mapStatus(status); 378 } catch (Exception ex) { 379 throw new SystemException (ex.toString()); 380 } 381 } 382 383 398 public synchronized void setTransactionTimeout(int seconds) 399 throws SystemException { 400 401 try { 402 if (seconds < 0) { 403 String msg = LogFormatter.getLocalizedMessage(_logger, 404 "jts.invalid_timeout"); 405 throw new SystemException (msg); 406 } 407 current.set_timeout(seconds); 408 } catch (Exception ex) { 409 throw new SystemException (ex.toString()); 410 } 411 } 412 413 417 public Transaction getTransaction() 418 throws SystemException { 419 420 try { 421 Control control = current.get_control(); 422 if (control == null) { 423 return null; 424 } else { 425 return createTransactionImpl(control); 426 } 427 } catch (Unavailable uex) { 428 throw new SystemException (uex.toString()); 429 } catch (Exception ex) { 430 throw new SystemException (ex.toString()); 431 } 432 } 433 434 440 public void resume(Transaction suspended) throws 441 InvalidTransactionException, IllegalStateException , SystemException { 442 if (getTransaction() != null) throw new IllegalStateException (); 444 if (suspended == null) throw new InvalidTransactionException(); 446 if ((suspended instanceof TransactionImpl) == false) { 447 throw new InvalidTransactionException(); 448 } 449 Control control = ((TransactionImpl) suspended).getControl(); 450 try { 451 current.resume(control); 452 } catch (InvalidControl ex) { 453 throw new InvalidTransactionException(); 455 } catch (Exception ex) { 456 throw new SystemException (ex.toString()); 457 } 458 } 459 460 461 469 public Transaction suspend() throws SystemException { 470 try { 471 Control control = current.suspend(); 472 if (control == null) return null; 473 return createTransactionImpl(control); 474 } catch (Unavailable uex) { 475 throw new SystemException (uex.toString()); 476 } catch (Exception ex) { 477 throw new SystemException (ex.toString()); 478 } 479 } 480 481 517 518 private Transaction createTransactionImpl(Control control) 519 throws Unavailable, SystemException 520 { 521 GlobalTID gtid = null; 522 if (Configuration.isLocalFactory()) { 523 gtid = ((ControlImpl) control).getGlobalTID(); 524 } else { 525 ControlImpl cntrlImpl = ControlImpl.servant(JControlHelper.narrow(control)); 526 gtid = cntrlImpl.getGlobalTID(); 527 } 528 529 return new TransactionImpl(control, gtid); 531 } 532 533 539 public static void recover(Enumeration xaResourceList) { 540 RecoveryManager.recoverXAResources(xaResourceList); 541 } 542 543 550 public static void recreate(Xid xid, long timeout) throws WorkException { 551 552 if (xid == null || xid.getFormatId() == 0 || 554 xid.getBranchQualifier() == null || 555 xid.getGlobalTransactionId() == null) { 556 WorkException workExc = new WorkCompletedException ("Invalid Xid"); 557 workExc.setErrorCode(WorkException.TX_RECREATE_FAILED); 558 throw workExc; 559 } 560 561 if (!DefaultTransactionService.isActive()) { 563 WorkException workExc = 564 new WorkCompletedException ("Transaction Manager unavailable"); 565 workExc.setErrorCode(WorkException.TX_RECREATE_FAILED); 566 throw workExc; 567 } 568 569 GlobalTID tid = new GlobalTID(xid); 571 try { 572 CurrentTransaction.recreate( 573 tid, (int) ((timeout <= 0) ? 0 : timeout)); 574 } catch (Throwable exc) { 575 String errorCode = WorkException.TX_RECREATE_FAILED; 576 if (exc instanceof INVALID_TRANSACTION && 577 (((INVALID_TRANSACTION) exc).minor == 578 MinorCode.TX_CONCURRENT_WORK_DISALLOWED)) { 579 errorCode = WorkException.TX_CONCURRENT_WORK_DISALLOWED; 580 } 581 WorkException workExc = new WorkCompletedException (exc); 582 workExc.setErrorCode(errorCode); 583 throw workExc; 584 } 585 } 586 587 593 public static void release(Xid xid) throws WorkException { 594 595 GlobalTID tid = new GlobalTID(xid); 596 try { 597 CurrentTransaction.release(tid); 598 } catch (Throwable exc) { 599 String errorCode = WorkException.UNDEFINED; 600 if (exc instanceof INTERNAL) { 601 errorCode = WorkException.INTERNAL; 602 } 603 WorkException workExc = new WorkCompletedException (exc); 604 workExc.setErrorCode(errorCode); 605 throw workExc; 606 } 607 } 608 609 616 public static javax.resource.spi.XATerminator getXATerminator() { 617 return new XATerminatorImpl(); 618 } 619 620 624 static private void assert_prejdk14(boolean value) { 625 if (!value) { 626 Exception e = new Exception (); 627 _logger.log(Level.WARNING,"jts.assert",e); 628 } 629 } 630 631 632 636 public static void setXAResourceTimeOut(int value){ 637 xaTimeOut = value; 638 } 639 640 public static int getXAResourceTimeOut(){ 641 return xaTimeOut; 642 } 643 671 672 677 } 678 679 | Popular Tags |