private void runDatasetLockTask() {
    try {
      produceDatasetLockRequest();
      if (isDone) {
        return;
      }
    } catch (ACIDException e) {
      e.printStackTrace();
      return;
    }

    try {
      Thread.sleep(DATASET_LOCK_THREAD_SLEEP_TIME);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }

    try {
      produceDatasetUnlockRequest();
      if (isDone) {
        return;
      }
    } catch (ACIDException e) {
      e.printStackTrace();
      return;
    }
  }
 private static TransactionContext generateTxnContext(TransactionSubsystem txnProvider) {
   try {
     return new TransactionContext(new JobId(jobId++), txnProvider);
   } catch (ACIDException e) {
     e.printStackTrace();
     return null;
   }
 }
  private void sendRequest(LockRequest request) throws ACIDException {

    switch (request.requestType) {
      case RequestType.LOCK:
        try {
          lockMgr.lock(
              request.datasetIdObj, request.entityHashValue, request.lockMode, request.txnContext);
        } catch (ACIDException e) {
          if (request.txnContext.isTimeout()) {
            if (request.txnContext.getTxnState() != ITransactionManager.ABORTED) {
              request.txnContext.setTxnState(ITransactionManager.ABORTED);
              log("*** " + request.txnContext.getJobId() + " lock request causing deadlock ***");
              log("Abort --> Releasing all locks acquired by " + request.txnContext.getJobId());
              try {
                lockMgr.releaseLocks(request.txnContext);
              } catch (ACIDException e1) {
                e1.printStackTrace();
              }
              log("Abort --> Released all locks acquired by " + request.txnContext.getJobId());
            }
            isDone = true;
          } else {
            throw e;
          }
        }
        break;
      case RequestType.INSTANT_LOCK:
        try {
          lockMgr.instantLock(
              request.datasetIdObj, request.entityHashValue, request.lockMode, request.txnContext);
        } catch (ACIDException e) {
          if (request.txnContext.isTimeout()) {
            if (request.txnContext.getTxnState() != ITransactionManager.ABORTED) {
              request.txnContext.setTxnState(ITransactionManager.ABORTED);
              log("*** " + request.txnContext.getJobId() + " lock request causing deadlock ***");
              log("Abort --> Releasing all locks acquired by " + request.txnContext.getJobId());
              try {
                lockMgr.releaseLocks(request.txnContext);
              } catch (ACIDException e1) {
                e1.printStackTrace();
              }
              log("Abort --> Released all locks acquired by " + request.txnContext.getJobId());
            }
            isDone = true;
          } else {
            throw e;
          }
        }
        break;
      case RequestType.TRY_LOCK:
        request.isTryLockFailed =
            !lockMgr.tryLock(
                request.datasetIdObj,
                request.entityHashValue,
                request.lockMode,
                request.txnContext);
        break;
      case RequestType.INSTANT_TRY_LOCK:
        lockMgr.instantTryLock(
            request.datasetIdObj, request.entityHashValue, request.lockMode, request.txnContext);
        break;
      case RequestType.UNLOCK:
        lockMgr.unlock(
            request.datasetIdObj, request.entityHashValue, request.lockMode, request.txnContext);
        break;
      case RequestType.RELEASE_LOCKS:
        lockMgr.releaseLocks(request.txnContext);
        break;
      default:
        throw new UnsupportedOperationException("Unsupported lock method");
    }
    try {
      Thread.sleep((long) 0);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  private void runEntityLockTask() {
    int i;
    byte lockMode;
    int lockCount;
    int upgradeCount;
    int releaseCount;
    boolean mayRelease = false;

    lockCount = 1 + rand.nextInt(20);
    if (isUpgradeThreadJob) {
      if (isUpgradeThread) {
        upgradeCount = 1; // rand.nextInt(4) + 1;
        if (upgradeCount > lockCount) {
          upgradeCount = lockCount;
        }
      } else {
        upgradeCount = 0;
      }
      lockMode = LockMode.S;
    } else {
      upgradeCount = 0;
      lockMode = (byte) (this.txnContext.getJobId().getId() % 2);
    }
    releaseCount = rand.nextInt(5) % 3 == 0 ? 1 : 0;

    // lock
    for (i = 0; i < lockCount; i++) {
      try {
        produceEntityLockRequest(lockMode);
        if (isDone) {
          return;
        }
      } catch (ACIDException e) {
        e.printStackTrace();
        return;
      }
    }

    // upgrade
    for (i = 0; i < upgradeCount; i++) {
      try {
        produceEntityLockUpgradeRequest();
        if (isDone) {
          return;
        }
      } catch (ACIDException e) {
        e.printStackTrace();
        return;
      }
    }

    // unlock or releaseLocks
    if (releaseCount == 0) {
      // unlock
      for (i = 0; i < lockCount; i++) {
        try {
          produceEntityUnlockRequest();
          if (isDone) {
            return;
          }
        } catch (ACIDException e) {
          e.printStackTrace();
          return;
        }
      }
    } else {
      try {
        synchronized (txnContext) {
          if (txnContext.getTxnState() != ITransactionManager.ABORTED) {
            txnContext.setTxnState(ITransactionManager.ABORTED);
            mayRelease = true;
          }
        }
        if (mayRelease) {
          produceEntityReleaseLocksRequest();
        }
      } catch (ACIDException e) {
        e.printStackTrace();
        return;
      }
    }
  }