1 23 package com.sun.enterprise.distributedtx; 24 25 import java.util.*; 26 import java.rmi.RemoteException ; 27 import javax.transaction.*; 28 import javax.transaction.xa.*; 29 30 import com.sun.enterprise.*; 31 import com.sun.ejb.*; 32 import com.sun.enterprise.resource.*; 33 import com.sun.enterprise.log.Log; 34 35 import javax.servlet.Servlet ; 36 import javax.servlet.Filter ; 37 import javax.servlet.SingleThreadModel ; 38 import javax.ejb.EnterpriseBean ; 39 40 import javax.resource.spi.XATerminator ; 41 import javax.resource.spi.work.WorkException ; 42 43 44 import com.sun.enterprise.config.ConfigContext; 46 import com.sun.enterprise.config.ConfigBean; 47 import com.sun.enterprise.config.serverbeans.ServerBeansFactory; 48 import com.sun.enterprise.config.serverbeans.Config; 49 import com.sun.enterprise.server.ApplicationServer; 50 import com.sun.enterprise.server.ServerContext; 51 import com.sun.enterprise.config.ConfigException; 52 import com.sun.enterprise.config.serverbeans.TransactionService; 53 import com.sun.enterprise.config.serverbeans.ElementProperty; 54 import com.sun.enterprise.transaction.TransactionAdminBean; 55 import com.sun.enterprise.transaction.JTSConfigChangeEventListener; 56 import com.sun.enterprise.admin.event.tx.JTSEvent; 57 import com.sun.enterprise.util.i18n.StringManager; 58 60 import com.sun.jts.jta.TransactionManagerImpl; 61 62 import java.util.logging.*; 64 import com.sun.logging.*; 65 67 import com.sun.appserv.util.cache.Cache; 69 import com.sun.appserv.util.cache.BaseCache; 70 72 73 import com.sun.ejb.ComponentContext; 74 75 76 85 public class J2EETransactionManagerImpl implements J2EETransactionManager { 86 87 static Logger _logger=LogDomains.getLogger(LogDomains.JTA_LOGGER); 89 91 private static final String TX_TIMEOUT = "transaction.timeout"; 92 private static final String TX_OPT = "transaction.nonXA.optimization"; 93 94 private TransactionManager tm; 96 97 private boolean multipleEnlistDelists = false; 102 private static StringManager sm = StringManager.getManager(J2EETransactionManagerImpl.class); 104 protected InvocationManager invMgr; 105 106 protected PoolManager poolmgr; 107 108 114 protected int transactionTimeout; 115 protected ThreadLocal <Integer > txnTmout = new ThreadLocal (); 116 protected boolean useLAO = true; 117 118 protected Hashtable statusMap; 121 protected Vector activeTransactions; 122 protected boolean monitoringEnabled = false; 123 124 static protected int JTAStatus[] = 125 { 126 javax.transaction.Status.STATUS_ACTIVE, 127 javax.transaction.Status.STATUS_MARKED_ROLLBACK, 128 javax.transaction.Status.STATUS_PREPARED, 129 javax.transaction.Status.STATUS_COMMITTED, 130 javax.transaction.Status.STATUS_ROLLEDBACK, 131 javax.transaction.Status.STATUS_UNKNOWN, 132 javax.transaction.Status.STATUS_NO_TRANSACTION, 133 javax.transaction.Status.STATUS_PREPARING, 134 javax.transaction.Status.STATUS_COMMITTING, 135 javax.transaction.Status.STATUS_ROLLING_BACK 136 }; 137 138 static protected String STATUS[] = 139 { 140 "Active", 141 "MarkedRollback", 142 "Prepared", 143 "Committed", 144 "RolledBack", 145 "UnKnown", 146 "NoTransaction", 147 "Preparing", 148 "Committing", 149 "RollingBack" 150 }; 151 protected int m_transCommitted = 0; 152 protected int m_transRolledback = 0; 153 protected int m_transInFlight = 0; 154 156 163 private Cache resourceTable; 165 167 168 169 public J2EETransactionManagerImpl() { 170 if (_logger.isLoggable(Level.FINE)) 171 _logger.log(Level.FINE,"TM: Initializing distributed TM..."); 172 if( Switch.getSwitch().getPoolManager() == null ) { 174 poolmgr = new PoolManagerImpl(); 175 Switch.getSwitch().setPoolManager( poolmgr ); 176 } else { 177 poolmgr = Switch.getSwitch().getPoolManager(); 178 } 179 Switch.getSwitch().setPoolManager(poolmgr); 180 181 ResourceInstaller installer = new ResourceInstaller(); 182 Switch.getSwitch().setResourceInstaller(installer); 183 184 int maxEntries = 8192; float loadFactor = 0.75f; try { 190 String mEnlistDelists 191 = System.getProperty("ALLOW_MULTIPLE_ENLISTS_DELISTS"); 192 if ("true".equals(mEnlistDelists)) { 193 multipleEnlistDelists = true; 194 if (_logger.isLoggable(Level.FINE)) 195 _logger.log(Level.FINE,"TM: multiple enlists, delists are enabled"); 196 } 197 String maxEntriesValue 198 = System.getProperty("JTA_RESOURCE_TABLE_MAX_ENTRIES"); 199 if (maxEntriesValue != null) { 200 int temp = Integer.parseInt(maxEntriesValue); 201 if (temp > 0) { 202 maxEntries = temp; 203 } 204 } 205 String loadFactorValue 206 = System.getProperty("JTA_RESOURCE_TABLE_DEFAULT_LOAD_FACTOR"); 207 if (loadFactorValue != null) { 208 float f = Float.parseFloat(loadFactorValue); 209 if (f > 0) { 210 loadFactor = f; 211 } 212 } 213 } catch (Exception ex) { 214 } 216 Properties cacheProps = null; 217 218 resourceTable = new BaseCache(); 219 ((BaseCache)resourceTable).init(maxEntries, loadFactor, cacheProps); 220 222 invMgr = Switch.getSwitch().getInvocationManager(); 223 225 ServerContext sCtx = ApplicationServer.getServerContext(); 226 if (sCtx != null) { 228 ConfigContext ctx = sCtx.getConfigContext(); 229 TransactionService txnService = null; 230 try { 231 txnService = ServerBeansFactory.getTransactionServiceBean(ctx); 232 transactionTimeout = Integer.parseInt(txnService.getTimeoutInSeconds()); 233 ElementProperty[] eprops = txnService.getElementProperty(); 234 for (int index = 0; index < eprops.length; index++) { 235 if ("use-last-agent-optimization".equals(eprops[index].getName())) { 236 if ("false".equals(eprops[index].getValue())) { 237 useLAO = false; 238 if (_logger.isLoggable(Level.FINE)) 239 _logger.log(Level.FINE,"TM: LAO is disabled"); 240 } 241 } 242 } 243 } catch(ConfigException e) { 244 throw new RuntimeException (sm.getString("enterprise_distributedtx.config_excep",e)); 245 } catch (NumberFormatException ex) { 246 } 247 } 248 if (_logger.isLoggable(Level.FINE)) 250 _logger.log(Level.FINE,"TM: Tx Timeout = " + transactionTimeout); 251 252 activeTransactions = new Vector(); 254 statusMap = new Hashtable(); 255 for (int i=0; i<JTAStatus.length; i++) { 256 statusMap.put(new Integer (JTAStatus[i]),STATUS[i]); 257 } 258 260 try { 262 String doStats 263 = System.getProperty("MONITOR_JTA_RESOURCE_TABLE_STATISTICS"); 264 if (Boolean.getBoolean(doStats)) { 265 registerStatisticMonitorTask(); 266 } 267 } catch (Exception ex) { 268 } 270 } 272 273 public static J2EETransactionManager createTransactionManager() { 274 ServerConfiguration sc = ServerConfiguration.getConfiguration(); 275 String txOpt = sc.getProperty(TX_OPT); 276 277 if (_logger.isLoggable(Level.FINE)) 278 _logger.log(Level.FINE,"TM: Tx Opt = " + txOpt); 279 280 J2EETransactionManager j2eeTM; 281 if ( txOpt != null && txOpt.equals("false") ) 282 j2eeTM = new J2EETransactionManagerImpl(); 283 else { 284 j2eeTM = new J2EETransactionManagerOpt(); 285 J2EETransaction.j2eeTM = (J2EETransactionManagerOpt)j2eeTM; 286 } 287 288 return j2eeTM; 289 } 290 291 public static void createJTSTransactionManager() { 292 J2EETransactionManagerImpl impl = (J2EETransactionManagerImpl) 295 Switch.getSwitch().getTransactionManager(); 296 297 impl.tm = TransactionManagerImpl.getTransactionManagerImpl(); 298 299 } 300 301 private static void print(String s) { 302 305 _logger.log(Level.FINE,s); 307 309 } 310 311 312 313 314 315 316 323 public boolean isNullTransaction() 324 { 325 try { 326 return com.sun.jts.pi.InterceptorImpl.isTxCtxtNull(); 327 } catch ( Exception ex ) { 328 return false; 331 } 332 } 333 334 public void recover(XAResource[] resourceList) { 335 int size = resourceList.length; 336 Vector v = new Vector(); 337 for (int i=0; i<size; i++) { 338 v.addElement(resourceList[i]); 339 } 340 ((TransactionManagerImpl)tm).recover(v.elements()); 341 } 342 343 public boolean enlistResource(Transaction tran, ResourceHandle h) 344 throws RollbackException, 345 IllegalStateException , SystemException { 346 if (_logger.isLoggable(Level.FINE)) 347 _logger.log(Level.FINE,"TM: enlistResource"); 348 if (h.isTransactional() && (!h.isEnlisted() || !h.isShareable() || multipleEnlistDelists)) { 349 XAResource res = h.getXAResource(); 350 boolean result = tran.enlistResource(res); 351 if (!h.isEnlisted()) 352 poolmgr.resourceEnlisted(tran, h); 353 return result; 354 } else { 355 return true; 356 } 357 } 358 359 public boolean enlistLAOResource(Transaction tran, ResourceHandle h) 360 throws RollbackException, 361 IllegalStateException , SystemException { 362 if (_logger.isLoggable(Level.FINE)) 363 _logger.log(Level.FINE,"TM: enlistLAOResource"); 364 if (h.isTransactional()) { 365 XAResource res = h.getXAResource(); 366 boolean result = tran.enlistResource(res); 367 return result; 368 } else { 369 return true; 370 } 371 } 372 373 public void enlistComponentResources() throws RemoteException { 374 if (_logger.isLoggable(Level.FINE)) 375 _logger.log(Level.FINE,"TM: enlistComponentResources"); 376 ComponentInvocation inv = invMgr.getCurrentInvocation(); 377 if (inv == null) { 379 throw new InvocationException(); 380 } 381 try { 383 Transaction tran = getTransaction(); 384 inv.setTransaction(tran); 385 enlistComponentResources(inv); 386 } catch (InvocationException ex) { 387 391 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_enlist" ,ex); 393 395 throw new RemoteException (ex.getMessage(), ex.getNestedException()); 396 } catch (Exception ex) { 397 401 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_enlist" ,ex); 403 throw new RemoteException (ex.getMessage(), ex); 405 } 406 } 407 408 public void delistComponentResources(boolean suspend) 409 throws RemoteException { 410 if (_logger.isLoggable(Level.FINE)) 411 _logger.log(Level.FINE,"TM: delistComponentResources"); 412 ComponentInvocation inv = invMgr.getCurrentInvocation(); 413 if (inv == null) { 415 throw new InvocationException(); 416 } 417 try { 419 delistComponentResources(inv, suspend); 420 } catch (InvocationException ex) { 421 425 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_delist",ex); 427 429 throw new RemoteException ("", ex.getNestedException()); 430 } catch (Exception ex) { 431 435 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_delist",ex); 437 throw new RemoteException ("", ex); 439 } 440 } 441 442 public void registerComponentResource(ResourceHandle h) { 443 ComponentInvocation inv = invMgr.getCurrentInvocation(); 444 if (inv != null) { 445 Object instance = inv.getInstance(); 446 if (instance == null) return; 447 h.setComponentInstance(instance); 448 List l = getResourceList(instance, inv); 449 l.add(h); 450 } else { 451 throw new InvocationException(); 452 } 453 } 454 455 public void unregisterComponentResource(ResourceHandle h) { 456 Object instance = h.getComponentInstance(); 457 if (instance == null) return; 458 h.setComponentInstance(null); 459 ComponentInvocation inv = invMgr.getCurrentInvocation(); 460 List l = null; 461 if (inv != null) 462 l = getExistingResourceList(instance, inv); 463 else 464 l = getExistingResourceList(instance); 465 if (l != null) { 466 l.remove(h); 467 } 468 } 469 470 private void handleResourceError(ResourceHandle h, 471 Exception ex, Transaction tran, 472 ComponentInvocation inv) { 473 474 if (_logger.isLoggable(Level.FINE)) { 475 if (h.isTransactional()) { 476 _logger.log(Level.FINE,"TM: HandleResourceError " + 477 h.getXAResource() + 478 "," + ex); 479 } 480 } 481 489 try { 490 if (tran != null && h.isTransactional() && h.isEnlisted() ) { 491 tran.delistResource(h.getXAResource(), XAResource.TMSUCCESS); 492 } 493 } catch (Exception ex2) { 494 } 496 497 498 if (ex instanceof RollbackException) { 499 return; 501 } else if (ex instanceof IllegalStateException ) { 502 try { 505 h.getResourceAllocator().closeUserConnection(h); 506 } catch (Exception ex2) { 507 } 509 } else { 510 try { 512 h.getResourceAllocator().destroyResource(h); 513 } catch (Exception ex2) { 514 } 516 } 517 } 518 519 private void enlistComponentResources(ComponentInvocation inv) 520 throws InvocationException { 521 522 try { 524 Transaction tran = inv.getTransaction(); 525 if (isTransactionActive(tran)) { 526 538 539 List l = getResourceList(inv.getInstance(), inv); 540 Iterator it = l.iterator(); 541 while(it.hasNext()) { 543 ResourceHandle h = (ResourceHandle) it.next(); 544 try{ 545 enlistResource(tran,h); 546 }catch(Exception ex){ 547 it.remove(); 549 handleResourceError(h,ex,tran,inv); 550 } 551 } 552 } 554 } catch (Exception ex) { 555 559 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_enlist",ex); 561 } 564 571 } 572 573 private void delistComponentResources(ComponentInvocation inv, 574 boolean suspend) 575 throws InvocationException { 576 577 try { 578 Transaction tran = inv.getTransaction(); 579 if (isTransactionActive(tran)) { 580 596 597 List l = getExistingResourceList(inv.getInstance(), inv); 598 if (l == null) 599 l = new ArrayList(0); 600 Iterator it = l.iterator(); 601 int flag = XAResource.TMSUCCESS; 603 if(suspend)flag = XAResource.TMSUSPEND; 604 while(it.hasNext()){ 605 ResourceHandle h = (ResourceHandle)it.next(); 606 try{ 607 if ( h.isEnlisted() ) { 608 delistResource(tran,h,flag); 609 } 610 } catch (IllegalStateException ex) { 611 }catch(Exception ex){ 613 it.remove(); 614 handleResourceError(h,ex,tran,inv); 615 } 616 } 617 } 619 } catch (Exception ex) { 620 624 _logger.log(Level.SEVERE,"enterprise_distributedtx.excep_in_delist",ex); 626 } 629 } 630 631 private boolean isTransactionActive(Transaction tran) { 632 return (tran != null); 633 } 634 635 636 public void preInvoke(ComponentInvocation prev) 637 throws InvocationException { 638 if ( prev != null && prev.getTransaction() != null && 639 prev.isTransactionCompleting() == false) { 640 delistComponentResources(prev, true); } 644 645 } 646 647 648 public void postInvoke(ComponentInvocation curr, ComponentInvocation prev) 649 throws InvocationException { 650 651 if ( curr != null && curr.getTransaction() != null ) 652 delistComponentResources(curr, false); if ( prev != null && prev.getTransaction() != null && 654 prev.isTransactionCompleting() == false) { 655 enlistComponentResources(prev); 658 } 659 660 } 661 662 663 public void componentDestroyed(Object instance) { 664 if (_logger.isLoggable(Level.FINE)) 665 _logger.log(Level.FINE,"TM: componentDestroyed" + instance); 666 List l = (List)resourceTable.get(getInstanceKey(instance)); 669 if (l != null) { 670 resourceTable.remove(getInstanceKey(instance)); 672 Iterator it = l.iterator(); 674 while (it.hasNext()) { 675 ResourceHandle h = (ResourceHandle) it.next(); 676 try { 677 h.getResourceAllocator().closeUserConnection(h); 678 } catch (PoolingException ex) { 679 682 if (_logger.isLoggable(Level.FINE)) 684 _logger.log(Level.WARNING,"enterprise_distributedtx.pooling_excep", ex); 685 } 687 } 688 l.clear(); 689 } 693 } 695 696 public void ejbDestroyed(ComponentContext context) { 697 if (_logger.isLoggable(Level.FINE)) 698 _logger.log(Level.FINE, " ejbDestroyed: " + context); 699 List l = (List)context.getResourceList(); 700 if (l != null) { 701 Iterator it = l.iterator(); 702 while (it.hasNext()) { 703 ResourceHandle h = (ResourceHandle) it.next(); 704 try { 705 h.getResourceAllocator().closeUserConnection(h); 706 } catch (PoolingException ex) { 707 if (_logger.isLoggable(Level.FINE)) 708 _logger.log(Level.WARNING,"enterprise_distributedtx.pooling_excep", ex); 709 } 710 } 711 l.clear(); 712 } 713 } 714 715 716 717 private Object getInstanceKey(Object instance) { 718 Object key = null; 719 if (instance instanceof Servlet || 720 instance instanceof Filter ) { 721 if (instance instanceof SingleThreadModel ) { 723 key = instance; 724 } else { 725 Vector pair = new Vector(2); 726 pair.addElement(instance); 727 pair.addElement(Thread.currentThread()); 728 key = pair; 729 } 730 } else { 731 key = instance; 732 } 733 return key; 734 } 735 736 public List getExistingResourceList(Object instance) { 738 Object key = getInstanceKey(instance); 739 return (List) resourceTable.get(key); 740 } 741 743 public List getExistingResourceList(Object instance, ComponentInvocation inv) { 744 List l = null; 745 if (inv.getInvocationType() == ComponentInvocation.EJB_INVOCATION) { 746 ComponentContext ctx = inv.context; 747 if (ctx != null) 748 l = ctx.getResourceList(); 749 return l; 750 } 751 else { 752 Object key = getInstanceKey(instance); 753 return (List) resourceTable.get(key); 754 } 755 } 756 757 public List getResourceList(Object instance) { 759 Object key = getInstanceKey(instance); 760 List l = (List) resourceTable.get(key); 761 if (l == null) { 762 l = new ArrayList(); resourceTable.put(key, l); 764 } 765 return l; 766 } 767 769 public List getResourceList(Object instance, ComponentInvocation inv) { 770 List l = null; 771 if (inv.getInvocationType() == ComponentInvocation.EJB_INVOCATION) { 772 ComponentContext ctx = inv.context; 773 if (ctx != null) 774 l = ctx.getResourceList(); 775 else { 776 l = new ArrayList(0); 777 } 778 } 779 else { 780 Object key = getInstanceKey(instance); 781 l = (List) resourceTable.get(key); 782 if (l == null) { 783 l = new ArrayList(); resourceTable.put(key, l); 785 } 786 } 787 return l; 788 } 789 790 791 792 796 public boolean delistResource(Transaction tran, ResourceHandle h, 797 int flag) 798 throws IllegalStateException , SystemException { 799 if (_logger.isLoggable(Level.FINE)) 800 _logger.log(Level.FINE,"TM: delistResource"); 801 if (!h.isShareable() || multipleEnlistDelists) { 802 if (h.isTransactional() && h.isEnlisted()) { 803 return tran.delistResource(h.getXAResource(), flag); 804 } else { 805 return true; 806 } 807 } 808 return true; 809 } 810 811 public void registerSynchronization(Synchronization sync) 812 throws IllegalStateException , SystemException 813 { 814 if (_logger.isLoggable(Level.FINE)) 815 _logger.log(Level.FINE,"TM: registerSynchronization"); 816 try { 817 Transaction tran = getTransaction(); 818 if (tran != null) { 819 tran.registerSynchronization(sync); 820 } 821 } catch (RollbackException ex) { 822 826 _logger.log(Level.SEVERE,"enterprise_distributedtx.rollbackexcep_in_regsynch",ex); 828 830 throw new IllegalStateException (); 831 } 832 } 833 834 public void begin(int timeout) 835 throws NotSupportedException, SystemException { 836 837 858 859 ((TransactionManagerImpl)tm).begin(timeout); 860 if (monitoringEnabled) { 862 Transaction tran = tm.getTransaction(); 863 activeTransactions.addElement(tran); 864 m_transInFlight++; 865 } 866 } 869 870 871 public void checkTransactionExport(boolean isLocal) { } 872 873 public void checkTransactionImport() { } 874 875 private void validateTransactionManager() throws IllegalStateException { 876 if (tm == null) { 877 throw new IllegalStateException 878 (sm.getString("enterprise_distributedtx.transaction_notactive")); 879 } 880 } 881 882 883 884 885 886 887 888 889 899 public void begin() throws NotSupportedException, SystemException { 900 if (_logger.isLoggable(Level.FINE)) 901 _logger.log(Level.FINE,"TM: begin"); 902 903 925 ((TransactionManagerImpl)tm).begin(getEffectiveTimeout()); 926 927 if (monitoringEnabled) { 929 Transaction tran = tm.getTransaction(); 930 activeTransactions.addElement(tran); 931 m_transInFlight++; 932 } 933 } 936 937 962 public void commit() throws RollbackException, 963 HeuristicMixedException, HeuristicRollbackException, SecurityException , 964 IllegalStateException , SystemException { 965 if (_logger.isLoggable(Level.FINE)) 966 _logger.log(Level.FINE,"TM: commit"); 967 validateTransactionManager(); 968 Object obj = null; 969 if(monitoringEnabled){ 970 obj = tm.getTransaction(); 971 } 972 if (invMgr.isInvocationStackEmpty()) { 973 try{ 974 tm.commit(); 975 if (monitoringEnabled){ 976 monitorTxCompleted(obj, true); 977 } 978 }catch(RollbackException e){ 979 if (monitoringEnabled){ 980 monitorTxCompleted(obj, false); 981 } 982 throw e; 983 }catch(HeuristicRollbackException e){ 984 if (monitoringEnabled){ 985 monitorTxCompleted(obj, false); 986 } 987 throw e; 988 }catch(HeuristicMixedException e){ 989 if (monitoringEnabled){ 990 monitorTxCompleted(obj, true); 991 } 992 throw e; 993 } 994 } else { 995 ComponentInvocation curr = null; 996 try { 997 curr = invMgr.getCurrentInvocation(); 998 curr.setTransactionCompeting(true); 999 tm.commit(); 1000 if (monitoringEnabled){ 1001 monitorTxCompleted(obj, true); 1002 } 1003 } catch (InvocationException ex) { 1004 assert false; 1005 }catch(RollbackException e){ 1006 if (monitoringEnabled){ 1007 monitorTxCompleted(obj, false); 1008 } 1009 throw e; 1010 }catch(HeuristicRollbackException e){ 1011 if (monitoringEnabled){ 1012 monitorTxCompleted(obj, false); 1013 } 1014 throw e; 1015 }catch(HeuristicMixedException e){ 1016 if (monitoringEnabled){ 1017 monitorTxCompleted(obj, true); 1018 } 1019 throw e; 1020 } finally { 1021 if (curr != null) { 1022 curr.setTransactionCompeting(false); 1023 } 1024 } 1025 } 1026 1027 } 1028 1029 1040 public int getStatus() throws SystemException { 1041 if (tm != null) { 1042 return tm.getStatus(); 1043 } else { 1044 return javax.transaction.Status.STATUS_NO_TRANSACTION; 1045 } 1046 } 1047 1048 1056 public Transaction getTransaction() throws SystemException { 1057 if (tm == null) { 1058 return null; 1059 } else { 1060 return tm.getTransaction(); 1061 } 1062 } 1063 1064 1079 public void resume(Transaction tobj) 1080 throws InvalidTransactionException, IllegalStateException , 1081 SystemException { 1082 if (_logger.isLoggable(Level.FINE)) 1083 _logger.log(Level.FINE,"TM: resume"); 1084 tm.resume(tobj); 1085 } 1086 1087 1088 1102 public void rollback() throws IllegalStateException , SecurityException , 1103 SystemException { 1104 if (_logger.isLoggable(Level.FINE)) 1105 _logger.log(Level.FINE,"TM: rollback"); 1106 validateTransactionManager(); 1107 Object obj = null; 1109 if (monitoringEnabled){ 1110 obj = tm.getTransaction(); 1111 } 1112 if (invMgr.isInvocationStackEmpty()) { 1113 tm.rollback(); 1114 } else { 1115 ComponentInvocation curr = null; 1116 try { 1117 curr = invMgr.getCurrentInvocation(); 1118 curr.setTransactionCompeting(true); 1119 tm.rollback(); 1120 } catch (InvocationException ex) { 1121 assert false; 1122 } finally { 1123 if (curr != null) { 1124 curr.setTransactionCompeting(false); 1125 } 1126 } 1127 } 1128 1129 if (monitoringEnabled){ 1130 monitorTxCompleted(obj, false); 1131 } 1132 1134 } 1135 1136 1148 public void setRollbackOnly() 1149 throws IllegalStateException , SystemException { 1150 if (_logger.isLoggable(Level.FINE)) 1151 _logger.log(Level.FINE,"TM: setRollbackOnly"); 1152 validateTransactionManager(); 1153 tm.setRollbackOnly(); 1154 } 1155 1156 1170 public void setTransactionTimeout(int seconds) throws SystemException { 1171 if (seconds < 0) seconds = 0; 1172 txnTmout.set(seconds); 1173 } 1175 1176 public void cleanTxnTimeout() { 1177 txnTmout.set(null); 1178 } 1179 1180 int getEffectiveTimeout() { 1181 Integer tmout = txnTmout.get(); 1182 if (tmout == null) { 1183 return transactionTimeout; 1184 } 1185 else { 1186 return tmout; 1187 } 1188 } 1189 1190 public void setDefaultTransactionTimeout(int seconds) { 1191 if (seconds < 0) seconds = 0; 1192 transactionTimeout = seconds; 1193 } 1194 1195 1210 public Transaction suspend() throws SystemException { 1211 if (_logger.isLoggable(Level.FINE)) 1212 _logger.log(Level.FINE,"TM: suspend"); 1213 validateTransactionManager(); 1214 return tm.suspend(); 1215 } 1216 1217 1224 public ArrayList getActiveTransactions() { 1225 ArrayList tranBeans = new ArrayList(); 1226 Vector active = (Vector)activeTransactions.clone(); for(int i=0;i<active.size();i++){ 1228 try{ 1229 Transaction tran = (Transaction)active.elementAt(i); 1230 String id="unknown"; 1231 long startTime = 0; 1232 long elapsedTime = 0; 1233 String status = "unknown"; 1234 String componentName = "unknown"; 1235 ArrayList<String > resourceNames = null; 1236 if(tran instanceof com.sun.jts.jta.TransactionImpl){ 1237 id=((com.sun.jts.jta.TransactionImpl)tran).getTransactionId(); 1238 startTime = ((com.sun.jts.jta.TransactionImpl)tran).getStartTime(); 1239 1240 }else if(tran instanceof J2EETransaction){ 1241 J2EETransaction tran1 = (J2EETransaction)tran; 1242 id=tran1.getTransactionId(); 1243 startTime = tran1.getStartTime(); 1244 componentName = tran1.getComponentName(); 1245 resourceNames = tran1.getResourceNames(); 1246 } 1247 elapsedTime = System.currentTimeMillis()-startTime; 1248 status = (String )statusMap.get(new Integer (tran.getStatus())); 1249 TransactionAdminBean tBean = new TransactionAdminBean(tran,id,status,elapsedTime, 1250 componentName, resourceNames); 1251 tranBeans.add(tBean); 1252 }catch(Exception ex){ 1253 } 1255 } 1256 return tranBeans; 1257 } 1258 1261 public void forceRollback(Transaction tran) throws IllegalStateException , SystemException{ 1262 if (tran != null){ 1263 tran.setRollbackOnly(); 1264 } 1265 } 1266 1270 1271 public int getNumberOfTransactionsCommitted(){ 1272 return m_transCommitted; 1273 } 1274 1275 1279 public int getNumberOfTransactionsRolledBack(){ 1280 return m_transRolledback; 1281 } 1282 1283 1286 public int getNumberOfActiveTransactions(){ 1287 return m_transInFlight; 1288 } 1289 1292 public synchronized void freeze(){ 1293 if(com.sun.jts.CosTransactions.AdminUtil.isFrozenAll()){ 1294 return; 1296 } 1297 com.sun.jts.CosTransactions.AdminUtil.freezeAll(); 1298 } 1299 1302 public synchronized void unfreeze(){ 1303 if(com.sun.jts.CosTransactions.AdminUtil.isFrozenAll()){ 1304 com.sun.jts.CosTransactions.AdminUtil.unfreezeAll(); 1305 } 1306 } 1307 1308 1311 public synchronized boolean isFrozen(){ 1312 return com.sun.jts.CosTransactions.AdminUtil.isFrozenAll(); 1313 } 1314 1315 1316 1323 public void recreate(Xid xid, long timeout) throws WorkException { 1324 ((TransactionManagerImpl) tm).recreate(xid, timeout); 1325 } 1326 1327 1334 public void release(Xid xid) throws WorkException { 1335 ((TransactionManagerImpl) tm).release(xid); 1336 } 1337 1338 1347 public XATerminator getXATerminator() { 1348 return ((TransactionManagerImpl) tm).getXATerminator(); 1349 } 1350 1351 1352 protected void monitorTxCompleted(Object tran, boolean committed){ 1353 if(tran==null || !activeTransactions.remove(tran)){ 1354 return; 1356 } 1357 if(committed){ 1358 m_transCommitted++; 1359 }else{ 1360 m_transRolledback++; 1361 } 1362 m_transInFlight--; 1363 } 1364 public void setMonitoringEnabled(boolean enabled){ 1365 monitoringEnabled = enabled; 1366 m_transCommitted = 0; 1368 m_transRolledback = 0; 1369 m_transInFlight = 0; 1370 activeTransactions.removeAllElements(); 1371 } 1372 static { 1373 com.sun.enterprise.admin.event.AdminEventListenerRegistry.addEventListener(JTSEvent.eventType, new JTSConfigChangeEventListener()); 1374 } 1375 1377 public boolean isTimedOut() { 1379 return false; 1380 } 1381 1382 private void registerStatisticMonitorTask() { 1385 TimerTask task = new StatisticMonitorTask(); 1386 Timer timer = Switch.getSwitch().getTimer(); 1387 int statInterval = 2 * 60 * 1000; 1389 try { 1390 String interval 1391 = System.getProperty("MONITOR_JTA_RESOURCE_TABLE_SECONDS"); 1392 int temp = Integer.parseInt(interval); 1393 if (temp > 0) { 1394 statInterval = temp; 1395 } 1396 } catch (Exception ex) { 1397 } 1399 1400 timer.scheduleAtFixedRate(task, 0, statInterval); 1401 } 1402 1403 class StatisticMonitorTask extends TimerTask { 1405 public void run() { 1406 if (resourceTable != null) { 1407 Map stats = resourceTable.getStats(); 1408 Iterator it = stats.keySet().iterator(); 1409 String key; 1410 System.out.println("********** J2EETransactionManagerImpl.resourceTable stats *****"); 1412 while (it.hasNext()) { 1413 key = (String )it.next(); 1414 System.out.println(key + ": " + stats.get(key).toString()); 1415 } 1416 } 1417 } 1418 } 1419 } 1421 | Popular Tags |