1 23 24 28 50 package com.sun.jts.CosTransactions; 51 52 54 import org.omg.CORBA.*; 55 import org.omg.PortableServer.*; 56 import org.omg.PortableServer.POAPackage.ServantAlreadyActive ; 57 import org.omg.PortableServer.POAPackage.ServantNotActive ; 58 import org.omg.CosTransactions.*; 59 60 import com.sun.jts.otsidl.*; 61 62 import com.sun.jts.trace.*; 63 import java.util.logging.Logger ; 64 import java.util.logging.Level ; 65 import com.sun.logging.LogDomains; 66 import com.sun.jts.utils.LogFormatter; 67 68 79 88 class CoordinatorResourceImpl extends CoordinatorResourcePOA 89 implements CompletionHandler { 90 private static POA poa = null; 91 private static boolean recoverable = false; 92 private CoordinatorResource thisRef = null; 93 94 96 boolean beingForced = false; 97 98 private GlobalTID globalTID = null; 99 private boolean subtransaction = false; 100 private boolean aborted = false; 101 private boolean heuristicDamage = false; 102 private boolean completed = false; 103 private boolean setAsTerminator = false; 104 105 108 109 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 110 127 CoordinatorResourceImpl( GlobalTID globalTID, 128 CoordinatorImpl coord, 129 boolean subtran ) { 130 131 132 134 subtransaction = subtran; 135 this.globalTID = globalTID; 136 137 140 if( coord != null ) 141 coord.setTerminator(this); 142 143 } 144 145 153 public void finalize() { 154 155 globalTID = null; 156 157 } 158 159 172 public void setCompleted( boolean aborted, 173 boolean heuristicDamage ) { 174 175 177 completed = true; 178 this.aborted = aborted; 179 this.heuristicDamage = heuristicDamage; 180 181 } 182 183 203 public Vote prepare() 204 throws SystemException, HeuristicMixed, HeuristicHazard { 205 206 207 Vote result = Vote.VoteRollback; 208 209 212 if( globalTID == null ) { 213 INTERNAL exc = new INTERNAL(MinorCode.NoGlobalTID, 214 CompletionStatus.COMPLETED_NO); 215 throw exc; 216 } 217 218 220 if( subtransaction ) { 221 222 INTERNAL exc = new INTERNAL(MinorCode.TopForSub, 223 CompletionStatus.COMPLETED_NO); 224 throw exc; 225 } 226 227 230 if (completed) { 231 if (aborted) { 232 result = Vote.VoteRollback; 233 } else { 234 result = Vote.VoteCommit; 235 } 236 } else { 237 238 240 TopCoordinator coord = (TopCoordinator)RecoveryManager.getCoordinator(globalTID); 241 242 245 if( coord != null ) 246 synchronized( coord ) { 247 248 252 result = coord.prepare(); 253 254 260 if( result == Vote.VoteRollback ) 261 coord.rollback(false); 262 } 263 } 264 265 269 if (result == Vote.VoteRollback) { 270 destroy(); 271 } 272 273 return result; 274 } 275 276 299 public void commit() 300 throws HeuristicRollback, HeuristicMixed, HeuristicHazard, NotPrepared, 301 SystemException { 302 303 305 if( globalTID == null ) { 306 INTERNAL exc = new INTERNAL(MinorCode.NoGlobalTID, 307 CompletionStatus.COMPLETED_NO); 308 throw exc; 309 } 310 311 313 if( subtransaction ) { 314 INTERNAL exc = new INTERNAL(MinorCode.TopForSub, 315 CompletionStatus.COMPLETED_NO); 316 throw exc; 317 } 318 319 323 if( completed ) { 324 if( aborted ) { 325 heuristicDamage = true; 326 HeuristicRollback exc = new HeuristicRollback(); 327 throw exc; 328 } else if( heuristicDamage ) { 329 HeuristicMixed exc = new HeuristicMixed(); 330 throw exc; 331 } 332 } else { 333 334 336 if(_logger.isLoggable(Level.FINE)) 338 { 339 _logger.logp(Level.FINE,"CoordinatorResourceImpl","commit()", 340 "Before invoking RecoveryManager.waitForRecovery():"+ 341 "GTID is: "+ globalTID.toString()); 342 343 } 344 RecoveryManager.waitForRecovery(); 345 346 TopCoordinator coord = (TopCoordinator)RecoveryManager.getCoordinator(globalTID); 347 348 353 if( coord != null ) 354 synchronized( coord ) { 355 363 369 makeSureSetAsTerminator(); 370 coord.commit(); 371 } 372 } 373 374 379 if( !beingForced ) 380 destroy(); 381 382 } 383 384 407 public void commit_one_phase() 408 throws TRANSACTION_ROLLEDBACK, HeuristicHazard, SystemException { 409 410 boolean rolledBack; 411 412 413 415 if( globalTID == null ) { 416 INTERNAL exc = new INTERNAL(MinorCode.NoGlobalTID, 417 CompletionStatus.COMPLETED_NO); 418 throw exc; 419 } 420 421 423 if( subtransaction ) { 424 INTERNAL exc = new INTERNAL(MinorCode.TopForSub, 425 CompletionStatus.COMPLETED_NO); 426 throw exc; 427 } 428 429 432 if( completed ) { 433 if( aborted ) { 434 TRANSACTION_ROLLEDBACK exc = new TRANSACTION_ROLLEDBACK(0,CompletionStatus.COMPLETED_NO); 435 throw exc; 436 } 437 } else { 438 439 441 if(_logger.isLoggable(Level.FINE)) 443 { 444 _logger.logp(Level.FINE,"CoordinatorResourceImpl","commit_one_phase()", 445 "Before invoking RecoveryManager.waitForRecovery(): "+ 446 "GTID is: " + globalTID.toString()); 447 } 448 RecoveryManager.waitForRecovery(); 449 450 TopCoordinator coord = (TopCoordinator)RecoveryManager.getCoordinator(globalTID); 451 452 457 if( coord != null ) { 458 rolledBack = false; 459 460 synchronized( coord ) { 461 462 makeSureSetAsTerminator(); 470 471 474 Vote vote = Vote.VoteRollback; 475 try { 476 vote = coord.prepare(); 477 } catch( HeuristicMixed exc ) { 478 throw new HeuristicHazard(); 479 } 480 try { 481 if( vote == Vote.VoteCommit ) 482 coord.commit(); 483 else if (vote == Vote.VoteRollback) { 484 coord.rollback(true); 488 rolledBack = true; 489 } 490 } catch (Throwable exc) { 491 if (exc instanceof INTERNAL) { 494 destroy(); 495 throw (INTERNAL) exc; 496 } 497 } 498 } 499 500 } else { 501 522 rolledBack = true; 523 524 } 525 526 if( rolledBack ) { 527 destroy(); 528 TRANSACTION_ROLLEDBACK exc = new TRANSACTION_ROLLEDBACK(0,CompletionStatus.COMPLETED_YES); 529 throw exc; 530 } 531 } 532 533 535 destroy(); 536 537 } 538 539 public void rollback() 562 throws HeuristicCommit, HeuristicMixed, 563 HeuristicHazard, SystemException { 564 565 567 if( globalTID == null ) { 568 INTERNAL exc = new INTERNAL(MinorCode.NoGlobalTID, 569 CompletionStatus.COMPLETED_NO); 570 throw exc; 571 } 572 573 575 if( subtransaction ) { 576 INTERNAL exc = new INTERNAL(MinorCode.TopForSub, 577 CompletionStatus.COMPLETED_NO); 578 throw exc; 579 } 580 581 585 if( completed ) { 586 if( !aborted ) { 587 heuristicDamage = true; 588 HeuristicCommit exc = new HeuristicCommit(); 589 throw exc; 590 } 591 else if( heuristicDamage ) { 592 HeuristicMixed exc = new HeuristicMixed(); 593 throw exc; 594 } 595 } else { 596 597 599 if(_logger.isLoggable(Level.FINE)) 601 { 602 _logger.logp(Level.FINE,"CoordinatorResourceImpl","rollback()", 603 "Before invoking RecoveryManager.waitForRecovery(): "+ 604 "GTID is : "+ globalTID.toString()); 605 606 } 607 RecoveryManager.waitForRecovery(); 608 609 TopCoordinator coord = (TopCoordinator)RecoveryManager.getCoordinator(globalTID); 610 611 616 if( coord != null ) 617 synchronized( coord ) { 618 619 makeSureSetAsTerminator(); 627 coord.rollback(true); 628 } 629 } 630 631 636 if( !beingForced ) 637 destroy(); 638 639 } 640 641 655 public void forget() throws SystemException { 656 657 659 if( subtransaction ) { 660 INTERNAL exc = new INTERNAL(MinorCode.TopForSub, 661 CompletionStatus.COMPLETED_NO); 662 throw exc; 663 } 664 665 667 destroy(); 668 } 669 670 689 public void commit_subtransaction( Coordinator parent ) 690 throws TRANSACTION_ROLLEDBACK, SystemException { 691 692 694 if( globalTID == null ) { 695 INTERNAL exc = new INTERNAL(MinorCode.NoGlobalTID, 696 CompletionStatus.COMPLETED_NO); 697 throw exc; 698 } 699 700 702 if( !subtransaction ) { 703 INTERNAL exc = new INTERNAL(MinorCode.SubForTop, 704 CompletionStatus.COMPLETED_NO); 705 throw exc; 706 } 707 708 711 if( completed ) { 712 if( aborted ) { 713 destroy(); 714 TRANSACTION_ROLLEDBACK exc = new TRANSACTION_ROLLEDBACK(0,CompletionStatus.COMPLETED_YES); 715 throw exc; 716 } 717 } else { 718 719 721 SubCoordinator coord = (SubCoordinator)RecoveryManager.getCoordinator(globalTID); 722 723 728 if( coord != null ) { 729 boolean rolledBack = false; 730 731 synchronized( coord ) { 732 try { 733 if( coord.prepare() == Vote.VoteCommit ) 734 coord.commit(); 735 else { 736 coord.rollback(true); 737 rolledBack = true; 738 } 739 } 740 catch( Throwable ex ) { 741 } 742 } 743 744 if( rolledBack ) { 745 destroy(); 746 TRANSACTION_ROLLEDBACK exc = new TRANSACTION_ROLLEDBACK(0,CompletionStatus.COMPLETED_YES); 747 throw exc; 748 } 749 } 750 } 751 752 755 destroy(); 756 757 } 758 759 776 777 public void rollback_subtransaction() throws SystemException { 778 779 781 if( globalTID == null ) { 782 INTERNAL exc = new INTERNAL(MinorCode.NoGlobalTID, 783 CompletionStatus.COMPLETED_NO); 784 throw exc; 785 } 786 787 789 if( !subtransaction ) { 790 INTERNAL exc = new INTERNAL(MinorCode.SubForTop, 791 CompletionStatus.COMPLETED_NO); 792 throw exc; 793 } 794 795 798 if( !completed ) { 799 800 802 SubCoordinator coord = (SubCoordinator)RecoveryManager.getCoordinator(globalTID); 803 804 807 if( coord != null ) 808 synchronized( coord ) { 809 coord.rollback(true); 810 } 811 } 812 813 816 destroy(); 817 818 } 819 820 830 CoordinatorResourceImpl( byte[] key ) { 831 832 834 globalTID = new GlobalTID(key); 835 836 841 843 846 854 857 860 862 } 863 864 872 CoordinatorResource object() { 873 if( thisRef == null ) { 874 if( poa == null ) { 875 poa = Configuration.getPOA("CoordinatorResource"); 876 recoverable = Configuration.isRecoverable(); 877 } 878 879 try { 880 byte[] id = null; 881 if (recoverable && globalTID != null) { 882 id = globalTID.toBytes(); 883 poa.activate_object_with_id(id, this); 884 org.omg.CORBA.Object obj = poa.create_reference_with_id(id, CoordinatorResourceHelper.id()); 885 thisRef = CoordinatorResourceHelper.narrow(obj); 886 } else { 888 poa.activate_object(this); 889 org.omg.CORBA.Object obj = poa.servant_to_reference(this); 890 thisRef = CoordinatorResourceHelper.narrow(obj); 891 } 893 } catch( ServantAlreadyActive saexc ) { 894 _logger.log(Level.SEVERE, 895 "jts.create_CoordinatorResource_object_error",saexc); 896 String msg = LogFormatter.getLocalizedMessage(_logger, 897 "jts.create_CoordinatorResource_object_error"); 898 throw new org.omg.CORBA.INTERNAL (msg); 899 900 } catch( ServantNotActive snexc ) { 901 _logger.log(Level.SEVERE, 902 "jts.create_CoordinatorResource_object_error",snexc); 903 String msg = LogFormatter.getLocalizedMessage(_logger, 904 "jts.create_CoordinatorResource_object_error"); 905 throw new org.omg.CORBA.INTERNAL (msg); 906 907 } catch( Exception exc ) { 908 _logger.log(Level.SEVERE, 909 "jts.create_CoordinatorResource_object_error",exc); 910 String msg = LogFormatter.getLocalizedMessage(_logger, 911 "jts.create_CoordinatorResource_object_error"); 912 throw new org.omg.CORBA.INTERNAL (msg); 913 914 } 915 } 916 917 return thisRef; 918 } 919 920 928 void destroy() { 929 930 try { 931 if (poa != null && thisRef != null) { 932 poa.deactivate_object(poa.reference_to_id(thisRef)); 933 thisRef = null; 934 } else { 935 941 POA crPoa = null; 942 if (poa == null) { 943 crPoa = Configuration.getPOA("CoordinatorResource"); 944 } else { 945 crPoa = poa; 946 } 947 if (thisRef == null) { 948 crPoa.deactivate_object(crPoa.servant_to_id(this)); 949 } else { 950 crPoa.deactivate_object(crPoa.reference_to_id(thisRef)); 951 thisRef = null; 952 } 953 } 954 } catch( Exception exc ) { 955 _logger.log(Level.WARNING,"jts.object_destroy_error","CoordinatorResource"); 956 957 } 958 959 finalize(); 960 } 961 962 970 void dump() { 971 } 972 973 981 void makeSureSetAsTerminator() { 982 983 if( !setAsTerminator ) { 984 985 CoordinatorImpl coord = RecoveryManager.getCoordinator(globalTID); 986 987 991 if( coord == null ) { 992 OBJECT_NOT_EXIST exc = new OBJECT_NOT_EXIST(); 993 throw exc; 994 } else { 995 coord.setTerminator(this); 996 setAsTerminator = true; 997 } 998 } 999 1000 } 1002 } 1004 | Popular Tags |