Index: ql/src/java/org/apache/hadoop/hive/ql/Driver.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/Driver.java (revision 1101116) +++ ql/src/java/org/apache/hadoop/hive/ql/Driver.java (working copy) @@ -145,7 +145,11 @@ return false; } } - return true; + // the reason that we set the lock manager for the cxt here is because each + // query has its own ctx object. The hiveLockMgr is shared accross the + // same instance of Driver, which can run multiple queries. + ctx.setHiveLockMgr(hiveLockMgr); + return hiveLockMgr != null; } private void setLockManager() throws SemanticException { @@ -161,11 +165,21 @@ conf); hiveLockMgr.setContext(new HiveLockManagerCtx(conf)); } catch (Exception e) { + // set hiveLockMgr to null just in case this invalid manager got set to + // next query's ctx. + if (hiveLockMgr != null) { + try { + hiveLockMgr.close(); + } catch (LockException e1) { + //nothing can do here + } + hiveLockMgr = null; + } throw new SemanticException(ErrorMsg.LOCKMGR_NOT_INITIALIZED.getMsg() + e.getMessage()); } } } - + public void init() { Operator.resetId(); } @@ -827,17 +841,15 @@ SQLState = null; int ret = compile(command); - - boolean requireLock = false; - boolean ckLock = checkLockManager(); - if (ret != 0) { releaseLocks(ctx.getHiveLocks()); return new CommandProcessorResponse(ret, errorMessage, SQLState); } - + + boolean requireLock = false; + boolean ckLock = checkLockManager(); + if (ckLock) { - ctx.setHiveLockMgr(hiveLockMgr); boolean lockOnlyMapred = HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_LOCK_MAPRED_ONLY); if(lockOnlyMapred) { Queue> taskQueue = new LinkedList>(); Index: ql/src/java/org/apache/hadoop/hive/ql/lockmgr/zookeeper/ZooKeeperHiveLockManager.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/lockmgr/zookeeper/ZooKeeperHiveLockManager.java (revision 1101116) +++ ql/src/java/org/apache/hadoop/hive/ql/lockmgr/zookeeper/ZooKeeperHiveLockManager.java (working copy) @@ -370,10 +370,11 @@ /* Release all locks - including PERSISTENT locks */ public static void releaseAllLocks(HiveConf conf) throws Exception { + ZooKeeper zkpClient = null; try { int sessionTimeout = conf.getIntVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_SESSION_TIMEOUT); String quorumServers = getQuorumServers(conf); - ZooKeeper zkpClient = new ZooKeeper(quorumServers, sessionTimeout, new DummyWatcher()); + zkpClient = new ZooKeeper(quorumServers, sessionTimeout, new DummyWatcher()); String parent = conf.getVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_NAMESPACE); List locks = getLocks(conf, zkpClient, null, parent, false, false); @@ -382,12 +383,14 @@ unlock(conf, zkpClient, lock, parent); } } - - zkpClient.close(); - zkpClient = null; } catch (Exception e) { LOG.error("Failed to release all locks: " + e.getMessage()); throw new Exception(ErrorMsg.ZOOKEEPER_CLIENT_COULD_NOT_BE_INITIALIZED.getMsg()); + } finally { + if (zkpClient != null) { + zkpClient.close(); + zkpClient = null; + } } } @@ -490,6 +493,10 @@ try { renewZookeeperInstance(sessionTimeout, quorumServers); checkRedundantNode("/" + parent); + if (zooKeeper != null) { + zooKeeper.close(); + zooKeeper = null; + } } catch (Exception e) { // ignore all errors } @@ -519,14 +526,16 @@ /* Release all transient locks, by simply closing the client */ public void close() throws LockException { try { + if (zooKeeper != null) { zooKeeper.close(); zooKeeper = null; } - + if (HiveConf.getBoolVar(ctx.getConf(), HiveConf.ConfVars.HIVE_ZOOKEEPER_CLEAN_EXTRA_NODES)) { removeAllRedundantNodes(); } + } catch (Exception e) { LOG.error("Failed to close zooKeeper client: " + e); throw new LockException(e);