void replaceSession(SessionType oldSession) throws Exception {
    // Re-setting the queue config is an old hack that we may remove in future.
    SessionType newSession = sessionObjFactory.create(oldSession);
    String queueName = oldSession.getQueueName();
    try {
      oldSession.close(false);
    } finally {
      poolLock.lock();
      try {
        // The expiring session may or may not be in the pool.
        pool.remove(oldSession);
      } finally {
        poolLock.unlock();
      }

      notifyClosed(oldSession);
      // There's some bogus code that can modify the queue name. Force-set it for pool sessions.
      // TODO: this might only be applicable to TezSessionPoolManager; try moving it there?
      newSession.getConf().set(TezConfiguration.TEZ_QUEUE_NAME, queueName);
      // The caller probably created the new session with the old config, but update the
      // registry again just in case. TODO: maybe we should enforce that.
      configureAmRegistry(newSession);
      if (SessionState.get() == null && parentSessionState != null) {
        // Tez session relies on a threadlocal for open... If we are on some non-session thread,
        // just use the same SessionState we used for the initial sessions.
        // Technically, given that all pool sessions are initially based on this state, shoudln't
        // we also set this at all times and not rely on an external session stuff? We should
        // probably just get rid of the thread local usage in TezSessionState.
        SessionState.setCurrentSessionState(parentSessionState);
      }
      newSession.open();
      if (!putSessionBack(newSession, false)) {
        LOG.debug("Closing an unneeded session {}; trying to replace {}", newSession, oldSession);
        try {
          newSession.close(false);
        } catch (Exception ex) {
          LOG.error("Failed to close an unneeded session", ex);
        }
      }
    }
  }
Puts session back into the pool.
@return true if the session has been put back; false if it's not needed and should be killed.
ql/src/java/org/apache/hadoop/hive/ql/exec/tez/TezSessionPool.java