@Override
  public List<HiveLock> lock(List<HiveLockObj> lockObjects,
      boolean keepAlive, DriverState driverState) throws LockException
  {
    // Sort the objects first. You are guaranteed that if a partition is being locked,
    // the table has already been locked

    Collections.sort(lockObjects, new Comparator<HiveLockObj>() {

    @Override
    public int compare(HiveLockObj o1, HiveLockObj o2) {
      int cmp = o1.getName().compareTo(o2.getName());
      if (cmp == 0) {
        if (o1.getMode() == o2.getMode()) {
          return cmp;
        }
        // EXCLUSIVE locks occur before SHARED locks
        if (o1.getMode() == HiveLockMode.EXCLUSIVE) {
          return -1;
        }
        return +1;
      }
      return cmp;
    }
    });

    // walk the list and acquire the locks - if any lock cant be acquired, release all locks, sleep
    // and retry
    HiveLockObj prevLockObj = null;
    List<HiveLock> hiveLocks = new ArrayList<HiveLock>();

    for (HiveLockObj lockObject : lockObjects) {
      // No need to acquire a lock twice on the same object
      // It is ensured that EXCLUSIVE locks occur before SHARED locks on the same object
      if ((prevLockObj != null) && (prevLockObj.getName().equals(lockObject.getName()))) {
        prevLockObj = lockObject;
        continue;
      }

      HiveLock lock = null;
      boolean isInterrupted = false;
      if (driverState != null) {
        driverState.lock();
        if (driverState.isAborted()) {
          isInterrupted = true;
        }
        driverState.unlock();
      }
      if (!isInterrupted) {
        try {
          lock = lock(lockObject.getObj(), lockObject.getMode(), keepAlive, true);
        } catch (LockException e) {
          console.printError("Error in acquireLocks..." );
          LOG.error("Error in acquireLocks...", e);
          lock = null;
        }
      }

      if (lock == null) {
        releaseLocks(hiveLocks);
        if (isInterrupted) {
          throw new LockException(ErrorMsg.LOCK_ACQUIRE_CANCELLED.getMsg());
        }
        return null;
      }

      hiveLocks.add(lock);
      prevLockObj = lockObject;
    }

    return hiveLocks;

  }
@param  lockObjects  List of objects and the modes of the locks requested
@param  keepAlive    Whether the lock is to be persisted after the statement

Acquire all the locks. Release all the locks and return null if any lock
could not be acquired.
ql/src/java/org/apache/hadoop/hive/ql/lockmgr/zookeeper/ZooKeeperHiveLockManager.java