1 23 24 28 50 package com.sun.jts.CosTransactions; 51 52 54 import java.util.*; 55 56 import org.omg.CORBA.*; 57 import org.omg.CosTransactions.*; 58 59 import com.sun.jts.otsidl.*; 60 import com.sun.jts.trace.*; 61 import java.util.logging.Logger ; 62 import java.util.logging.Level ; 63 import com.sun.logging.LogDomains; 64 import com.sun.jts.utils.LogFormatter; 65 66 75 82 public class CurrentTransaction { 83 private static Hashtable threadContexts = new Hashtable(Configuration.EXPECTED_CONCURRENT_THREADS); 84 private static Vector suspended = new Vector(Configuration.EXPECTED_CONCURRENT_TRANSACTIONS); 85 86 static boolean statsOn=false; 88 89 private static Hashtable importedTransactions = new Hashtable(); 90 private static RegisteredStatics statics = null; 91 94 96 private static ThreadLocal m_tid=new ThreadLocal (); 97 101 107 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 108 109 private static PropagationContext emptyContext = 110 new PropagationContext(0,new TransIdentity(null,null,new otid_t(-1,0,new byte[0])), 111 new TransIdentity[0],null); 112 113 121 static void initialise() { 122 123 125 } 126 127 146 static boolean setCurrent( ControlImpl control, 147 boolean stack ) { 148 149 boolean result = false; 150 151 154 ControlImpl current = (ControlImpl)m_tid.get(); 155 157 158 163 if( current != null ) { 164 if( stack ) { 165 166 172 if( statics != null ) 173 statics.distributeEnd(current,false); 174 175 178 StatusHolder outStatus = new StatusHolder(); 179 control.pushControl(current,outStatus); 180 if(statsOn){ 181 Thread thread = Thread.currentThread(); 182 result = (threadContexts.remove(thread) != null); 183 } 184 else 185 result=true; 186 m_tid.set(null); 187 188 191 if(statsOn) 192 suspended.addElement(current); 193 } 194 } else 195 result = true; 196 197 199 if( result ) { 200 201 207 if( statics != null ) 208 statics.distributeStart(control,stack); 209 210 212 if(statsOn){ 213 Thread thread = Thread.currentThread(); 214 threadContexts.put(thread,control); 215 } 216 m_tid.set(control); 217 218 220 if(statsOn) 221 suspended.removeElement(control); 222 223 225 control.incrementAssociation(); 226 } 227 228 return result; 229 } 230 231 251 static ControlImpl endCurrent( boolean unstack ) { 252 253 255 ControlImpl result = (ControlImpl)m_tid.get(); 257 259 260 264 if( result != null ){ 265 if(statsOn){ 266 Thread thread = Thread.currentThread(); 267 threadContexts.remove(thread); 268 } 269 m_tid.set(null); 270 271 273 result.decrementAssociation(); 274 275 278 if( !unstack && statsOn) suspended.addElement(result); 279 280 284 if( statics != null ) 285 statics.distributeEnd(result,unstack); 286 287 292 if( unstack ) { 293 StatusHolder outStatus = new StatusHolder(); 294 ControlImpl stacked = result.popControl(outStatus); 295 if( stacked != null && 296 outStatus.value == Status.StatusActive ) { 297 298 304 if( statics != null ) 305 statics.distributeStart(stacked,false); 306 307 310 if(statsOn){ 311 Thread thread = Thread.currentThread(); 312 threadContexts.put(thread,stacked); 313 suspended.removeElement(stacked); 314 } 315 m_tid.set(stacked); 316 } 317 } 318 } 319 320 322 else 323 result = null; 324 325 return result; 326 } 327 328 public static boolean isTxAssociated() { 332 return (m_tid.get()!=null); 336 } 337 338 private static ControlImpl endAborted() { 339 return (ControlImpl)m_tid.get(); 340 } 342 343 363 private static ControlImpl 364 endAborted( boolean[] aborted, boolean endAssociation) { 365 366 369 boolean completed = true; 370 aborted[0] = false; 371 372 ControlImpl result = (ControlImpl)m_tid.get(); 373 374 378 if( result != null ) 379 try { 380 completed = (result.getTranState() != Status.StatusActive); 381 } catch( Throwable exc ) {} 382 383 if( result != null && completed ) { 384 if (endAssociation) { 385 synchronized(CurrentTransaction.class){ 386 if(statsOn){ 387 Thread thread = Thread.currentThread(); 388 threadContexts.remove(thread); 389 } 390 m_tid.set(null); 391 392 396 if( statics != null ) 397 statics.distributeEnd(result,false); 398 399 402 result = result.popAborted(); 403 404 406 if( result != null ) { 407 m_tid.set(result); 408 if(statsOn){ 409 Thread thread = Thread.currentThread(); 410 threadContexts.put(thread,result); 411 suspended.removeElement(result); 412 } 413 } 414 415 419 if( statics != null ) 420 statics.distributeStart(result,false); 421 } 422 } 423 aborted[0] = true; 424 } 425 426 if(_logger.isLoggable(Level.FINEST)) 427 { 428 Thread thread = Thread.currentThread(); 429 _logger.logp(Level.FINEST,"CurrentTransaction","endAborted()", 430 "threadContexts.get(thread) returned " + 431 result + " for current thread " + thread); 432 } 433 434 return result; 435 } 436 437 446 static void addSuspended( ControlImpl control ) { 447 if(statsOn) 448 suspended.addElement(control); 449 } 450 451 461 static boolean removeSuspended( ControlImpl control ) { 462 boolean result = true; 463 if(statsOn) 464 result=suspended.removeElement(control); 465 return result; 466 } 467 468 481 public static ControlImpl getCurrent() 482 throws TRANSACTION_ROLLEDBACK { 483 484 ControlImpl result = (ControlImpl)m_tid.get(); 486 487 return result; 488 } 489 490 509 static Coordinator getCurrentCoordinator() 510 throws TRANSACTION_ROLLEDBACK, Unavailable { 511 512 520 521 524 ControlImpl control = (ControlImpl)m_tid.get(); 525 Coordinator result = null; 526 527 if (control != null) { 528 529 if( Configuration.isLocalFactory()) { 530 result = (Coordinator) ((ControlImpl) control).get_localCoordinator(); 531 } else { 532 result = control.get_coordinator(); 535 } 536 } 537 538 return result; 539 540 } 541 542 555 static int numActive( Long localTID, 556 boolean[] outstanding ) { 557 if(!statsOn){ 558 throw new NO_IMPLEMENT("statistics not on"); 559 } 560 561 int result = 0; 562 outstanding[0] = false; 563 StatusHolder outStatus = new StatusHolder(); 564 565 568 Enumeration controls = threadContexts.elements(); 569 while( controls.hasMoreElements() ) { 570 ControlImpl current = (ControlImpl)controls.nextElement(); 571 572 575 outStatus.value = Status.StatusRolledBack; 576 try { 577 Long currentLocalTID = new Long (current.getLocalTID(outStatus)); 578 if( outStatus.value == Status.StatusActive ) 579 if( currentLocalTID.equals(localTID) ) { 580 outstanding[0] |= current.isOutgoing(); 581 result++; 582 } 583 } catch( Throwable exc ) { 584 } 585 } 586 587 return result; 588 } 589 590 601 synchronized static void registerStatic( StaticResource obj ) { 602 603 606 if( statics == null ) 607 statics = new RegisteredStatics(); 608 609 611 statics.addStatic(obj); 612 } 613 614 623 static Control[] getSuspendedTransactions() { 624 625 if(!statsOn){ 626 throw new NO_IMPLEMENT("statistics not on"); 627 } 628 629 Control[] result = null; 630 631 633 int suspNum = suspended != null ? suspended.size() : 0; 634 if( suspNum > 0 ) { 635 result = new Control[suspNum]; 636 637 Enumeration controls = suspended.elements(); 638 int pos = 0; 639 while( controls.hasMoreElements() ) 640 result[pos++] = ((ControlImpl)controls.nextElement()).object(); 641 } 642 else 643 result = new Control[0]; 644 645 return result; 646 } 647 648 657 static Control[] getRunningTransactions() { 658 if(!statsOn){ 659 throw new NO_IMPLEMENT("statistics not on"); 660 } 661 662 Control[] result = null; 663 664 666 int runNum = threadContexts != null ? threadContexts.size() : 0; 667 if( runNum > 0 ) { 668 result = new Control[runNum]; 669 670 Enumeration controls = threadContexts.elements(); 671 int pos = 0; 672 while( controls.hasMoreElements() ) 673 result[pos++] = ((ControlImpl)controls.nextElement()).object(); 674 } 675 else 676 result = new Control[0]; 677 678 return result; 679 } 680 681 690 static Control[] getAllTransactions() { 691 692 if(!statsOn){ 693 throw new NO_IMPLEMENT("statistics not on"); 694 } 695 Control[] result = null; 696 697 int allNum = threadContexts != null ? threadContexts.size()+suspended.size() : 0; 698 if( allNum > 0 ) { 699 result = new Control[allNum]; 700 701 703 Enumeration controls = suspended.elements(); 704 int pos = 0; 705 while( controls.hasMoreElements() ) 706 result[pos++] = ((ControlImpl)controls.nextElement()).object(); 707 708 710 controls = threadContexts.elements(); 711 while( controls.hasMoreElements() ) 712 result[pos++] = ((ControlImpl)controls.nextElement()).object(); 713 } 714 else 715 result = new Control[0]; 716 717 return result; 718 } 719 720 737 static void sendingRequest( int id, 738 PropagationContextHolder holder ) 739 throws TRANSACTION_ROLLEDBACK, TRANSACTION_REQUIRED { 740 741 748 762 763 766 boolean[] outBoolean = new boolean[1]; 767 ControlImpl current = endAborted(outBoolean, false); 768 if( outBoolean[0] ) { 769 TRANSACTION_ROLLEDBACK exc = new TRANSACTION_ROLLEDBACK(0,CompletionStatus.COMPLETED_NO); 770 throw exc; 771 } 772 773 775 if( current == null ) { 776 return; 780 } 781 782 785 try { 786 holder.value = current.getTXContext(); 787 } 788 789 793 catch (Unavailable exc) { 794 INVALID_TRANSACTION ex2 = new INVALID_TRANSACTION(0,CompletionStatus.COMPLETED_NO); 795 throw ex2; 796 } 797 798 801 catch( TRANSACTION_ROLLEDBACK exc ) { 802 endCurrent(true); 803 current.destroy(); 804 throw (TRANSACTION_ROLLEDBACK)exc.fillInStackTrace(); 805 } 806 807 809 catch( Throwable exc ) { 810 } 811 812 815 } 824 825 838 static void receivedReply( int id, 839 PropagationContext context, 840 org.omg.CORBA.Environment ex ) 841 throws org.omg.CORBA.WrongTransaction { 842 843 845 ControlImpl current = (ControlImpl)m_tid.get(); 847 848 851 if( current == null ) { 852 return; 853 } 854 855 857 java.lang.Exception ctxExc = ex.exception(); 860 if (ctxExc instanceof SystemException) { 861 862 Coordinator currentCoord = null; 863 try { 864 if (Configuration.isLocalFactory()) { 865 currentCoord = current.get_localCoordinator(); 866 } else { 867 currentCoord = current.get_coordinator(); 868 } 869 } catch (Unavailable exc) {} 870 871 if (currentCoord == null) { 872 return; } 874 875 try { 876 currentCoord.rollback_only(); 877 } catch (Inactive exc) {} 878 879 } 883 884 886 if( context == null || 887 context.current == null || 888 context.current.coord == null || 889 context.current.otid.formatID == -1 ) { 890 return; 891 } 892 893 896 StatusHolder outStatus = new StatusHolder(); 897 outStatus.value = Status.StatusRolledBack; 898 GlobalTID globalTID = null; 899 try { 900 globalTID = new GlobalTID(current.getGlobalTID(outStatus)); 901 } catch( Throwable exc ) { 902 } 903 904 907 if( globalTID != null ) { 908 if( outStatus.value != Status.StatusActive ) { 909 endCurrent(true); 910 current.destroy(); 911 912 org.omg.CORBA.WrongTransaction exc = new org.omg.CORBA.WrongTransaction (); 914 throw exc; 915 } 916 917 920 if( !globalTID.equals(context.current.otid) ) { 921 org.omg.CORBA.WrongTransaction exc = new org.omg.CORBA.WrongTransaction (); 923 throw exc; 924 } 925 } 926 927 929 } 938 939 951 static void receivedRequest( int id, 952 PropagationContext context ) { 953 954 957 if( context == null || 958 context.current == null || 959 context.current.otid.formatID == -1 ) { 960 return; 961 } 962 963 965 Control current = Configuration.getFactory().recreate(context); 969 970 972 importedTransactions.put(Thread.currentThread(),new GlobalTID(context.current.otid)); 973 974 976 try { 977 ControlImpl contImpl = null; 978 if (Configuration.isLocalFactory()) { 979 contImpl = (ControlImpl) current; 980 } else { 981 contImpl = ControlImpl.servant(JControlHelper.narrow(current)); 982 } 983 setCurrent(contImpl,false); 984 } 985 986 989 catch( Throwable exc ) { 990 _logger.log(Level.WARNING,"jts.unable_to_create_subordinate_coordinator"); 991 String msg = LogFormatter.getLocalizedMessage(_logger, 992 "jts.unable_to_create_subordinate_coordinator"); 993 throw new org.omg.CORBA.INTERNAL (msg); 994 } 995 } 996 997 1011 static void sendingReply( int id, 1012 PropagationContextHolder holder ) 1013 throws INVALID_TRANSACTION, TRANSACTION_ROLLEDBACK { 1014 1015 1022 if( emptyContext.implementation_specific_data == null ) { 1023 ORB orb = Configuration.getORB(); 1024 emptyContext.implementation_specific_data = orb.create_any(); 1025 emptyContext.implementation_specific_data.insert_boolean(false); 1026 } 1027 1028 1034 1035 1037 boolean[] outBoolean = new boolean[1]; 1038 ControlImpl current = endAborted(outBoolean, true); if( outBoolean[0] ) { 1040 importedTransactions.remove(Thread.currentThread()); 1041 TRANSACTION_ROLLEDBACK exc = new TRANSACTION_ROLLEDBACK(0,CompletionStatus.COMPLETED_YES); 1042 throw exc; 1043 } 1044 1045 1048 Thread thread = Thread.currentThread(); 1049 GlobalTID importedTID = (GlobalTID)importedTransactions.remove(thread); 1050 1051 1054 if( importedTID == null && current == null ) { 1055 return; 1056 } 1057 1058 1060 StatusHolder outStatus = new StatusHolder(); 1061 try { 1062 if( importedTID == null || 1063 current == null || 1064 !importedTID.equals(current.getGlobalTID(outStatus)) || 1065 outStatus.value != Status.StatusActive ) { 1066 INVALID_TRANSACTION exc = new INVALID_TRANSACTION(MinorCode.WrongContextOnReply,CompletionStatus.COMPLETED_YES); 1067 throw exc; 1068 } 1069 } catch( SystemException ex ) { 1070 INVALID_TRANSACTION exc = new INVALID_TRANSACTION(MinorCode.WrongContextOnReply,CompletionStatus.COMPLETED_YES); 1071 throw exc; 1072 } 1073 1074 1076 CoordinatorImpl coord = null; 1077 Coordinator coordRef = null; 1078 try { 1079 if (Configuration.isLocalFactory()) { 1080 coord = (CoordinatorImpl) current.get_localCoordinator(); 1081 } else { 1082 coordRef = current.get_coordinator(); 1083 coord = CoordinatorImpl.servant(coordRef); 1084 } 1085 1086 } catch( Throwable exc ) { 1088 } 1089 1090 1096 CoordinatorImpl forgetParent = null; 1097 int[] outInt = new int[1]; 1098 try { 1100 forgetParent = coord.replyAction(outInt); 1101 } catch( Throwable exc ) { 1102 } 1103 1104 int replyAction = outInt[0]; 1105 if( replyAction == CoordinatorImpl.activeChildren ) { 1106 try { 1107 coord.rollback_only(); 1108 } catch( Throwable ex ) {} 1109 1110 INVALID_TRANSACTION exc = new INVALID_TRANSACTION(MinorCode.UnfinishedSubtransactions, 1111 CompletionStatus.COMPLETED_YES); 1112 throw exc; 1113 } 1114 1115 1117 endCurrent(false); 1118 1119 1124 if( replyAction == CoordinatorImpl.forgetMe ) { 1125 current.destroy(); 1126 coord.cleanUpEmpty(forgetParent); 1127 } 1128 1129 1131 else { 1132 if( current.isAssociated() || 1133 current.isOutgoing() ) { 1134 try { 1135 coord.rollback_only(); 1136 } catch( Throwable exc ) {} 1137 1138 INVALID_TRANSACTION exc = new INVALID_TRANSACTION(MinorCode.DeferredActivities, 1139 CompletionStatus.COMPLETED_YES); 1140 throw exc; 1141 } 1142 1143 current.destroy(); 1144 } 1145 1146 1149 holder.value = new PropagationContext(0,new TransIdentity(null,null,importedTID.realTID), 1150 new TransIdentity[0],emptyContext.implementation_specific_data); 1151 1152 } 1153 1154 1161 public static void recreate(GlobalTID tid, int timeout) { 1162 1163 if (RecoveryManager.readAndUpdateTxMap(tid) == false) { 1165 throw new INVALID_TRANSACTION( 1166 MinorCode.TX_CONCURRENT_WORK_DISALLOWED, 1167 CompletionStatus.COMPLETED_NO); 1168 } 1169 1170 1172 try { 1173 1174 TransactionFactoryImpl factory = 1176 (TransactionFactoryImpl) Configuration.getFactory(); 1177 Control current = factory.recreate(tid, timeout); 1178 1179 importedTransactions.put(Thread.currentThread(), tid); 1181 1182 ControlImpl contImpl = null; 1184 if (Configuration.isLocalFactory()) { 1185 contImpl = (ControlImpl) current; 1186 } else { 1187 contImpl = ControlImpl.servant(JControlHelper.narrow(current)); 1188 } 1189 setCurrent(contImpl,false); 1190 1191 } catch (Throwable exc) { 1192 RecoveryManager.removeFromTxMap(tid); _logger.log(Level.WARNING,"jts.unable_to_create_subordinate_coordinator"); 1194 String msg = LogFormatter.getLocalizedMessage(_logger, 1195 "jts.unable_to_create_subordinate_coordinator"); 1196 throw new INVALID_TRANSACTION( 1198 MinorCode.TX_RECREATE_FAILED, CompletionStatus.COMPLETED_MAYBE); 1199 } 1200 } 1201 1202 1208 public static void release(GlobalTID tid) { 1209 1210 Thread thread = (Thread ) RecoveryManager.getThreadFromTxMap(tid); 1211 1212 if (thread == null || (thread != Thread.currentThread())) { 1213 return; 1215 } else { 1216 RecoveryManager.removeFromTxMap(tid); 1217 } 1218 1219 boolean[] outBoolean = new boolean[1]; 1221 ControlImpl control = endAborted(outBoolean, true); if (outBoolean[0]) { 1223 importedTransactions.remove(Thread.currentThread()); 1224 return; } 1226 1227 GlobalTID importedTID = (GlobalTID) importedTransactions.remove(thread); 1230 1231 StatusHolder outStatus = new StatusHolder(); 1233 try { 1234 if (importedTID == null || control == null || 1235 !importedTID.equals(control.getGlobalTID(outStatus)) || 1236 outStatus.value != Status.StatusActive) { 1237 INVALID_TRANSACTION exc = 1238 new INVALID_TRANSACTION(MinorCode.WrongContextOnReply, 1239 CompletionStatus.COMPLETED_YES); 1240 throw exc; 1241 } 1242 } catch (SystemException ex) { 1243 INVALID_TRANSACTION exc = 1244 new INVALID_TRANSACTION(MinorCode.WrongContextOnReply, 1245 CompletionStatus.COMPLETED_YES); 1246 throw exc; 1247 } 1248 1249 endCurrent(false); 1251 control.destroy(); 1252 } 1253 1254 1263 synchronized static void endAll( GlobalTID globalTID, 1265 boolean aborted ) { 1266 throw new NO_IMPLEMENT("not implemented"); 1267 1305 } 1306 1307 1320 static void shutdown( boolean immediate ) { 1321 1322 } 1324 1325 1333 static void dump() { 1334 } 1335 1336 1345 1399} 1400 | Popular Tags |