1 22 package org.jboss.tm; 23 24 import java.util.HashMap ; 25 import java.util.Hashtable ; 26 import java.util.Map ; 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 import javax.naming.Name ; 30 import javax.naming.Reference ; 31 import javax.naming.spi.ObjectFactory ; 32 import javax.transaction.TransactionManager ; 33 import javax.transaction.xa.XAException ; 34 import org.jboss.logging.Logger; 35 import org.jboss.tm.integrity.TransactionIntegrityFactory; 36 import org.jboss.tm.recovery.RecoveryLogger; 37 38 51 public class TransactionManagerInitializer implements XAExceptionFormatter, ObjectFactory 52 { 53 private static Logger log = Logger.getLogger(TransactionManagerInitializer.class); 54 55 public static String JNDI_NAME = "java:/TransactionManager"; 57 public static String JNDI_IMPORTER = "java:/TransactionPropagationContextImporter"; 58 public static String JNDI_EXPORTER = "java:/TransactionPropagationContextExporter"; 59 60 62 private boolean globalIdsEnabled = false; 64 65 private boolean interruptThreads = false; 66 67 private int timeout = 300; 69 private int completionRetryLimit = 0; 70 71 private int completionRetryTimeout = 1; 72 73 private int xaRetryTimeout; 74 75 private int preparedTimeout; 76 77 private boolean rootBranchRemembersHeuristicDecisions = true; 78 79 private boolean reportHeuristicHazardAsHeuristicMixed = false; 80 81 private final Map xaExceptionFormatters = new HashMap (); 82 83 private RecoveryLogger recoveryLogger; 84 85 private XidFactoryBase xidFactory; 86 87 private TransactionIntegrityFactory integrityFactory; 88 89 private InitialContext initialContext; 90 91 protected Hashtable initialContextProperties; 92 93 95 static TxManager tm; 96 97 99 public void setInitialContextProperties(Hashtable initialContextProperties) 100 { 101 this.initialContextProperties = initialContextProperties; 102 } 103 104 105 public void start() throws Exception 106 { 107 if (initialContextProperties == null) initialContext = new InitialContext (); 108 else initialContext = new InitialContext (initialContextProperties); 109 110 TransactionImpl.xidFactory = xidFactory; 111 TransactionImpl.xaExceptionFormatter = this; 112 113 tm = TxManager.getInstance(); 115 tm.setDefaultTransactionTimeout(timeout); 117 tm.setCompletionRetryLimit(completionRetryLimit); 120 tm.setCompletionRetryTimeout(completionRetryTimeout); 121 tm.setXARetryTimeout(xaRetryTimeout); 124 tm.setPreparedTimeout(preparedTimeout); 125 tm.setRootBranchRemembersHeuristicDecisions( 128 rootBranchRemembersHeuristicDecisions); 129 tm.setReportHeuristicHazardAsHeuristicMixed( 130 reportHeuristicHazardAsHeuristicMixed); 131 tm.setGlobalIdsEnabled(globalIdsEnabled); 133 tm.setInterruptThreads(interruptThreads); 134 if (integrityFactory != null) 135 tm.setTransactionIntegrity(integrityFactory.createTransactionIntegrity()); 136 else 137 tm.setTransactionIntegrity(null); 138 139 bindRef(JNDI_NAME, "org.jboss.tm.TxManager"); 146 bindRef(JNDI_IMPORTER, "org.jboss.tm.TransactionPropagationContextImporter"); 147 bindRef(JNDI_EXPORTER, "org.jboss.tm.TransactionPropagationContextFactory"); 148 } 149 150 public void stop() 151 { 152 try 153 { 154 Context ctx = initialContext; 156 ctx.unbind(JNDI_NAME); 157 ctx.unbind(JNDI_IMPORTER); 158 ctx.unbind(JNDI_EXPORTER); 159 } 160 catch (Exception e) 161 { 162 log.error("Failed to clear JNDI bindings", e); 163 } 164 } 165 166 171 public void setRecoveryLogger(RecoveryLogger recoveryLogger) 172 { 173 this.recoveryLogger = recoveryLogger; 174 TxManager.getInstance().setRecoveryLogger(this.recoveryLogger); 176 } 177 178 179 184 public void setTransactionIntegrityFactory(TransactionIntegrityFactory factory) 185 { 186 this.integrityFactory = factory; 187 if (tm != null) 188 { 189 if (factory != null) 190 tm.setTransactionIntegrity(factory.createTransactionIntegrity()); 191 else 192 tm.setTransactionIntegrity(null); 193 } 194 } 195 196 201 public boolean getGlobalIdsEnabled() 202 { 203 return globalIdsEnabled; 204 } 205 206 211 public void setGlobalIdsEnabled(boolean newValue) 212 { 213 globalIdsEnabled = newValue; 214 if (tm != null) tm.setGlobalIdsEnabled(newValue); 216 } 217 218 223 public boolean isInterruptThreads() 224 { 225 return interruptThreads; 226 } 227 228 233 public void setInterruptThreads(boolean interruptThreads) 234 { 235 this.interruptThreads = interruptThreads; 236 if (tm != null) 237 tm.setInterruptThreads(interruptThreads); 238 } 239 240 245 public int getTransactionTimeout() 246 { 247 if (tm != null) timeout = tm.getDefaultTransactionTimeout(); 249 return timeout; 250 } 251 252 258 public void setTransactionTimeout(int timeout) 259 { 260 this.timeout = timeout; 261 if (tm != null) tm.setDefaultTransactionTimeout(timeout); 263 } 264 265 276 public void setCompletionRetryLimit(int maxCompletionRetries) 277 { 278 this.completionRetryLimit = maxCompletionRetries; 279 if (tm != null) tm.setCompletionRetryLimit(maxCompletionRetries); 281 } 282 283 294 public int getCompletionRetryLimit() 295 { 296 return completionRetryLimit; 297 } 298 299 315 public void setCompletionRetryTimeout(int seconds) 316 { 317 this.completionRetryTimeout = seconds; 318 if (tm != null) tm.setCompletionRetryTimeout(seconds); 320 } 321 322 338 public int getCompletionRetryTimeout() 339 { 340 return completionRetryTimeout; 341 } 342 343 349 public void setXARetryTimeout(int xaRetryTimeout) 350 { 351 this.xaRetryTimeout = xaRetryTimeout; 352 if (tm != null) tm.setXARetryTimeout(xaRetryTimeout); 354 } 355 356 361 public int getXARetryTimeout() 362 { 363 return xaRetryTimeout; 364 } 365 366 375 public void setPreparedTimeout(int preparedTimeout) 376 { 377 this.preparedTimeout = preparedTimeout; 378 if (tm != null) tm.setPreparedTimeout(preparedTimeout); 380 } 381 382 391 public int getPreparedTimeout() 392 { 393 return preparedTimeout; 394 } 395 396 407 public void setRootBranchRemembersHeuristicDecisions(boolean newValue) 408 { 409 this.rootBranchRemembersHeuristicDecisions = newValue; 410 if (tm != null) tm.setRootBranchRemembersHeuristicDecisions(newValue); 412 } 413 414 425 public boolean isRootBranchRemembersHeuristicDecisions() 426 { 427 return rootBranchRemembersHeuristicDecisions; 428 } 429 430 447 public void setReportHeuristicHazardAsHeuristicMixed(boolean newValue) 448 { 449 this.reportHeuristicHazardAsHeuristicMixed = newValue; 450 if (tm != null) tm.setReportHeuristicHazardAsHeuristicMixed(newValue); 452 453 } 454 455 471 boolean isReportHeuristicHazardAsHeuristicMixed() 472 { 473 return reportHeuristicHazardAsHeuristicMixed; 474 } 475 476 482 public XidFactoryBase getXidFactory() 483 { 484 return xidFactory; 485 } 486 487 492 public void setXidFactory(XidFactoryBase xidFactory) 493 { 494 this.xidFactory = xidFactory; 495 } 496 497 498 504 public TransactionManager getTransactionManager() 505 { 506 return tm; 507 } 508 509 514 public JBossXATerminator getXATerminator() 515 { 516 return tm; 517 } 518 519 525 public long getTransactionCount() 526 { 527 return tm.getTransactionCount(); 528 } 529 534 public long getCommitCount() 535 { 536 return tm.getCommitCount(); 537 } 538 543 public long getRollbackCount() 544 { 545 return tm.getRollbackCount(); 546 } 547 548 553 public String listInDoubtTransactions() 554 { 555 return tm.listInDoubtTransactions(); 556 } 557 558 564 public void heuristicallyCommit(long localTransactionId) 565 { 566 tm.heuristicallyCommit(localTransactionId); 567 } 568 569 572 public void heuristicallyCommitAll() 573 { 574 tm.heuristicallyCommitAll(); 575 } 576 577 583 public void heuristicallyRollback(long localTransactionId) 584 { 585 tm.heuristicallyRollback(localTransactionId); 586 } 587 588 591 public void heuristicallyRollbackAll() 592 { 593 tm.heuristicallyRollbackAll(); 594 } 595 596 608 public String listHeuristicallyCompletedTransactions() 609 { 610 return tm.listHeuristicallyCompletedTransactions(); 611 } 612 613 621 public void forget(long localTransactionId) 622 { 623 tm.forget(localTransactionId); 624 } 625 626 630 public void forgetAll() 631 { 632 tm.forgetAll(); 633 } 634 635 642 public void registerXAExceptionFormatter(Class clazz, XAExceptionFormatter formatter) 643 { 644 xaExceptionFormatters.put(clazz, formatter); 645 } 646 647 653 public void unregisterXAExceptionFormatter(Class clazz) 654 { 655 xaExceptionFormatters.remove(clazz); 656 } 657 658 public void formatXAException(XAException xae, Logger log) 659 { 660 Class clazz = xae.getClass(); 661 while (clazz != XAException .class) 662 { 663 XAExceptionFormatter formatter = (XAExceptionFormatter) xaExceptionFormatters.get(clazz); 664 if (formatter != null) 665 { 666 formatter.formatXAException(xae, log); 667 return; 668 } clazz = clazz.getSuperclass(); 670 } 671 } 672 673 675 public Object getObjectInstance(Object obj, Name name, 676 Context nameCtx, Hashtable environment) 677 throws Exception 678 { 679 return tm; 681 } 682 683 684 686 private void bindRef(String jndiName, String className) 687 throws Exception 688 { 689 Reference ref = new Reference (className, getClass().getName(), null); 690 initialContext.bind(jndiName, ref); 691 } 692 } 693 | Popular Tags |