use of com.arjuna.ats.txoj.Lock 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.txoj.Lock in project narayana by jbosstm.
the class OptimisticLockManager method loadState.
/*
* Lock and load the concurrency control state. First we grab the semaphore
* to ensure exclusive access and then we build the held lock list by
* retreiving the locks from the lock repository. If there is only one
* server we do not bother doing this since all the locks can stay in the
* server's memory. This is yet another consequence of not having
* multi-threaded servers. Does not require synchronized since it can only
* be called from other synchronized methods.
*/
protected final boolean loadState() {
if (txojLogger.logger.isTraceEnabled()) {
txojLogger.logger.trace("LockManager::loadState()");
}
if (super.objectModel == ObjectModel.SINGLE) {
stateLoaded = true;
return true;
} else {
InputObjectState S = null;
if ((systemKey == null) && !initialise()) {
return false;
/* init failed */
}
if ((mutex == null) || (!mutex.tryLock())) {
return false;
}
stateLoaded = false;
objectLocked = true;
try {
S = lockStore.read_state(get_uid(), type());
if (S != null) {
Uid u = null;
/*
* avoid system calls in Uid
* creation
*/
Lock current = null;
int count = 0;
try {
count = S.unpackInt();
boolean cleanLoad = true;
if (txojLogger.logger.isTraceEnabled()) {
txojLogger.logger.trace("LockManager::loadState() loading " + count + " lock(s)");
}
for (int i = 0; (i < count) && cleanLoad; i++) {
try {
u = UidHelper.unpackFrom(S);
current = new OptimisticLock(u);
if (current != null) {
if (current.restore_state(S, ObjectType.ANDPERSISTENT)) {
locksHeld.push(current);
} else {
current = null;
cleanLoad = false;
}
} else {
cleanLoad = false;
}
} catch (IOException e) {
cleanLoad = false;
}
}
if (cleanLoad)
stateLoaded = true;
else {
while ((current = locksHeld.pop()) != null) current = null;
}
} catch (IOException e) {
}
S = null;
} else
stateLoaded = true;
} catch (LockStoreException e) {
txojLogger.logger.warn(e);
}
}
if (!stateLoaded) {
if (// means object model != SINGLE
mutex != null) {
// and exit mutual exclusion
mutex.unlock();
}
objectLocked = false;
}
return stateLoaded;
}
use of com.arjuna.ats.txoj.Lock in project narayana by jbosstm.
the class BasicLockRecord2 method getValue.
public int getValue(int retry, int wait_time) {
int return_value = 0;
int locking_result = LockResult.REFUSED;
int locking_attempt_count = 0;
Lock lck = new Lock(LockMode.READ);
do {
locking_result = setlock(lck, retry, wait_time);
if (locking_result == LockResult.GRANTED) {
return_value = mValue;
} else {
locking_attempt_count++;
}
} while ((locking_result != LockResult.GRANTED) && (locking_attempt_count < mLimit));
if (locking_result != LockResult.GRANTED) {
qautil.qadebug("trying to get lock for " + mLimit + "th time");
}
return return_value;
}
use of com.arjuna.ats.txoj.Lock in project narayana by jbosstm.
the class TXBasicLockRecord2 method getValue.
public int getValue(int retry, int wait_time) {
int return_value = 0;
AtomicAction a = new AtomicAction();
a.begin();
try {
int locking_result = LockResult.REFUSED;
int locking_attempt_count = 0;
Lock lck = new Lock(LockMode.READ);
do {
locking_result = setlock(lck, retry, wait_time);
if (locking_result == LockResult.GRANTED) {
return_value = mValue;
} else {
locking_attempt_count++;
}
} while ((locking_result != LockResult.GRANTED) && (locking_attempt_count < mLimit));
if (locking_result != LockResult.GRANTED) {
qautil.qadebug("trying to get lock for " + mLimit + "th time");
}
a.commit();
} catch (Exception e) {
a.abort();
qautil.debug("exception in get method ", e);
}
return return_value;
}
use of com.arjuna.ats.txoj.Lock in project narayana by jbosstm.
the class AITCounterImpl04 method get.
public void get(IntHolder value) throws InvocationException {
try {
AtomicTransaction atomicTransaction = new AtomicTransaction();
try {
atomicTransaction.begin();
if (setlock(new Lock(LockMode.READ), super.waitTotalTimeout) == LockResult.GRANTED) {
value.value = _value;
atomicTransaction.commit(true);
} else {
atomicTransaction.rollback();
throw new InvocationException();
}
} catch (InvocationException invocationException) {
throw invocationException;
} catch (Exception exception) {
System.err.println("AITCounterImpl04.get: " + exception);
if (atomicTransaction.get_status() == Status.StatusActive) {
atomicTransaction.rollback();
}
throw new InvocationException();
}
} catch (InvocationException invocationException) {
throw invocationException;
} catch (Exception exception) {
System.err.println("AITCounterImpl04.get: " + exception);
throw new InvocationException();
}
}
Aggregations