use of com.arjuna.ats.arjuna.TopLevelAction in project narayana by jbosstm.
the class InvocationHandler method invoke.
public Object invoke(Object proxy, java.lang.reflect.Method method, Object[] args) throws Throwable {
if (_txObject == null)
throw new LockException("Transactional object is null!");
AtomicAction currentTx = null;
synchronized (_txObject) {
synchronized (_theObject) {
AtomicAction act = null;
if (_nestedTransactions) {
act = new AtomicAction();
act.begin();
} else {
if (_nestedTopLevel) {
act = new TopLevelAction();
act.begin();
}
}
try {
LockInformation cachedLock = _cachedMethods.get(method);
if (BasicAction.Current() != null) {
Method theMethod = null;
/*
* Look for the corresponding method in the original object and
* check the annotations applied there.
*
* Check to see if we've cached this before.
*/
int lockType = -1;
boolean lockFree = false;
boolean transactionFree = false;
if (cachedLock == null) {
for (Method mt : _methods) {
if (mt.getName().equals(method.getName())) {
if (mt.getReturnType().equals(method.getReturnType())) {
if (Arrays.equals(mt.getParameterTypes(), method.getParameterTypes()))
theMethod = mt;
}
}
}
if (theMethod == null)
throw new LockException("Could not locate method " + method);
if (theMethod.isAnnotationPresent(ReadLock.class))
lockType = LockMode.READ;
else {
if (theMethod.isAnnotationPresent(WriteLock.class))
lockType = LockMode.WRITE;
else {
if (theMethod.isAnnotationPresent(TransactionFree.class))
transactionFree = true;
else {
if (theMethod.isAnnotationPresent(LockFree.class))
lockFree = true;
}
}
}
if (!lockFree && !transactionFree) {
int timeout = LockManager.defaultSleepTime;
int retry = LockManager.defaultRetry;
if (theMethod.isAnnotationPresent(Timeout.class))
timeout = theMethod.getAnnotation(Timeout.class).period();
if (theMethod.isAnnotationPresent(Retry.class))
retry = theMethod.getAnnotation(Retry.class).count();
if (// default to WRITE
lockType == -1)
lockType = LockMode.WRITE;
cachedLock = new LockInformation(lockType, timeout, retry);
_cachedMethods.put(method, cachedLock);
} else {
if (transactionFree)
currentTx = AtomicAction.suspend();
}
}
if (!lockFree && !transactionFree) {
int result = _txObject.setlock((_optimistic ? new OptimisticLock(cachedLock._lockType) : new Lock(cachedLock._lockType)), cachedLock._retry, cachedLock._timeout);
if (result != GRANTED) {
throw new LockException(Thread.currentThread() + " could not set " + LockMode.stringForm(cachedLock._lockType) + " lock. Got: " + LockResult.stringForm(result));
}
}
}
try {
return method.invoke(_theObject, args);
} catch (InvocationTargetException e) {
if (txojLogger.logger.isTraceEnabled()) {
Throwable ae = e.getCause() != null ? e.getCause() : e;
txojLogger.logger.tracef("STM InvocationHandler::invoke application method %s threw exception %s", method.getName(), ae.getMessage());
}
throw e.getCause() != null ? e.getCause() : e;
}
} finally {
if (act != null) {
int status = act.commit();
if ((status != ActionStatus.COMMITTED) && (status != ActionStatus.COMMITTING)) {
if (currentTx != null)
AtomicAction.resume(currentTx);
throw new TransactionException("Failed to commit container transaction!", status);
}
}
if (currentTx != null)
AtomicAction.resume(currentTx);
}
}
}
}
use of com.arjuna.ats.arjuna.TopLevelAction in project narayana by jbosstm.
the class TopLevelActionUnitTest method test.
@Test
public void test() throws Exception {
AtomicAction A = new AtomicAction();
AtomicAction B = new AtomicAction();
TopLevelAction tl = new TopLevelAction();
// top level
A.begin();
// nested
B.begin();
// nested top level
tl.begin();
// not recommended in practice!
A.abort();
assertEquals(A.status(), ActionStatus.ABORTED);
assertEquals(B.status(), ActionStatus.ABORTED);
assertEquals(tl.status(), ActionStatus.RUNNING);
tl.abort();
}
use of com.arjuna.ats.arjuna.TopLevelAction in project narayana by jbosstm.
the class NestedTopLevelAction method test.
@Test
public void test() {
AtomicAction A = new AtomicAction();
TopLevelAction B = new TopLevelAction();
AtomicAction C = new AtomicAction();
AtomicObject foo1 = new AtomicObject();
AtomicObject foo2 = new AtomicObject();
try {
A.begin();
foo1.set(5);
System.out.println("Current atomic object 1 state: " + foo1.get());
System.out.println("\nStarting nested top-level action.");
B.begin();
System.out.println(B);
foo2.set(7);
System.out.println("Current atomic object 2 state: " + foo2.get());
System.out.println("\nCommitting nested top-level action.");
B.commit();
System.out.println("\nAborting top-level action.");
A.abort();
C.begin();
int val1 = foo1.get();
int val2 = foo2.get();
System.out.println("\nFinal atomic object 1 state: " + val1);
assertEquals(0, val1);
System.out.println("\nFinal atomic object 2 state: " + val2);
assertEquals(7, val2);
C.commit();
} catch (TestException e) {
A.abort();
B.abort();
C.abort();
fail("AtomicObject exception raised.");
}
}
Aggregations