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; @@ -110,14 +111,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; } @@ -237,7 +237,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 +309,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,6 +18,7 @@ package org.apache.hadoop.hive.ql.exec; +import java.io.IOException; import java.io.Serializable; import org.apache.hadoop.hive.ql.session.SessionState; @@ -43,8 +44,16 @@ @Override public void run() { - SessionState.start(ss); - runSequential(); + try { + 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(); } }