Index: service/src/java/org/apache/hadoop/hive/service/HiveServer.java =================================================================== --- service/src/java/org/apache/hadoop/hive/service/HiveServer.java (revision 1411423) +++ service/src/java/org/apache/hadoop/hive/service/HiveServer.java (working copy) @@ -231,8 +231,10 @@ } SessionState session = SessionState.get(); - if (session.getTmpOutputFile() != null) { - session.getTmpOutputFile().delete(); + try { + session.close(); + } catch (IOException e) { + LOG.warn("Failed to close session. " + e.toString()); } pipeIn = null; } Index: cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java =================================================================== --- cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java (revision 1411423) +++ cli/src/java/org/apache/hadoop/hive/cli/CliSessionState.java (working copy) @@ -18,6 +18,7 @@ package org.apache.hadoop.hive.cli; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -77,11 +78,6 @@ private Hive hive; // currently only used (and init'ed) in getCurrentDbName - public CliSessionState() { - super(); - remoteMode = false; - } - public CliSessionState(HiveConf conf) { super(conf); remoteMode = false; @@ -110,14 +106,18 @@ return port; } + @Override public void close() { try { if (remoteMode) { client.clean(); transport.close(); } + super.close(); } catch (TException e) { e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } } Index: ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java =================================================================== --- ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java (revision 1411423) +++ ql/src/test/org/apache/hadoop/hive/ql/QTestUtil.java (working copy) @@ -30,6 +30,7 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; +import java.io.IOException; import java.io.PrintStream; import java.io.Serializable; import java.io.UnsupportedEncodingException; @@ -495,6 +496,7 @@ // modify conf by using 'set' commands conf = new HiveConf (Driver.class); initConf(); + Hive.closeCurrent(); db = Hive.get(conf); // propagate new conf to meta store setup.preTest(conf); } @@ -720,8 +722,11 @@ ss.err = new CachingPrintStream(fo, true, "UTF-8"); ss.setIsSilent(true); SessionState oldSs = SessionState.get(); - if (oldSs != null && oldSs.out != null && oldSs.out != System.out) { - oldSs.out.close(); + if (oldSs != null) { + oldSs.close(); + if (oldSs.out != null && oldSs.out != System.out) { + oldSs.out.close(); + } } SessionState.start(ss); @@ -1183,6 +1188,17 @@ + e.getMessage()); e.printStackTrace(); outputTestFailureHelpMessage(); + } finally { + // last chance to remove the thread local storage + SessionState state = SessionState.get(); + if(state != null) { + try { + state.close(); + } catch(IOException e) { + System.err.println("Warning - could not close session."); + } + } + Hive.closeCurrent(); } } } Index: ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java (revision 1411423) +++ ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java (working copy) @@ -154,7 +154,7 @@ return tmpOutputFile; } - public void setTmpOutputFile(File f) { + private void setTmpOutputFile(File f) { tmpOutputFile = f; } @@ -181,10 +181,6 @@ this.isVerbose = isVerbose; } - public SessionState() { - this(null); - } - public SessionState(HiveConf conf) { this.conf = conf; isSilent = conf.getBoolVar(HiveConf.ConfVars.HIVESESSIONSILENT); @@ -237,7 +233,20 @@ * Singleton Session object per thread. * **/ - private static ThreadLocal tss = new ThreadLocal(); + private static ThreadLocal tss = new ThreadLocal() { + @Override + public synchronized void remove() { + SessionState ss = this.get(); + if (ss != null) { + try { + ss.close(); + } catch(IOException e) { + // ignore + } + } + super.remove(); + } + }; /** * start a new session and set it to current session. @@ -296,6 +305,21 @@ } /** + * release the session's resources + */ + public void close() throws IOException { + if (hiveHist != null) { + hiveHist.close(); + hiveHist = null; + } + + if (tmpOutputFile != null) { + tmpOutputFile.delete(); + tmpOutputFile = null; + } + } + + /** * get the current session. */ public static SessionState get() { Index: ql/src/java/org/apache/hadoop/hive/ql/exec/TaskRunner.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/exec/TaskRunner.java (revision 1411423) +++ ql/src/java/org/apache/hadoop/hive/ql/exec/TaskRunner.java (working copy) @@ -18,8 +18,10 @@ package org.apache.hadoop.hive.ql.exec; +import java.io.IOException; import java.io.Serializable; +import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.ql.session.SessionState; /** @@ -30,11 +32,12 @@ protected Task tsk; protected TaskResult result; protected SessionState ss; + protected HiveConf conf; - public TaskRunner(Task tsk, TaskResult result) { + public TaskRunner(Task tsk, TaskResult result, HiveConf conf) { this.tsk = tsk; this.result = result; - ss = SessionState.get(); + this.conf = conf; } public Task getTask() { @@ -43,8 +46,21 @@ @Override public void run() { - SessionState.start(ss); - runSequential(); + SessionState ss = null; + try { + ss = SessionState.get(); + if (ss == null) { + ss = new SessionState(conf); + } + SessionState.start(ss); + runSequential(); + } finally { + try { + ss.close(); + } catch(IOException e) { + //ignore + } + } } /** Index: ql/src/java/org/apache/hadoop/hive/ql/history/HiveHistory.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/history/HiveHistory.java (revision 1411423) +++ ql/src/java/org/apache/hadoop/hive/ql/history/HiveHistory.java (working copy) @@ -528,11 +528,19 @@ return null; } + + /** + * Releases the resources used by the history + */ + public void close() throws IOException { + if (histStream != null) { + histStream.close(); + } + } + @Override public void finalize() throws Throwable { - if (histStream !=null){ - histStream.close(); - } + close(); super.finalize(); } } Index: ql/src/java/org/apache/hadoop/hive/ql/Driver.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/Driver.java (revision 1411423) +++ ql/src/java/org/apache/hadoop/hive/ql/Driver.java (working copy) @@ -1340,7 +1340,7 @@ } tsk.initialize(conf, plan, cxt); TaskResult tskRes = new TaskResult(); - TaskRunner tskRun = new TaskRunner(tsk, tskRes); + TaskRunner tskRun = new TaskRunner(tsk, tskRes, conf); // Launch Task if (HiveConf.getBoolVar(conf, HiveConf.ConfVars.EXECPARALLEL) && tsk.isMapRedTask()) {