use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.
the class OptimisticLockUnitTest method testConflict.
public void testConflict() throws Exception {
Container<Atomic> theContainer1 = new Container<Atomic>();
Container<Atomic> theContainer2 = new Container<Atomic>();
final Atomic obj1 = theContainer1.create(new ExampleSTM());
AtomicAction act = new AtomicAction();
/*
* Make sure there's state on "disk" before we try to do anything
* with a shared instance.
*
* https://issues.jboss.org/browse/JBTM-1732
*/
act.begin();
obj1.set(10);
act.commit();
final Atomic obj2 = theContainer2.clone(new ExampleSTM(), obj1);
AtomicAction a = new AtomicAction();
a.begin();
obj1.set(1234);
AtomicAction.suspend();
AtomicAction b = new AtomicAction();
b.begin();
obj2.set(12345);
b.commit();
AtomicAction.resume(a);
assertEquals(a.commit(), ActionStatus.ABORTED);
AtomicAction c = new AtomicAction();
c.begin();
assertEquals(obj1.get(), 12345);
assertEquals(obj2.get(), 12345);
c.commit();
System.out.println("done");
}
use of com.arjuna.ats.arjuna.AtomicAction 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.AtomicAction in project narayana by jbosstm.
the class TypicalExampleUnitTest method test.
public void test() {
MyExample ex = new MyExample(10);
Container<Sample> theContainer = new Container<Sample>();
Sample obj1 = theContainer.create(ex);
Sample obj2 = theContainer.clone(new MyExample(), obj1);
assertTrue(obj2 != null);
AtomicAction act = new AtomicAction();
act.begin();
obj1.increment();
act.commit();
act = new AtomicAction();
act.begin();
assertEquals(obj2.value(), 11);
act.commit();
act = new AtomicAction();
act.begin();
for (int i = 0; i < 1000; i++) {
obj1.increment();
}
act.abort();
assertEquals(ex._numberOfTransactions, 1002);
}
use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.
the class AtomicArrayUnitTest method testTransaction.
@SuppressWarnings("unchecked")
public void testTransaction() {
AtomicAction act = new AtomicAction();
AtomicArray<Integer> a1 = ArrayFactory.instance().createArray(10);
assertEquals(a1.size(), 10);
for (int i = 0; i < a1.size(); i++) a1.set(i, 0);
act.begin();
a1.set(0, 1);
a1.set(1, 2);
assertTrue(a1.get(0).equals(1));
assertTrue(a1.get(1).equals(2));
act.abort();
assertTrue(a1.get(0).equals(0));
assertTrue(a1.get(1).equals(0));
}
use of com.arjuna.ats.arjuna.AtomicAction in project narayana by jbosstm.
the class Worker004 method run.
/**
* The main method of the class that will perform the work.
*/
public void run() {
expectedValue = new int[mNumberOfResources];
for (int j = 0; j < mNumberOfResources; j++) {
expectedValue[j] = 0;
}
try {
for (int j = 0; j < mNumberOfResources; j++) {
for (int i = 0; i < mMaxIteration; i++) {
// start transaction
AtomicAction a = new AtomicAction();
a.begin();
int incValue = mLockRecordList[j].increase();
if (i % 2 == 0) {
a.commit();
expectedValue[j] += incValue;
} else {
a.abort();
}
}
}
for (int j = 0; j < mNumberOfResources; j++) {
for (int i = 0; i < mMaxIteration; i++) {
AtomicAction b = new AtomicAction();
b.begin();
int incValue = mLockRecordList[j].increase();
if (i % 2 == 0) {
b.commit();
expectedValue[j] += incValue;
} else {
b.abort();
}
}
}
} catch (Exception e) {
mCorrect = false;
qautil.debug("exception in worker001: ", e);
}
}
Aggregations