use of org.omg.CosTransactions.Terminator in project narayana by jbosstm.
the class StrictInterposition method createHierarchy.
protected synchronized ControlImple createHierarchy(PropagationContext ctx, Uid currentUid) throws SystemException {
/*
* Start at the parent and work our way down to "current". The current
* transaction is not in the IDL sequence, but sent as separate field
* of the propagation context. This tends to make the code more
* complex than it would be if the entire hierarchy was represented in
* one place.
*/
/*
* We only ever register the current transaction with its parent, and
* as each transaction commits, it registers its parent with the
* "real" parent.
*/
int depth = ctx.parents.length;
ServerResource action = null;
Coordinator tmpCoord = null;
Terminator tmpTerm = null;
if (depth == 0) {
tmpCoord = ctx.current.coord;
tmpTerm = ctx.current.term;
} else {
tmpCoord = ctx.parents[depth - 1].coord;
tmpTerm = ctx.parents[depth - 1].term;
}
if (// terminator my correctory be null
tmpCoord == null)
return null;
ServerControl control = ServerFactory.create_transaction(currentUid, null, null, tmpCoord, tmpTerm, ctx.timeout);
action = new ServerStrictTopLevelAction(control, ((depth == 0) ? true : false));
if (!action.valid()) {
try {
// does dispose as well!
((ServerStrictTopLevelAction) action).rollback();
action = null;
} catch (Exception e) {
}
throw new TRANSACTION_ROLLEDBACK();
}
ServerTopLevelAction newElement = (ServerStrictTopLevelAction) action;
_head.add(newElement);
if (// current is a nested transaction
depth > 0) {
/*
* Now deal with any nested transactions.
* As we create, register with the original transactions.
*/
ServerResource nestedAction = null;
for (int i = depth - 2; i >= 0; i--) {
tmpCoord = ctx.parents[i].coord;
tmpTerm = ctx.parents[i].term;
control = ServerFactory.create_subtransaction(Utility.otidToUid(ctx.parents[i].otid), tmpCoord, tmpTerm, control);
// not current, so don't register
nestedAction = new ServerStrictNestedAction(control, false);
if (!nestedAction.valid()) {
try {
// does dispose as well!
((ServerStrictNestedAction) nestedAction).rollback_subtransaction();
nestedAction = null;
} catch (Exception e) {
}
throw new TRANSACTION_ROLLEDBACK();
}
/*
* Add transaction resource to list.
*/
action.addChild((ServerStrictNestedAction) nestedAction);
action = nestedAction;
}
/*
* Now deal with current transaction. If there is
* only one transaction we do nothing.
*/
tmpCoord = ctx.current.coord;
tmpTerm = ctx.current.term;
control = ServerFactory.create_subtransaction(Utility.otidToUid(ctx.current.otid), tmpCoord, tmpTerm, control);
// current, so register
nestedAction = new ServerStrictNestedAction(control, true);
if (!nestedAction.valid()) {
try {
// does dispose as well!
((ServerStrictNestedAction) nestedAction).rollback_subtransaction();
nestedAction = null;
} catch (Exception e) {
}
throw new TRANSACTION_ROLLEDBACK();
}
action.addChild((ServerStrictNestedAction) nestedAction);
}
if (jtsLogger.logger.isTraceEnabled())
compareHierarchies(ctx, newElement);
return control;
}
use of org.omg.CosTransactions.Terminator in project narayana by jbosstm.
the class TerminationTest method test.
@Test
public void test() {
boolean commit = true;
Control myControl = null;
ORB myORB = null;
RootOA myOA = null;
try {
myORB = ORB.getInstance("test");
myOA = OA.getRootOA(myORB);
myORB.initORB(new String[] {}, null);
myOA.initOA();
ORBManager.setORB(myORB);
ORBManager.setPOA(myOA);
TransactionFactoryImple theOTS = new TransactionFactoryImple();
System.out.println("Creating transaction with 2 second timeout.");
myControl = theOTS.create(2);
assertNotNull(myControl);
Terminator handle = myControl.get_terminator();
try {
System.out.println("Sleeping for 5 seconds.");
Thread.sleep(5000);
} catch (Exception e) {
}
if (handle != null) {
System.out.print("Attempting to ");
if (commit)
System.out.println("commit transaction. Should fail!");
else
System.out.println("rollback transaction. Should fail!.");
if (commit)
handle.commit(true);
else
handle.rollback();
assertFalse(commit);
System.out.println("\nNow attempting to destroy transaction. Should fail!");
OTSManager.destroyControl(myControl);
} else
System.err.println("No transaction terminator!");
} catch (UserException e) {
System.err.println("Caught UserException: " + e);
} catch (SystemException e) {
System.err.println("Caught SystemException: " + e);
try {
Coordinator coord = myControl.get_coordinator();
Status s = coord.get_status();
System.err.println("Transaction status: " + com.arjuna.ats.jts.utils.Utility.stringStatus(s));
coord = null;
} catch (Exception ex) {
}
}
try {
CurrentImple current = OTSImpleManager.current();
FakeCheckedAction act = new FakeCheckedAction();
current.set_timeout(2);
current.setCheckedAction(act);
assertEquals(act, current.getCheckedAction());
System.out.println("\nNow creating current transaction with 2 second timeout.");
current.begin();
myControl = current.get_control();
try {
System.out.println("Sleeping for 5 seconds.");
Thread.sleep(5000);
} catch (Exception e) {
}
System.out.print("Attempting to ");
if (commit)
System.out.println("commit transaction. Should fail!");
else
System.out.println("rollback transaction. Should fail!.");
if (commit)
current.commit(true);
else
current.rollback();
assertFalse(commit);
assertTrue(act.called);
} catch (UserException e) {
System.err.println("Caught UserException: " + e);
System.out.println("Test did not completed successfully.");
} catch (SystemException e) {
System.err.println("Caught SystemException: " + e);
try {
Coordinator coord = myControl.get_coordinator();
Status s = coord.get_status();
System.err.println("Transaction status: " + com.arjuna.ats.jts.utils.Utility.stringStatus(s));
myControl = null;
coord = null;
} catch (Exception ex) {
}
System.out.println("Test completed successfully.");
}
myOA.destroy();
myORB.shutdown();
}
use of org.omg.CosTransactions.Terminator in project narayana by jbosstm.
the class TransactionTest1 method test.
@Test
public void test() throws Exception {
ORB myORB = null;
RootOA myOA = null;
myORB = ORB.getInstance("test");
myOA = OA.getRootOA(myORB);
myORB.initORB(new String[] {}, null);
myOA.initOA();
ORBManager.setORB(myORB);
ORBManager.setPOA(myOA);
TransactionFactory theFactory = OTSManager.get_factory();
Control topLevelControl = null;
Control nestedControl = null;
Terminator terminator = null;
try {
org.omg.CosTransactions.Current current = OTSManager.get_current();
current.begin();
topLevelControl = current.get_control();
current.begin();
nestedControl = current.get_control();
/*
* Try to commit top-level action without terminating nested
* action.
*/
terminator = topLevelControl.get_terminator();
System.out.println("\nAttempting to terminate out of sequence 'current'. Should fail!");
try {
terminator.commit(true);
fail();
} catch (TRANSACTION_ROLLEDBACK e) {
System.err.println("Commit failed! " + e);
}
System.out.println("\nNow attempting to terminate 'current' hierarchy. Should fail!");
try {
current.commit(true);
fail();
} catch (INVALID_TRANSACTION e1) {
System.err.println("Nested commit failed! " + e1);
} catch (TRANSACTION_ROLLEDBACK e1) {
System.err.println("Nested commit failed! " + e1);
} catch (Exception e2) {
fail("Nested commit threw unexpected exception: " + e2);
}
try {
current.commit(true);
fail();
} catch (TRANSACTION_ROLLEDBACK e1) {
System.err.println("Top-level commit failed! " + e1);
} catch (INVALID_TRANSACTION e2) {
System.err.println("Top-level commit failed! " + e2);
} catch (Exception e3) {
fail("Top-level commit threw unexpected exception: " + e3);
}
/*
* Now with the factory.
*/
System.out.println("\nNow creating new top-level action.");
topLevelControl = theFactory.create(0);
nestedControl = topLevelControl.get_coordinator().create_subtransaction();
terminator = topLevelControl.get_terminator();
System.out.println("\nAttempting to terminate out of sequence 'factory' action. Should fail!");
try {
terminator.commit(true);
fail();
} catch (TRANSACTION_ROLLEDBACK e1) {
System.err.println("Commit failed! " + e1);
} catch (INVALID_TRANSACTION e2) {
System.err.println("Commit failed! " + e2);
}
terminator = nestedControl.get_terminator();
System.out.println("\nNow attempting to terminate 'factory' nested action. Should fail!");
try {
terminator.commit(true);
fail();
} catch (TRANSACTION_ROLLEDBACK e1) {
System.err.println("Commit failed! " + e1);
} catch (INVALID_TRANSACTION e2) {
System.err.println("Commit failed! " + e2);
}
System.out.println("Test completed successfully.");
} catch (Exception e) {
fail("Test failed - received unexpected exception " + e);
}
myOA.destroy();
myORB.shutdown();
}
Aggregations