public boolean tryBegin(String lockName, WaitInvocation timeout, int lockLevel) { logTryBegin0(lockName, lockLevel); if (isTransactionLoggingDisabled() || objectManager.isCreationInProgress()) { return true; } final TxnType txnType = getTxnTypeFromLockLevel(lockLevel); ClientTransaction currentTransaction = getTransactionOrNull(); if ((currentTransaction != null) && lockLevel == LockLevel.CONCURRENT) { // make formatter sane throw new AssertionError("Can't acquire concurrent locks in a nested lock context."); } final LockID lockID = lockManager.lockIDFor(lockName); boolean isLocked = lockManager.tryLock(lockID, timeout, lockLevel); if (!isLocked) { return isLocked; } pushTxContext(lockID, txnType); if (currentTransaction == null) { createTxAndInitContext(); } else { currentTransaction.setTransactionContext(this.peekContext()); } return isLocked; }
/** * In order to support ReentrantLock, the TransactionContext that is going to be removed when * doing a commit may not always be at the top of a stack because an reentrant lock could issue a * lock within a synchronized block (although it is highly not recommended). Therefore, when a * commit is issued, we need to search back the stack from the top of the stack to find the * appropriate TransactionContext to be removed. Most likely, the TransactionContext to be removed * will be on the top of the stack. Therefore, the performance should be make must difference. * Only in some weird situations where reentrantLock is mixed with synchronized block will the * TransactionContext to be removed be found otherwise. */ public void commit(String lockName) throws UnlockedSharedObjectException { logCommit0(); if (isTransactionLoggingDisabled() || objectManager.isCreationInProgress()) { return; } // ClientTransaction tx = popTransaction(); ClientTransaction tx = getTransaction(); LockID lockID = lockManager.lockIDFor(lockName); if (lockID == null || lockID.isNull()) { lockID = tx.getLockID(); } boolean hasCommitted = commit(lockID, tx, false); popTransaction(lockManager.lockIDFor(lockName)); if (peekContext() != null) { if (hasCommitted) { createTxAndInitContext(); } else { // If the current transaction has not committed, we will reuse the current transaction // so that the current changes will have a chance to commit at the next commit point. tx.setTransactionContext(peekContext()); setTransaction(tx); } } }
public void unlock(String lockName) { final LockID lockID = lockManager.lockIDFor(lockName); if (lockID != null) { lockManager.unlock(lockID); getThreadTransactionContext().removeLock(lockID); } }
public boolean isHeldByCurrentThread(String lockName, int lockLevel) { if (isTransactionLoggingDisabled()) { return true; } final LockID lockID = lockManager.lockIDFor(lockName); return lockManager.localHeldCount(lockID, lockLevel) > 0; }
public void notify(String lockName, boolean all, Object object) throws UnlockedSharedObjectException { final ClientTransaction currentTxn = getTransaction(); LockID lockID = lockManager.lockIDFor(lockName); if (lockID == null || lockID.isNull()) { lockID = currentTxn.getLockID(); } currentTxn.addNotify(lockManager.notify(lockID, all)); }
public void wait(String lockName, WaitInvocation call, Object object) throws UnlockedSharedObjectException, InterruptedException { final ClientTransaction topTxn = getTransaction(); LockID lockID = lockManager.lockIDFor(lockName); if (lockID == null || lockID.isNull()) { lockID = topTxn.getLockID(); } commit(lockID, topTxn, true); try { lockManager.wait(lockID, call, object, waitListener); } finally { createTxAndInitContext(); } }
public boolean begin(String lockName, int lockLevel) { logBegin0(lockName, lockLevel); if (isTransactionLoggingDisabled() || objectManager.isCreationInProgress()) { return false; } final TxnType txnType = getTxnTypeFromLockLevel(lockLevel); ClientTransaction currentTransaction = getTransactionOrNull(); final LockID lockID = lockManager.lockIDFor(lockName); pushTxContext(lockID, txnType); if (currentTransaction == null) { createTxAndInitContext(); } else { currentTransaction.setTransactionContext(this.peekContext()); } lockManager.lock(lockID, lockLevel); return true; }
private boolean commitInternal( LockID lockID, ClientTransaction currentTransaction, boolean isWaitContext) { Assert.assertNotNull("transaction", currentTransaction); try { disableTransactionLogging(); // If the current transactionContext is READ_ONLY, there is no need to commit. TransactionContext tc = peekContext(lockID); if (tc.getType().equals(TxnType.READ_ONLY)) { txMonitor.committedReadTransaction(); return false; } boolean hasPendingCreateObjects = objectManager.hasPendingCreateObjects(); if (hasPendingCreateObjects) { objectManager.addPendingCreateObjectsToTransaction(); } currentTransaction.setAlreadyCommitted(); if (currentTransaction.hasChangesOrNotifies() || hasPendingCreateObjects) { if (txMonitor.isEnabled()) { currentTransaction.updateMBean(txMonitor); } remoteTxManager.commit(currentTransaction); } return true; } finally { enableTransactionLogging(); // always try to unlock even if we are throwing an exception // if (!isWaitContext && !currentTransaction.isNull()) { // lockManager.unlock(currentTransaction.getLockID()); // } if (!isWaitContext && !currentTransaction.isNull()) { if (lockID != null && !lockID.isNull()) { lockManager.unlock(lockID); } else { throw new AssertionError("Trying to unlock with lockID = null!"); } } } }
public int localHeldCount(String lockName, int lockLevel) { final LockID lockID = lockManager.lockIDFor(lockName); return lockManager.localHeldCount(lockID, lockLevel); }
public int waitLength(String lockName) { final LockID lockID = lockManager.lockIDFor(lockName); return lockManager.waitLength(lockID); }
public void lock(String lockName, int lockLevel) { final LockID lockID = lockManager.lockIDFor(lockName); lockManager.lock(lockID, lockLevel); }
public boolean isLocked(String lockName, int lockLevel) { final LockID lockID = lockManager.lockIDFor(lockName); return lockManager.isLocked(lockID, lockLevel); }