1 23 24 28 29 51 package com.sun.jts.CosTransactions; 52 53 import java.util.*; 54 55 import org.omg.CORBA.*; 56 import org.omg.CosTransactions.*; 57 58 import com.sun.jts.otsidl.*; 59 import com.sun.jts.jtsxa.OTSResourceImpl; 60 65 68 import com.sun.jts.trace.*; 69 70 import java.util.logging.Logger ; 71 import java.util.logging.Level ; 72 import com.sun.logging.LogDomains; 73 import com.sun.jts.utils.LogFormatter; 74 75 92 93 110 public class TopCoordinator extends CoordinatorImpl { 111 String name = null; 112 RegisteredResources participants = null; 113 RegisteredSyncs synchronizations = null; 114 SuperiorInfo superInfo = null; 115 NestingInfo nestingInfo = null; 116 TransactionState tranState = null; 117 CoordinatorLog logRecord = null; 118 CompletionHandler terminator = null; 119 boolean registered = false; 120 boolean registeredSync = false; 121 boolean root = true; 122 boolean rollbackOnly = false; 123 boolean dying = false; 124 boolean temporary = false; 125 int hash = 0; 126 127 130 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 131 Vector recoveryCoordinatorList = null; 133 CoordinatorSynchronizationImpl coordSyncImpl = null; 134 135 boolean delegated = false; 137 String logPath = null; 138 139 148 TopCoordinator() { 149 } 151 152 167 TopCoordinator(int timeOut) throws LogicErrorException { 168 169 174 if (Configuration.isRecoverable()) { 175 logRecord = CoordinatorLogPool.getCoordinatorLog(); 178 } else { 179 logRecord = null; 180 } 181 182 184 tranState = new TransactionState(logRecord); 185 186 189 superInfo = new SuperiorInfo(tranState.localTID, tranState.globalTID, 190 null, logRecord); 191 192 194 196 198 hash = superInfo.globalTID.hashCode(); 199 200 203 nestingInfo = null; 204 participants = null; 205 synchronizations = null; 206 207 209 root = true; 210 registered = true; 211 registeredSync = true; 212 rollbackOnly = false; 213 dying = false; 214 temporary = false; 215 terminator = null; 216 217 if (!tranState.setState(TransactionState.STATE_ACTIVE)) { 218 219 222 LogicErrorException exc = 223 new LogicErrorException( 224 LogFormatter.getLocalizedMessage(_logger, 225 "jts.invalid_state_change")); 226 throw exc; 227 228 } else { 229 230 if (!RecoveryManager.addCoordinator(tranState.globalTID, 232 tranState.localTID, 233 this, 234 timeOut)) { 235 LogicErrorException exc = 236 new LogicErrorException( 237 LogFormatter.getLocalizedMessage(_logger, 238 "jts.transaction_id_already_in_use")); 239 throw exc; 240 } 241 } 242 } 243 244 263 TopCoordinator(int timeOut, GlobalTID globalTID, Coordinator superior, 264 boolean temporary) throws LogicErrorException { 265 266 271 if (Configuration.isRecoverable()) { 272 logRecord = CoordinatorLogPool.getCoordinatorLog(); 275 } else { 276 logRecord = null; 277 } 278 279 283 tranState = new TransactionState(globalTID,logRecord); 284 285 288 superInfo = new SuperiorInfo(tranState.localTID, tranState.globalTID, 289 superior, logRecord); 290 291 293 295 297 hash = superInfo.globalTID.hashCode(); 298 299 302 nestingInfo = null; 303 participants = null; 304 synchronizations = null; 305 306 308 root = false; 309 registered = false; 310 registeredSync = false; 311 rollbackOnly = false; 312 dying = false; 313 this.temporary = temporary; 314 terminator = null; 315 316 319 if (!tranState.setState(TransactionState.STATE_ACTIVE)) { 320 LogicErrorException exc = 321 new LogicErrorException( 322 LogFormatter.getLocalizedMessage(_logger, 323 "jts.invalid_state_change")); 324 throw exc; 325 } else { 326 if (!RecoveryManager.addCoordinator(globalTID, tranState.localTID, 327 this, timeOut)) { 328 LogicErrorException exc = 329 new LogicErrorException( 330 LogFormatter.getLocalizedMessage(_logger, 331 "jts.transaction_id_already_in_use")); 332 throw exc; 333 } 334 } 335 } 336 337 346 synchronized public void finalize() { 347 348 350 dying = true; 351 352 355 int state = TransactionState.STATE_ROLLED_BACK; 356 if (tranState != null && !temporary) { 357 state = tranState.state; 358 } 359 360 switch (state) { 361 362 366 case TransactionState.STATE_ACTIVE : 367 try { 368 rollback(true); 369 } catch (Throwable exc) {} 370 371 break; 372 373 376 case TransactionState.STATE_PREPARED_READONLY : 377 case TransactionState.STATE_COMMITTED : 378 case TransactionState.STATE_ROLLED_BACK : 379 case TransactionState.STATE_COMMITTED_ONE_PHASE_OK : 380 case TransactionState.STATE_COMMIT_ONE_PHASE_ROLLED_BACK : 381 382 if (tranState != null) { 383 tranState.finalize(); 384 } 385 386 if (superInfo != null) { 387 superInfo.finalize(); 388 } 389 390 if (nestingInfo != null) { 391 nestingInfo.finalize(); 392 } 393 394 if (participants != null) { 395 participants.finalize(); 396 } 397 398 if (synchronizations != null) { 399 synchronizations.finalize(); 400 } 401 402 tranState = null; 403 superInfo = null; 404 nestingInfo = null; 405 participants = null; 406 synchronizations = null; 407 logRecord = null; 408 terminator = null; 409 name = null; 410 break; 411 412 415 default : 416 break; 417 } 418 } 419 420 435 synchronized void reconstruct(CoordinatorLog log) { 436 437 439 rollbackOnly = false; 440 registered = false; 441 registeredSync = false; 442 root = false; 443 dying = false; 444 temporary = false; 445 terminator = null; 446 logRecord = log; 447 name = null; 448 449 452 nestingInfo = null; 453 synchronizations = null; 454 455 458 tranState = new TransactionState(); 459 int state = tranState.reconstruct(log); 460 if (state == TransactionState.STATE_NONE || 461 state == TransactionState.STATE_COMMITTED || 462 state == TransactionState.STATE_ROLLED_BACK) { 465 466 469 CoordinatorLog.removeLog(log.localTID); 470 destroy(); 471 472 } else { 473 474 participants = new RegisteredResources(this); 476 participants.reconstruct(log); 477 478 484 superInfo = new SuperiorInfo(); 485 superInfo.reconstruct(log, this); 486 487 490 name = superInfo.globalTID.toString(); 491 492 494 hash = superInfo.globalTID.hashCode(); 495 } 496 } 497 498 515 synchronized void delegated_reconstruct(CoordinatorLog log, String logPath ) { 516 517 519 rollbackOnly = false; 520 registered = false; 521 registeredSync = false; 522 root = false; 523 dying = false; 524 temporary = false; 525 terminator = null; 526 logRecord = log; 527 name = null; 528 529 532 nestingInfo = null; 533 synchronizations = null; 534 535 delegated = true; 536 this.logPath = logPath; 537 538 541 tranState = new TransactionState(); 542 int state = tranState.reconstruct(log); 544 if (state == TransactionState.STATE_NONE || 545 state == TransactionState.STATE_COMMITTED || 546 state == TransactionState.STATE_ROLLED_BACK) { 549 550 553 CoordinatorLog.removeLog(log.localTID, logPath); 554 destroy(); 555 556 } else { 557 558 participants = new RegisteredResources(this); 560 participants.reconstruct(log); 561 562 568 superInfo = new SuperiorInfo(); 569 superInfo.delegated_reconstruct(log, this, logPath); 570 571 574 name = superInfo.globalTID.toString(); 575 576 578 hash = superInfo.globalTID.hashCode(); 579 } 580 } 581 582 601 synchronized Status recover(boolean[] isRoot) { 602 603 Status result; 604 605 608 if (superInfo.recovery != null) { 609 610 613 614 650 switch (tranState.state) { 651 652 656 case TransactionState.STATE_COMMITTED : 657 case TransactionState.STATE_COMMITTED_ONE_PHASE_OK : 659 case TransactionState.STATE_COMMITTING_ONE_PHASE : 661 case TransactionState.STATE_COMMIT_ONE_PHASE_ROLLED_BACK : 663 case TransactionState.STATE_COMMITTING : 665 result = Status.StatusCommitted; 666 break; 667 668 672 case TransactionState.STATE_ROLLED_BACK : case TransactionState.STATE_ROLLING_BACK : 674 result = Status.StatusRolledBack; 677 break; 678 679 687 default : 688 689 boolean attemptRetry = true; 690 693 697 710 int commitRetries = Configuration.getRetries(); 711 if (commitRetries >= 0 && (superInfo.resyncRetries() >= commitRetries)) 712 attemptRetry = false; 713 714 715 if (!attemptRetry) { 716 717 721 String heuristicVar; 722 boolean commitTransaction = false; 723 result = Status.StatusRolledBack; 724 725 heuristicVar = 726 Configuration.getPropertyValue( 727 Configuration.HEURISTIC_DIRECTION); 728 729 if (heuristicVar != null) { 730 commitTransaction = (heuristicVar.charAt(0) == '1'); 731 } 732 733 if (commitTransaction) { 734 result = Status.StatusCommitted; 735 } 736 737 } else { 738 739 743 try { 744 if(_logger.isLoggable(Level.FINE)) 745 { 746 _logger.logp(Level.FINE,"TopCoordinator","recover", 747 "Before invoking replay_completion on Superior Coordinator"); 748 } 749 if (!delegated) { 750 result = superInfo.recovery. 751 replay_completion(superInfo.resource); 752 } else { 753 result = ((RecoveryCoordinatorImpl)(superInfo.recovery)). 754 replay_completion(superInfo.resource, logPath); 755 } 756 757 762 if (result == Status.StatusCommitting) { 763 result = Status.StatusCommitted; 764 } 765 } catch (Throwable exc) { 766 770 if (!(exc instanceof COMM_FAILURE) && 771 !(exc instanceof TRANSIENT)) { 772 result = Status.StatusRolledBack; 773 } else { 774 result = Status.StatusUnknown; 777 } 778 } 779 } 780 781 break; 782 } 783 784 787 root = false; 788 789 } else { 790 791 796 if (tranState.state == TransactionState.STATE_PREPARED_SUCCESS) { 797 result = Status.StatusCommitted; 798 } else { 799 result = Status.StatusRolledBack; 800 } 801 802 804 root = true; 805 } 806 807 isRoot[0] = root; 808 809 return result; 810 } 811 812 821 public Status get_status() { 822 823 Status result = Status.StatusUnknown; 824 825 if (tranState != null) { 826 827 switch (tranState.state) { 828 829 832 case TransactionState.STATE_ACTIVE : 833 if( rollbackOnly ) 834 result = Status.StatusMarkedRollback; 835 else 836 result = Status.StatusActive; 837 break; 838 839 842 case TransactionState.STATE_PREPARED_SUCCESS : 843 case TransactionState.STATE_PREPARED_FAIL : 844 case TransactionState.STATE_PREPARED_READONLY : 845 result = Status.StatusPrepared; 846 break; 847 848 851 case TransactionState.STATE_NONE : 852 result = Status.StatusNoTransaction; 853 break; 854 case TransactionState.STATE_PREPARING : 855 case TransactionState.STATE_COMMITTING_ONE_PHASE : 856 result = Status.StatusPreparing; 857 break; 858 case TransactionState.STATE_COMMITTING : 859 result = Status.StatusCommitting; 860 break; 861 case TransactionState.STATE_COMMITTED : 862 case TransactionState.STATE_COMMITTED_ONE_PHASE_OK : 863 case TransactionState.STATE_COMMIT_ONE_PHASE_HEURISTIC_HAZARD : 864 case TransactionState.STATE_COMMIT_ONE_PHASE_HEURISTIC_MIXED : 865 result = Status.StatusCommitted; 866 break; 867 case TransactionState.STATE_ROLLING_BACK : 868 case TransactionState.STATE_COMMIT_ONE_PHASE_ROLLED_BACK : 869 result = Status.StatusRollingBack; 870 break; 871 case TransactionState.STATE_ROLLED_BACK : 872 result = Status.StatusRolledBack; 873 break; 874 875 878 default : 879 result = Status.StatusUnknown; 880 break; 881 } 882 } else { 883 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 884 MinorCode.Completed, 885 CompletionStatus.COMPLETED_NO); 886 throw exc; 887 } 888 889 return result; 890 } 891 892 905 public Status get_parent_status() { 906 Status result = get_status(); 907 return result; 908 } 909 910 923 public Status get_top_level_status() { 924 925 Status result = get_status(); 926 return result; 927 } 928 929 948 952 public boolean is_same_transaction(Coordinator other) 953 throws SystemException { 954 955 boolean result = false; 956 957 959 if (tranState != null) { 960 if (name == null) 961 name = superInfo.globalTID.toString(); 962 result = name.equals(other.get_transaction_name()); 963 } else { 964 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 965 MinorCode.Completed, 966 CompletionStatus.COMPLETED_NO); 967 throw exc; 968 } 969 970 return result; 971 } 972 973 990 public boolean is_related_transaction(Coordinator other) 991 throws SystemException { 992 993 boolean result = false; 994 995 if (tranState != null) { 996 result = other.is_descendant_transaction(this.object()); 997 } else { 998 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 999 MinorCode.Completed, 1000 CompletionStatus.COMPLETED_NO); 1001 throw exc; 1002 } 1003 1004 return result; 1005 } 1006 1007 1017 public boolean is_root_transaction() { 1018 1019 boolean result = root; 1020 1021 return result; 1022 } 1023 1024 1041 public boolean is_ancestor_transaction(Coordinator other) 1042 throws SystemException { 1043 1044 boolean result = false; 1045 if (tranState != null) { 1046 result = other.is_descendant_transaction(this.object()); 1047 } else { 1048 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 1049 MinorCode.Completed, 1050 CompletionStatus.COMPLETED_NO); 1051 throw exc; 1052 } 1053 1054 return result; 1055 } 1056 1057 1074 public boolean is_descendant_transaction(Coordinator other) 1075 throws SystemException { 1076 1077 boolean result = false; 1078 if (tranState != null) { 1079 result = is_same_transaction(other); 1080 } else { 1081 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 1082 MinorCode.Completed, 1083 CompletionStatus.COMPLETED_NO); 1084 throw exc; 1085 } 1086 1087 return result; 1088 } 1089 1090 1105 public boolean is_top_level_transaction() { 1106 1107 boolean result = true; 1108 return result; 1109 } 1110 1111 1123 1127 public int hash_transaction() { 1128 1129 int result = hash; 1130 1131 if (tranState == null) { 1132 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 1133 MinorCode.Completed, 1134 CompletionStatus.COMPLETED_NO); 1135 throw exc; 1136 } 1137 1138 return result; 1139 } 1140 1141 1153 synchronized public int hash_top_level_tran() { 1154 1155 int result = hash; 1156 1157 if (tranState == null) { 1158 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 1159 MinorCode.Completed, 1160 CompletionStatus.COMPLETED_NO); 1161 1162 throw exc; 1163 } 1164 1165 return result; 1166 } 1167 1168 1188 synchronized public RecoveryCoordinator register_resource(Resource res) 1189 throws Inactive, TRANSACTION_ROLLEDBACK { 1190 1191 RecoveryCoordinator result = null; 1192 1193 1196 if (tranState == null || tranState.state != 1197 TransactionState.STATE_ACTIVE) { 1198 Inactive exc = new Inactive(); 1199 throw exc; 1200 } 1201 1202 1204 if (rollbackOnly) { 1205 TRANSACTION_ROLLEDBACK exc = 1206 new TRANSACTION_ROLLEDBACK(0, CompletionStatus.COMPLETED_NO); 1207 throw exc; 1208 } 1209 1210 1215 if (!registered) { 1216 1217 1221 CoordinatorResourceImpl cImpl = 1222 new CoordinatorResourceImpl(superInfo.globalTID, this, false); 1223 1224 try { 1225 1226 1230 CoordinatorResource cRes = cImpl.object(); 1231 RecoveryCoordinator superRecovery = 1232 superInfo.superior.register_resource(cRes); 1233 superInfo.setRecovery(superRecovery); 1234 superInfo.setResource(cRes); 1235 registered = true; 1236 if(_logger.isLoggable(Level.FINEST)) 1237 { 1238 _logger.logp(Level.FINEST,"TopCoordinator","register_resource()", 1239 "CoordinatorResource " + cImpl + 1240 " has been registered with (Root)TopCoordinator"+ 1241 superInfo.globalTID.toString()); 1242 } 1243 1244 } catch (Exception exc) { 1245 1246 1250 cImpl.destroy(); 1251 1252 if (exc instanceof OBJECT_NOT_EXIST) { 1253 1254 TRANSACTION_ROLLEDBACK ex2 = 1257 new TRANSACTION_ROLLEDBACK( 1258 0, CompletionStatus.COMPLETED_NO); 1259 ex2.initCause(exc); 1260 throw ex2; 1261 } 1262 1263 if (exc instanceof Inactive) { 1264 throw (Inactive)exc; 1265 } 1266 1267 if (exc instanceof SystemException) { 1268 throw (SystemException)exc; 1269 } 1270 1271 1273 INTERNAL ex2 = new INTERNAL(MinorCode.NotRegistered, 1274 CompletionStatus.COMPLETED_NO); 1275 ex2.initCause(exc); 1276 throw ex2; 1277 } 1278 } 1279 1280 1282 if (participants == null) { 1283 participants = new RegisteredResources(logRecord, this); 1284 } 1285 1286 1290 int numRes = 0; 1295 if (res instanceof OTSResourceImpl) { 1296 numRes = participants.addRes(res); 1297 if(_logger.isLoggable(Level.FINEST)) 1298 { 1299 _logger.logp(Level.FINEST,"TopCoordinator","register_resource()", 1300 "OTSResource " + res +" has been registered"+"GTID is:"+ 1301 superInfo.globalTID.toString()); 1302 } 1303 1304 } else { 1305 numRes = participants.addRes((Resource)res._duplicate()); 1306 } 1307 1308 temporary = false; 1309 1310 1313 if (!(res instanceof OTSResourceImpl)) { 1316 RecoveryCoordinatorImpl rcImpl = null; 1317 try { 1318 rcImpl = new RecoveryCoordinatorImpl( 1319 superInfo.globalTID, numRes); 1320 result = rcImpl.object(); 1321 } catch (Exception exc) { 1322 1323 1326 INTERNAL ex2 = new INTERNAL(MinorCode.RecCoordCreateFailed, 1327 CompletionStatus.COMPLETED_NO); 1328 throw ex2; 1329 } 1330 1331 if (recoveryCoordinatorList == null) { 1334 recoveryCoordinatorList = new Vector(); 1335 } 1336 recoveryCoordinatorList.add(rcImpl); 1337 } 1338 1339 return result; 1340 } 1341 1342 1356 synchronized public void register_subtran_aware( 1357 SubtransactionAwareResource sares) throws NotSubtransaction { 1358 1359 NotSubtransaction exc = new NotSubtransaction(); 1360 throw exc; 1361 } 1362 1363 1375 public void rollback_only() throws Inactive { 1376 1377 if (tranState == null || 1378 tranState.state != TransactionState.STATE_ACTIVE) { 1379 Inactive exc = new Inactive(); 1380 throw exc; 1381 } else { 1382 rollbackOnly = true; 1384 } 1385 } 1386 1387 1398 1402 public String get_transaction_name() { 1403 1404 String result = null; 1405 if (tranState != null) { 1406 if (name == null) 1407 name = superInfo.globalTID.toString(); 1408 result = new String (name); 1409 } else { 1410 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 1411 MinorCode.Completed, 1412 CompletionStatus.COMPLETED_NO); 1413 throw exc; 1414 } 1415 1416 return result; 1417 } 1418 1419 1432 synchronized public Control create_subtransaction() throws Inactive { 1433 1434 Control result = null; 1435 1436 1439 if (tranState == null || 1440 tranState.state != TransactionState.STATE_ACTIVE) { 1441 Inactive exc = new Inactive(); 1442 throw exc; 1443 } 1444 1445 1451 CoordinatorImpl[] ancestors = new CoordinatorImpl[1]; 1452 ancestors[0] = this; 1453 1454 1460 SubCoordinator child = null; 1461 TerminatorImpl terminator = null; 1462 try { 1463 child = new SubCoordinator(superInfo.globalTID, 1464 superInfo.localTID, 1465 ancestors); 1466 1467 1471 terminator = new TerminatorImpl(child,true); 1472 1473 1476 result = new ControlImpl(terminator, child, 1477 new GlobalTID(child.getGlobalTID()), 1478 new Long (child.getLocalTID()) 1479 ).object(); 1480 } catch (Throwable exc) { 1481 Inactive ex2 = new Inactive(); 1482 throw ex2; 1483 } 1484 1485 1488 if (nestingInfo == null) { 1489 nestingInfo = new NestingInfo(); 1490 } 1491 1492 nestingInfo.addChild(child); 1493 1494 return result; 1495 } 1496 1497 1513 public otid_t getGlobalTID() { 1514 1515 otid_t result = superInfo.globalTID.realTID; 1516 return result; 1517 } 1518 1519 public GlobalTID getGlobalTid() { 1520 return superInfo.globalTID; 1521 } 1522 1523 public int getParticipantCount() { 1524 if (participants == null) { 1525 return 0; 1526 } 1527 return participants.numRegistered(); 1528 } 1529 1530 1541 public long getLocalTID() { 1542 1543 long result = superInfo.localTID.longValue(); 1544 return result; 1545 } 1546 1547 1565 synchronized CoordinatorImpl replyAction(int[] action) 1566 throws SystemException { 1567 1568 CoordinatorImpl result = null; 1569 action[0] = CoordinatorImpl.doNothing; 1570 1571 1575 if (!root && nestingInfo != null && nestingInfo.replyCheck()) { 1576 1577 action[0] = CoordinatorImpl.activeChildren; 1578 1579 1582 } else { 1583 1584 1587 if (!registered) { 1588 if (participants != null && participants.involved()) { 1589 1590 INTERNAL ex2 = new INTERNAL(MinorCode.NotRegistered, 1591 CompletionStatus.COMPLETED_NO); 1592 throw ex2; 1593 } else if (!registeredSync) { 1594 action[0] = forgetMe; 1595 } 1596 } 1597 1598 1601 if (!registeredSync) { 1602 if (synchronizations != null && synchronizations.involved()) { 1603 1604 INTERNAL ex2 = new INTERNAL(MinorCode.NotRegistered, 1605 CompletionStatus.COMPLETED_NO); 1606 throw ex2; 1607 } else if (action[0] == doNothing && !registered) { 1608 action[0] = forgetMe; 1614 } 1615 } 1616 } 1617 1618 1620 result = null; 1621 1622 return result; 1623 } 1624 1625 1634 synchronized Long setPermanent() { 1635 1636 Long result = superInfo.localTID; 1637 temporary = false; 1638 return result; 1639 } 1640 1641 1650 synchronized public boolean isRollbackOnly() { 1651 1652 boolean result = rollbackOnly; 1653 return result; 1654 } 1655 1656 1665 synchronized boolean isActive() { 1666 1667 boolean result = (tranState.state == TransactionState.STATE_ACTIVE); 1668 return result; 1669 } 1670 1671 1680 synchronized boolean hasRegistered() { 1681 1682 boolean result = registered || registeredSync; 1683 return result; 1684 } 1685 1686 1695 synchronized public TransIdentity[] getAncestors() { 1696 return null; 1697 } 1698 1699 1709 synchronized boolean addChild(CoordinatorImpl child) { 1710 1711 boolean result; 1712 1713 1716 if (nestingInfo == null) { 1717 nestingInfo = new NestingInfo(); 1718 } 1719 1720 result = nestingInfo.addChild(child); 1721 1722 return result; 1723 } 1724 1725 1737 synchronized boolean removeChild(CoordinatorImpl child) { 1738 1739 boolean result = false; 1740 1741 1745 if (nestingInfo != null) { 1746 result = nestingInfo.removeChild(child); 1747 } 1748 1749 1753 if (temporary && !registered && 1754 !(participants != null && participants.involved()) && 1755 !(synchronizations != null && synchronizations.involved()) && 1756 !(nestingInfo != null && nestingInfo.numChildren() > 0)) { 1757 cleanUpEmpty(null); 1758 } 1759 1760 return result; 1761 } 1762 1763 static String [] resultName = { "Commit", "Rollback", "Read-only" }; 1764 1765 1788 Vote prepare() 1789 throws INVALID_TRANSACTION, HeuristicMixed, HeuristicHazard { 1790 1791 Vote result = Vote.VoteRollback; 1792 1793 1795 synchronized(this) { 1796 1797 1801 if (root && nestingInfo != null && 1802 nestingInfo.numChildren() != 0) { 1803 INVALID_TRANSACTION exc = 1804 new INVALID_TRANSACTION( 1805 MinorCode.UnfinishedSubtransactions, 1806 CompletionStatus.COMPLETED_NO); 1807 throw exc; 1808 } 1809 1810 1812 if (!tranState.setState(TransactionState.STATE_PREPARING)) { 1813 return Vote.VoteRollback; 1814 } 1815 1816 1818 if (rollbackOnly) { 1819 1820 1822 if (!tranState. 1823 setState(TransactionState.STATE_PREPARED_FAIL)) { 1824 } 1826 1827 return Vote.VoteRollback; 1828 } 1829 1830 1834 } 1836 1840 Vote overallResult = Vote.VoteReadOnly; 1841 Throwable heuristicExc = null; 1842 1843 if (participants != null) { 1844 1845 try { 1846 1847 overallResult = participants.distributePrepare(); 1848 if (overallResult == Vote.VoteCommit || 1849 overallResult == Vote.VoteReadOnly) { 1850 1851 if (participants.getLAOResource() != null) { 1852 if (logRecord == null) { 1853 if (!(LogDBHelper.getInstance().addRecord( 1854 tranState.localTID.longValue(), 1855 tranState.globalTID.toTidBytes()))) { 1856 overallResult = Vote.VoteRollback; 1857 } 1858 } 1859 if (overallResult != Vote.VoteRollback) { 1860 participants.getLAOResource().commit(); 1861 } 1862 } 1863 } 1864 } catch (Throwable exc) { 1865 1866 1870 if (exc instanceof HeuristicMixed || 1871 exc instanceof HeuristicHazard) { 1872 1873 if (!tranState. 1874 setState(TransactionState.STATE_ROLLED_BACK)) { 1875 } 1877 1878 1895 1896 if (!root) { 1900 afterCompletion(Status.StatusRolledBack); 1901 } 1902 1903 1904 1905 1906 1907 1908 if (heuristicExc instanceof HeuristicMixed) { 1909 throw (HeuristicMixed)heuristicExc; 1910 } else { 1911 throw (HeuristicHazard)heuristicExc; 1912 } 1913 } 1914 1915 1917 overallResult = Vote.VoteRollback; 1918 1919 } 1921 } 1923 1925 synchronized(this) { 1926 1927 1930 if (rollbackOnly) { 1931 overallResult = Vote.VoteRollback; 1932 } 1933 1934 1935 1941 if (overallResult == Vote.VoteReadOnly) { 1942 1943 if (!tranState. 1944 setState(TransactionState.STATE_PREPARED_READONLY)) { 1945 overallResult = Vote.VoteRollback; 1946 } 1947 1948 1965 1966 if (!root) { 1974 afterCompletion(Status.StatusUnknown); 1975 } 1976 } else if (overallResult == Vote.VoteCommit) { 1977 1978 1980 int timeoutType = TimeoutManager.NO_TIMEOUT; 1981 1982 1987 if (!root) { 1988 timeoutType = TimeoutManager.IN_DOUBT_TIMEOUT; 1989 } 1990 1991 TimeoutManager.setTimeout(superInfo.localTID, timeoutType, 60); 1992 1993 1995 if (!tranState. 1996 setState(TransactionState.STATE_PREPARED_SUCCESS)) { 1997 overallResult = Vote.VoteRollback; 1998 } 1999 } else { 2000 2001 2005 if (!tranState. 2006 setState(TransactionState.STATE_PREPARED_FAIL)) { 2007 overallResult = Vote.VoteRollback; 2008 } 2009 } 2010 } 2011 2012 return overallResult; 2013 } 2014 2015 2035 void commit() throws HeuristicMixed, HeuristicHazard, NotPrepared { 2036 2037 2039 2040 synchronized(this) { 2041 if(_logger.isLoggable(Level.FINE)) 2042 { 2043 _logger.logp(Level.FINE,"TopCoordinator","commit()", 2044 "Within TopCoordinator.commit()"+"GTID is :"+ 2045 superInfo.globalTID.toString()); 2046 } 2047 2048 2051 if (tranState.state == TransactionState.STATE_PREPARED_READONLY) { 2052 return; 2053 } 2054 2055 2065 if (tranState.state == TransactionState.STATE_COMMITTED) { 2066 return; 2067 } 2068 2069 2071 if (!tranState.setState(TransactionState.STATE_COMMITTING)) { 2072 _logger.log(Level.SEVERE,"jts.transaction_wrong_state","commit"); 2073 String msg = LogFormatter.getLocalizedMessage(_logger, 2074 "jts.transaction_wrong_state", 2075 new java.lang.Object [] { "commit"}); 2076 throw new org.omg.CORBA.INTERNAL (msg); 2077 } 2081 2082 2084 } 2085 2086 2089 Throwable heuristicExc = null; 2090 if (participants != null) { 2091 try { 2092 participants.distributeCommit(); 2093 } catch (Throwable exc) { 2094 if (exc instanceof HeuristicMixed || 2095 exc instanceof HeuristicHazard) { 2096 heuristicExc = exc; 2097 } 2098 2099 if (exc instanceof INTERNAL) { 2102 throw (INTERNAL) exc; 2103 } 2104 } 2105 } 2106 2107 2109 synchronized(this) { 2110 2111 2113 2115 if (!tranState.setState(TransactionState.STATE_COMMITTED)) { 2116 _logger.log(Level.SEVERE,"jts.transaction_wrong_state","commit"); 2117 String msg = LogFormatter.getLocalizedMessage(_logger, 2118 "jts.transaction_wrong_state", 2119 new java.lang.Object [] { "commit"}); 2120 throw new org.omg.CORBA.INTERNAL (msg); 2121 } 2122 2123 2135 if (terminator != null) { 2136 terminator.setCompleted(false, heuristicExc != null); 2137 } 2138 2139 2153 2154 if (!root) { 2158 afterCompletion(Status.StatusCommitted); 2159 } 2160 2161 2162 2163 2164 2165 2166 2168 if (heuristicExc != null) { 2169 if (heuristicExc instanceof HeuristicMixed) { 2170 throw (HeuristicMixed) heuristicExc; 2171 } else { 2172 throw (HeuristicHazard) heuristicExc; 2173 } 2174 } 2175 } 2176 } 2177 2178 2197 void rollback(boolean force) throws HeuristicMixed, HeuristicHazard { 2198 2199 2201 synchronized(this){ 2202 if(_logger.isLoggable(Level.FINE)) 2203 { 2204 _logger.logp(Level.FINE,"TopCoordinator","rollback()", 2205 "Within TopCoordinator.rollback() :"+"GTID is : "+ 2206 superInfo.globalTID.toString()); 2207 } 2208 2209 2211 if (tranState == null) { 2212 return; 2213 } 2214 2215 2220 if (tranState.state == TransactionState.STATE_ROLLED_BACK) { 2221 return; 2222 } 2223 2224 2234 if (tranState.state == TransactionState.STATE_COMMITTED) { 2235 return; 2236 } 2237 2238 2242 if (!force && ((tranState.state == 2243 TransactionState.STATE_PREPARED_SUCCESS) || 2244 (!tranState.setState( 2245 TransactionState.STATE_ROLLING_BACK)) 2246 )) { 2247 return; 2248 } 2249 2250 2255 if( !temporary && 2256 !tranState.setState(TransactionState.STATE_ROLLING_BACK)) { 2257 } 2259 2260 2264 if (nestingInfo != null) { 2265 nestingInfo.rollbackFamily(); 2266 } 2267 2268 2270 } 2271 2272 2275 Throwable heuristicExc = null; 2276 if (participants != null) { 2277 try { 2278 participants.distributeRollback(false); 2279 } catch(Throwable exc) { 2280 2281 if (exc instanceof HeuristicMixed || 2282 exc instanceof HeuristicHazard) { 2283 heuristicExc = exc; 2284 } 2285 2286 if (exc instanceof INTERNAL) { 2288 throw (INTERNAL) exc; 2289 } 2290 } 2291 } 2292 2293 2295 synchronized(this) { 2296 2297 2300 if (!temporary && 2301 !tranState.setState(TransactionState.STATE_ROLLED_BACK)) { 2302 } 2304 2305 2319 if (terminator != null) { 2320 terminator.setCompleted(true, heuristicExc != null); 2321 } 2322 2323 2340 if (!root) { 2344 afterCompletion(Status.StatusRolledBack); 2345 } 2346 2347 2348 2349 2350 2351 2353 if (heuristicExc != null) { 2354 if (heuristicExc instanceof HeuristicMixed) { 2355 throw (HeuristicMixed) heuristicExc; 2356 } else { 2357 throw (HeuristicHazard) heuristicExc; 2358 } 2359 } 2360 } 2361 2362 } 2364 2365 2385 synchronized public void register_synchronization(Synchronization sync) 2386 throws SystemException, Inactive, SynchronizationUnavailable { 2387 2388 2391 if (tranState == null || 2392 tranState.state != TransactionState.STATE_ACTIVE) { 2393 Inactive exc = new Inactive(); 2394 throw exc; 2395 } 2396 2397 2402 if (!registeredSync) { 2403 2404 2407 CoordinatorSynchronizationImpl sImpl = 2408 new CoordinatorSynchronizationImpl(this); 2409 2410 2412 try { 2413 Synchronization subSync = sImpl.object(); 2414 superInfo.superior.register_synchronization(subSync); 2415 registeredSync = true; 2416 2417 this.coordSyncImpl = sImpl; 2419 if(_logger.isLoggable(Level.FINER)) 2420 { 2421 _logger.logp(Level.FINER,"TopCoordinator", 2422 "register_synchronization()", 2423 "CoordinatorSynchronizationImpl :" + sImpl + 2424 " has been registered with (Root)TopCoordinator"+ 2425 "GTID is: "+ superInfo.globalTID.toString()); 2426 } 2427 2428 } catch (Exception exc) { 2429 sImpl.destroy(); 2431 2432 2435 if (exc instanceof OBJECT_NOT_EXIST) { 2436 TRANSACTION_ROLLEDBACK ex2 = 2437 new TRANSACTION_ROLLEDBACK( 2438 0, CompletionStatus.COMPLETED_NO); 2439 throw ex2; 2440 } 2441 2442 if (exc instanceof Inactive) { 2443 throw (Inactive)exc; 2444 } 2445 2446 if (exc instanceof SystemException) { 2447 throw (SystemException) exc; 2448 } 2449 2450 2452 INTERNAL ex2 = new INTERNAL(MinorCode.NotRegistered, 2453 CompletionStatus.COMPLETED_NO); 2454 throw ex2; 2455 } 2456 } 2457 2458 2460 if (synchronizations == null) { 2461 synchronizations = new RegisteredSyncs(); 2462 } 2463 2464 2469 if (sync instanceof com.sun.jts.jta.SynchronizationImpl) { 2474 synchronizations.addSync(sync); 2475 2476 if(_logger.isLoggable(Level.FINER)) 2477 { 2478 _logger.logp(Level.FINER,"TopCoordinator", 2479 "register_synchronization()", 2480 "SynchronizationImpl :" + sync + 2481 " has been registeredwith TopCoordinator :"+ 2482 "GTID is : "+ superInfo.globalTID.toString().toString()); 2483 } 2484 2485 } else { 2486 synchronizations.addSync((Synchronization) sync._duplicate()); 2487 } 2488 2489 temporary = false; 2490 } 2491 2492 2507 synchronized void beforeCompletion() throws INVALID_TRANSACTION { 2508 2509 2513 if (root && nestingInfo != null && nestingInfo.numChildren() != 0) { 2514 INVALID_TRANSACTION exc = new INVALID_TRANSACTION( 2515 MinorCode.UnfinishedSubtransactions, 2516 CompletionStatus.COMPLETED_NO); 2517 throw exc; 2518 } 2519 2520 2523 if (synchronizations != null) { 2524 2525 2529 if (!synchronizations.distributeBefore()) { 2530 rollbackOnly = true; 2531 } 2532 } 2533 } 2534 2535 2547 synchronized void afterCompletion(Status status) { 2548 2549 2552 if (tranState.state == TransactionState.STATE_ACTIVE) { 2553 tranState.setState(TransactionState.STATE_PREPARING); 2554 tranState.setState(TransactionState.STATE_PREPARED_READONLY); 2555 } 2556 2557 2560 if (synchronizations != null) { 2561 2562 2565 synchronizations.distributeAfter(status); 2567 } 2568 2569 boolean aborted = true; 2572 if (status == Status.StatusCommitted) { 2573 aborted = false; 2574 } 2575 if (!delegated) { 2576 RecoveryManager.removeCoordinator(superInfo.globalTID, 2577 superInfo.localTID, 2578 aborted); 2579 } else { 2580 DelegatedRecoveryManager.removeCoordinator(superInfo.globalTID, 2581 superInfo.localTID, 2582 aborted, logPath); 2583 } 2584 2585 if (recoveryCoordinatorList != null) { 2587 for (int i = 0; i < recoveryCoordinatorList.size(); i++) { 2588 RecoveryCoordinatorImpl rcImpl = (RecoveryCoordinatorImpl) 2589 recoveryCoordinatorList.elementAt(i); 2590 rcImpl.destroy(); 2591 } 2592 recoveryCoordinatorList = null; 2593 } 2594 2595 if (this.coordSyncImpl != null) { 2598 this.coordSyncImpl.destroy(); 2599 } 2600 this.synchronizations = null; 2601 2602 destroy(); 2604 } 2605 2606 2619 synchronized void setTerminator(CompletionHandler term) { 2620 terminator = term; 2621 } 2622 2623 2633 Coordinator getParent() { 2634 2635 Coordinator result = null; 2636 return result; 2637 } 2638 2639 2648 Coordinator getSuperior() { 2649 2650 Coordinator result = superInfo.superior; 2651 return result; 2652 } 2653 2654 2664 2684 2685 2695 CompletionHandler getTerminator() { 2696 2697 CompletionHandler result = terminator; 2698 return result; 2699 } 2700 2701 2717 void directRegisterResource(Resource res) { 2718 2719 2725 if (participants == null) { 2726 participants = new RegisteredResources(null, this); 2727 } 2728 2729 2732 participants.addRes(res); 2733 if(_logger.isLoggable(Level.FINE)) 2734 { 2735 _logger.logp(Level.FINE,"TopCoordinator","directRegisterResource()", 2736 "Registered resource :" + res ); 2737 } 2738 2739 } 2740 2741 private static Any emptyData = null; 2742 2743 2756 synchronized public PropagationContext get_txcontext() throws Unavailable { 2757 2758 2761 if (tranState == null || 2762 tranState.state != TransactionState.STATE_ACTIVE || 2763 rollbackOnly ) { 2764 Unavailable exc = new Unavailable(); 2765 throw exc; 2766 } 2767 2768 2775 long timeLeft = TimeoutManager.timeLeft(superInfo.localTID); 2776 int timeout = 0; 2777 if (timeLeft > 0) { 2778 2779 timeout = (int)timeLeft/1000; 2780 2781 } else if (timeLeft == 0) { 2782 2783 2787 TimeoutManager.timeoutCoordinator(superInfo.localTID, 2788 TimeoutManager.ACTIVE_TIMEOUT); 2789 TRANSACTION_ROLLEDBACK exc = new TRANSACTION_ROLLEDBACK( 2790 0, CompletionStatus.COMPLETED_NO); 2791 throw exc; 2792 } 2793 2794 2797 TransIdentity current = new TransIdentity(this.object(), 2798 null, 2799 superInfo.globalTID.realTID); 2800 2801 2803 if (emptyData == null){ 2804 emptyData = Configuration.getORB().create_any(); 2805 emptyData.insert_boolean(false); 2806 } 2807 2808 PropagationContext result = new PropagationContext( 2809 timeout, current, 2810 new TransIdentity[0], emptyData); 2811 if(_logger.isLoggable(Level.FINEST)) 2812 { 2813 _logger.logp(Level.FINEST,"TopCoordinator","get_txcontext()", 2814 "Obtained PropagationContext"+"GTID is: "+ 2815 superInfo.globalTID.toString()); 2816 } 2817 2818 return result; 2819 } 2820 2821 2830 void cleanUpEmpty(CoordinatorImpl parent) { 2831 2832 2834 try { 2835 rollback(true); 2836 } catch( Throwable exc ) {} 2837 } 2838 2839 2850 2858 boolean commitOnePhase() throws HeuristicMixed, HeuristicHazard { 2859 2860 synchronized (this) { 2861 2862 2866 if (root && nestingInfo != null && 2867 nestingInfo.numChildren() != 0) { 2868 INVALID_TRANSACTION exc = 2869 new INVALID_TRANSACTION( 2870 MinorCode.UnfinishedSubtransactions, 2871 CompletionStatus.COMPLETED_NO); 2872 throw exc; 2873 } 2874 2875 2876 2878 if (participants != null && participants.numRegistered() > 1) { 2879 return false; 2881 } 2882 2883 2885 if (!tranState. 2886 setState(TransactionState.STATE_COMMITTING_ONE_PHASE)) { 2887 return false; 2888 } 2889 2890 2896 if (rollbackOnly) { 2897 return false; 2898 } 2899 2900 int timeoutType = TimeoutManager.NO_TIMEOUT; 2901 2902 2906 if (root) { 2907 TimeoutManager.setTimeout(superInfo.localTID, 2908 TimeoutManager.NO_TIMEOUT, 2909 60); 2910 } 2911 else { 2912 TimeoutManager.setTimeout(superInfo.localTID, 2913 TimeoutManager.IN_DOUBT_TIMEOUT, 2914 60); 2915 } 2916 2917 2918 2923 } 2925 2926 if ((participants != null) && (participants.numRegistered() == 1)) { 2927 Throwable heuristicExc = null; 2928 boolean rolled_back = false; 2929 try { 2930 participants.commitOnePhase(); 2931 } catch (Throwable exc) { 2932 2933 if (exc instanceof HeuristicMixed) { 2934 if (!tranState.setState( 2938 TransactionState. 2939 STATE_COMMIT_ONE_PHASE_HEURISTIC_MIXED)) { 2940 _logger.log(Level.SEVERE,"jts.transaction_wrong_state", 2941 "COMMIT_ONE_PHASE (1)"); 2942 String msg = LogFormatter.getLocalizedMessage(_logger, 2943 "jts.transaction_wrong_state", 2944 new java.lang.Object [] 2945 { "COMMIT_ONE_PHASE (1)"}); 2946 throw new org.omg.CORBA.INTERNAL (msg); 2947 } 2948 throw (HeuristicMixed)exc; 2950 } else if (exc instanceof HeuristicHazard) { 2952 if (!tranState.setState( 2956 TransactionState. 2957 STATE_COMMIT_ONE_PHASE_HEURISTIC_HAZARD)) { 2958 _logger.log(Level.SEVERE,"jts.transaction_wrong_state", 2959 "COMMIT_ONE_PHASE (2)"); 2960 String msg = LogFormatter.getLocalizedMessage(_logger, 2961 "jts.transaction_wrong_state", 2962 new java.lang.Object [] 2963 { "COMMIT_ONE_PHASE (2)"}); 2964 throw new org.omg.CORBA.INTERNAL (msg); 2965 } 2966 throw (HeuristicHazard)exc; 2968 } else if (exc instanceof TRANSACTION_ROLLEDBACK) { 2970 rolled_back = true; 2971 2972 } else if (exc instanceof INTERNAL) { 2979 throw (INTERNAL) exc; 2981 } 2983 2986 } 2988 2990 if (rolled_back) { 2992 2993 2996 if (!tranState.setState( 2999 TransactionState. 3000 STATE_COMMIT_ONE_PHASE_ROLLED_BACK)) { 3001 _logger.log(Level.SEVERE,"jts.transaction_wrong_state", 3002 "COMMIT_ONE_PHASE (4)"); 3003 String msg = LogFormatter.getLocalizedMessage(_logger, 3004 "jts.transaction_wrong_state", 3005 new java.lang.Object [] 3006 { "COMMIT_ONE_PHASE (4)"}); 3007 throw new org.omg.CORBA.INTERNAL (msg); 3008 } 3009 3020 } else { 3022 3026 if (!tranState.setState(TransactionState. 3030 STATE_COMMITTED_ONE_PHASE_OK)) { 3031 _logger.log(Level.SEVERE,"jts.transaction_wrong_state", 3032 "COMMIT_ONE_PHASE (6)"); 3033 String msg = LogFormatter.getLocalizedMessage(_logger, 3034 "jts.transaction_wrong_state", 3035 new java.lang.Object [] { "COMMIT_ONE_PHASE (6)"}); 3036 throw new org.omg.CORBA.INTERNAL (msg); 3037 } 3038 3050 } 3052 3053 3055 synchronized (this) { 3056 3057 3072 if (terminator != null) { 3073 terminator.setCompleted(false, heuristicExc != null); 3074 } 3075 3076 3090 3091 if (!root) { 3095 afterCompletion(Status.StatusCommitted); 3096 } 3097 3098 3099 3100 3101 3102 3104 if (heuristicExc != null) { 3105 if (heuristicExc instanceof HeuristicMixed) { 3106 throw (HeuristicMixed)heuristicExc; 3107 } else { 3108 throw (HeuristicHazard)heuristicExc; 3109 } 3110 } 3111 3112 3114 if (rolled_back) { 3115 TRANSACTION_ROLLEDBACK exc = 3116 new TRANSACTION_ROLLEDBACK( 3117 0, CompletionStatus.COMPLETED_YES); 3118 throw exc; 3119 } 3120 3121 } 3123 3125 return true; 3126 3127 } else { 3128 3131 if (!tranState. 3137 setState(TransactionState.STATE_COMMITTED_ONE_PHASE_OK)) { 3138 _logger.log(Level.SEVERE,"jts.transaction_wrong_state", 3139 "COMMIT_ONE_PHASE (8)"); 3140 String msg = LogFormatter.getLocalizedMessage(_logger, 3141 "jts.transaction_wrong_state", 3142 new java.lang.Object [] { "COMMIT_ONE_PHASE (8)"}); 3143 throw new org.omg.CORBA.INTERNAL (msg); 3144 } 3145 3146 3157 } 3159 return true; 3160 } 3161 3162 3174 public int hashCode() { 3175 if (hash == 0 && superInfo != null && superInfo.globalTID != null) { 3176 hash = superInfo.globalTID.hashCode(); 3177 } 3178 3179 return hash; 3180 } 3181 3182 3191 public boolean equals(java.lang.Object other) { 3192 3193 3195 if( this == other ) return true; 3196 3197 3199 otid_t otherTID = null; 3200 3201 3204 if (other instanceof CoordinatorImpl) { 3205 if (other instanceof TopCoordinator) { 3206 otherTID = ((TopCoordinator) other). 3207 superInfo.globalTID.realTID; 3208 } 3209 } else if (other instanceof org.omg.CORBA.Object ) { 3210 3211 3214 try { 3215 JCoordinator jcoord = JCoordinatorHelper. 3216 narrow((org.omg.CORBA.Object ) other); 3217 otherTID = jcoord.getGlobalTID(); 3218 } catch (BAD_PARAM exc) { 3219 3220 3226 try { 3227 Coordinator coord = 3228 CoordinatorHelper.narrow((org.omg.CORBA.Object ) other); 3229 PropagationContext pc = coord.get_txcontext(); 3230 otherTID = pc.current.otid; 3231 } catch (BAD_PARAM ex2) { 3232 } catch (Unavailable ex2) { 3235 INVALID_TRANSACTION ex3 = new INVALID_TRANSACTION( 3240 MinorCode.CompareFailed, 3241 CompletionStatus.COMPLETED_NO); 3242 throw ex3; 3243 } 3244 } 3245 } 3246 3247 3249 if (otherTID != null) { 3250 return superInfo.globalTID.equals(otherTID); 3251 } 3252 3253 return false; 3254 } 3255} 3256 | Popular Tags |