use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.
the class OptimisticLockManager method setlock.
/*
* Some of these methods are copied directly from LockManager and do 99% the same thing.
* The only way they differ is with the types of Locks or LockRecords that they create.
* Probably should refactor later to reduce the amount of copied code.
*/
/*
* All of this is here to prevent us grabbing a copy of the state of the object when we lock it.
* Optimistic and pessimistic locks cannot be used in the same transaction on the same object.
*
* WARNING HERE BE DRAGONS.
*
* (non-Javadoc)
* @see com.arjuna.ats.txoj.LockManager#setlock(com.arjuna.ats.txoj.Lock, int, int)
*/
public int setlock(Lock toSet, int retry, int sleepTime) {
if (txojLogger.logger.isTraceEnabled()) {
txojLogger.logger.trace("OptimisticLockManager::setlock(" + toSet + ", " + retry + ", " + sleepTime + ")");
}
int conflict = ConflictType.CONFLICT;
int returnStatus = LockResult.REFUSED;
LockRecord newLockR = null;
boolean modifyRequired = false;
BasicAction currAct = null;
if (toSet == null) {
txojLogger.i18NLogger.warn_LockManager_2();
return LockResult.REFUSED;
}
if (!(toSet instanceof OptimisticLock))
return LockResult.REFUSED;
initialise();
currAct = BasicAction.Current();
if (currAct != null) {
ActionHierarchy ah = currAct.getHierarchy();
if (ah != null)
toSet.changeHierarchy(ah);
else {
txojLogger.i18NLogger.warn_LockManager_3();
toSet = null;
return LockResult.REFUSED;
}
}
if (super.loadObjectState())
super.setupStore();
while ((conflict == ConflictType.CONFLICT) && ((retry >= 0) || ((retry == LockManager.waitTotalTimeout) && (sleepTime > 0)))) {
Object syncObject = ((currAct == null) ? getMutex() : currAct);
synchronized (super.lockStore.getClass()) {
synchronized (syncObject) {
synchronized (locksHeldLockObject) {
conflict = ConflictType.CONFLICT;
if (loadState()) {
conflict = lockConflict(toSet);
} else {
txojLogger.i18NLogger.warn_LockManager_4();
}
if (conflict != ConflictType.CONFLICT) {
/*
* When here the conflict was resolved or the retry limit
* expired.
*/
/* no conflict so set lock */
modifyRequired = toSet.modifiesObject();
if (super.activate()) {
returnStatus = LockResult.GRANTED;
if (conflict == ConflictType.COMPATIBLE) {
int lrStatus = AddOutcome.AR_ADDED;
if (currAct != null) {
/* add new lock record to action list */
newLockR = new OptimisticLockRecord(this, (modifyRequired ? false : true), currAct, true);
if ((lrStatus = currAct.add(newLockR)) != AddOutcome.AR_ADDED) {
newLockR = null;
if (lrStatus == AddOutcome.AR_REJECTED) {
returnStatus = LockResult.REFUSED;
}
}
}
if (returnStatus == LockResult.GRANTED) {
locksHeld.insert(toSet);
/*
* add to local lock
* list
*/
}
} else {
if (modifyRequired)
returnStatus = LockResult.GRANTED;
}
} else {
/* activate failed - refuse request */
txojLogger.i18NLogger.warn_LockManager_5();
returnStatus = LockResult.REFUSED;
}
}
if ((returnStatus == LockResult.GRANTED) && (conflict == ConflictType.COMPATIBLE)) {
if (!unloadState()) {
txojLogger.i18NLogger.warn_LockManager_6();
returnStatus = LockResult.REFUSED;
}
} else
freeState();
if (returnStatus == LockResult.GRANTED) {
if (modifyRequired) {
if (super.modified()) {
hasBeenLocked = true;
} else {
conflict = ConflictType.CONFLICT;
returnStatus = LockResult.REFUSED;
}
}
}
if (conflict == ConflictType.CONFLICT)
freeState();
}
}
}
if (conflict == ConflictType.CONFLICT) {
if (retry != 0) {
if (sleepTime > 0) {
sleepTime -= conflictManager.wait(retry, sleepTime);
} else
retry = 0;
}
if (retry != LockManager.waitTotalTimeout)
retry--;
}
}
return returnStatus;
}
use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.
the class LockManager method setlock.
/*
* This is the main user visible operation. Attempts to set the given lock
* on the current object. If lock cannot be set, then the lock attempt is
* retried retry times before giving up and returning an error. This gives a
* simple handle on deadlock.
*
* sleepTime is milliseconds.
*
* @return <code>LockResult</code> indicating outcome.
*/
public int setlock(Lock toSet, int retry, int sleepTime) {
if (txojLogger.logger.isTraceEnabled()) {
txojLogger.logger.trace("LockManager::setlock(" + toSet + ", " + retry + ", " + sleepTime + ")");
}
int returnStatus = LockResult.REFUSED;
// JBTM-2098, we need to have the action locked in case a simultaneous abort
// is issued which would try to lock the mutex before we can call modified
Object toLock = BasicAction.Current();
if (toLock == null) {
toLock = new Object();
}
synchronized (toLock) {
if (!lockMutex())
return LockResult.REFUSED;
try {
int conflict = ConflictType.CONFLICT;
LockRecord newLockR = null;
boolean modifyRequired = false;
BasicAction currAct = null;
if (toSet == null) {
txojLogger.i18NLogger.warn_LockManager_2();
return LockResult.REFUSED;
}
currAct = BasicAction.Current();
if (currAct != null) {
ActionHierarchy ah = currAct.getHierarchy();
if (ah != null)
toSet.changeHierarchy(ah);
else {
txojLogger.i18NLogger.warn_LockManager_3();
toSet = null;
return LockResult.REFUSED;
}
}
if (super.loadObjectState())
super.setupStore();
while ((conflict == ConflictType.CONFLICT) && ((retry >= 0) || ((retry == LockManager.waitTotalTimeout) && (sleepTime > 0)))) {
synchronized (locksHeldLockObject) {
conflict = ConflictType.CONFLICT;
if (loadState()) {
conflict = lockConflict(toSet);
} else {
txojLogger.i18NLogger.warn_LockManager_4();
}
if (conflict != ConflictType.CONFLICT) {
/*
* When here the conflict was resolved or the retry limit
* expired.
*/
/* no conflict so set lock */
modifyRequired = toSet.modifiesObject();
if (super.activate()) {
returnStatus = LockResult.GRANTED;
if (conflict == ConflictType.COMPATIBLE) {
int lrStatus = AddOutcome.AR_ADDED;
if (currAct != null) {
/* add new lock record to action list */
newLockR = new LockRecord(this, (modifyRequired ? false : true), currAct);
if ((lrStatus = currAct.add(newLockR)) != AddOutcome.AR_ADDED) {
newLockR = null;
if (lrStatus == AddOutcome.AR_REJECTED)
returnStatus = LockResult.REFUSED;
}
}
if (returnStatus == LockResult.GRANTED) {
locksHeld.insert(toSet);
/*
* add to local lock
* list
*/
}
}
} else {
/* activate failed - refuse request */
txojLogger.i18NLogger.warn_LockManager_5();
returnStatus = LockResult.REFUSED;
}
}
if ((returnStatus == LockResult.GRANTED) && (conflict == ConflictType.COMPATIBLE)) {
if (!unloadState()) {
txojLogger.i18NLogger.warn_LockManager_6();
returnStatus = LockResult.REFUSED;
}
} else
freeState();
if (returnStatus == LockResult.GRANTED) {
if (modifyRequired) {
if (super.modified())
hasBeenLocked = true;
else {
conflict = ConflictType.CONFLICT;
returnStatus = LockResult.REFUSED;
}
}
}
if (conflict == ConflictType.CONFLICT)
freeState();
}
if (conflict == ConflictType.CONFLICT) {
if (retry != 0) {
if (sleepTime > 0) {
sleepTime -= conflictManager.wait(retry, sleepTime);
} else
retry = 0;
}
if (retry != LockManager.waitTotalTimeout)
retry--;
}
}
} finally {
unlockMutex();
}
}
return returnStatus;
}
use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.
the class LockRecordUnitTest method test.
@Test
public void test() {
LockRecord lr = new LockRecord();
assertEquals(lr.lockType(), null);
assertTrue(lr.save_state(new OutputObjectState(), ObjectType.ANDPERSISTENT));
assertFalse(lr.restore_state(new InputObjectState(), ObjectType.ANDPERSISTENT));
assertEquals(lr.value(), null);
lr.setValue(null);
assertEquals(lr.nestedAbort(), TwoPhaseOutcome.FINISH_ERROR);
assertEquals(lr.nestedCommit(), TwoPhaseOutcome.FINISH_ERROR);
assertEquals(lr.topLevelAbort(), TwoPhaseOutcome.FINISH_ERROR);
assertEquals(lr.topLevelCommit(), TwoPhaseOutcome.FINISH_ERROR);
lr = new LockRecord(new AtomicObject(), new AtomicAction());
assertTrue(lr.toString() != null);
lr.print(new PrintWriter(new ByteArrayOutputStream()));
assertTrue(lr.type() != null);
lr.merge(null);
lr.alter(null);
}
use of com.arjuna.ats.internal.txoj.abstractrecords.LockRecord in project narayana by jbosstm.
the class CadaverUnitTest method testBasic.
@Test
public void testBasic() throws Exception {
AtomicAction A = new AtomicAction();
AtomicObject B = new AtomicObject();
A.begin();
CadaverLockRecord clr = new CadaverLockRecord(null, B, A);
LockRecord lr = new LockRecord(B, A);
assertTrue(clr.type() != null);
clr.print(new PrintWriter(new ByteArrayOutputStream()));
clr.replace(lr);
A.abort();
}
Aggregations