示例#1
0
  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;
  }
示例#2
0
  /**
   * 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);
      }
    }
  }
示例#3
0
 public void unlock(String lockName) {
   final LockID lockID = lockManager.lockIDFor(lockName);
   if (lockID != null) {
     lockManager.unlock(lockID);
     getThreadTransactionContext().removeLock(lockID);
   }
 }
示例#4
0
 public boolean isHeldByCurrentThread(String lockName, int lockLevel) {
   if (isTransactionLoggingDisabled()) {
     return true;
   }
   final LockID lockID = lockManager.lockIDFor(lockName);
   return lockManager.localHeldCount(lockID, lockLevel) > 0;
 }
示例#5
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));
  }
示例#6
0
  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();
    }
  }
示例#7
0
  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;
  }
示例#8
0
  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!");
        }
      }
    }
  }
示例#9
0
 public int localHeldCount(String lockName, int lockLevel) {
   final LockID lockID = lockManager.lockIDFor(lockName);
   return lockManager.localHeldCount(lockID, lockLevel);
 }
示例#10
0
 public int waitLength(String lockName) {
   final LockID lockID = lockManager.lockIDFor(lockName);
   return lockManager.waitLength(lockID);
 }
示例#11
0
 public void lock(String lockName, int lockLevel) {
   final LockID lockID = lockManager.lockIDFor(lockName);
   lockManager.lock(lockID, lockLevel);
 }
示例#12
0
 public boolean isLocked(String lockName, int lockLevel) {
   final LockID lockID = lockManager.lockIDFor(lockName);
   return lockManager.isLocked(lockID, lockLevel);
 }