public void closeSession(SessionHandle sessionHandle) throws HiveSQLException {
   HiveSession session = handleToSession.remove(sessionHandle);
   if (session == null) {
     throw new HiveSQLException("Session does not exist!");
   }
   session.close();
   // Shutdown HiveServer2 if it has been deregistered from ZooKeeper and has no active sessions
   if (!(hiveServer2 == null)
       && (hiveConf.getBoolVar(ConfVars.HIVE_SERVER2_SUPPORT_DYNAMIC_SERVICE_DISCOVERY))
       && (!hiveServer2.isRegisteredWithZooKeeper())) {
     // Asynchronously shutdown this instance of HiveServer2,
     // if there are no active client sessions
     if (getOpenSessionCount() == 0) {
       LOG.info(
           "This instance of HiveServer2 has been removed from the list of server "
               + "instances available for dynamic service discovery. "
               + "The last client session has ended - will shutdown now.");
       Thread shutdownThread =
           new Thread() {
             @Override
             public void run() {
               hiveServer2.stop();
             }
           };
       shutdownThread.start();
     }
   }
 }
  /**
   * Opens a new session and creates a session handle. The username passed to this method is the
   * effective username. If withImpersonation is true (==doAs true) we wrap all the calls in
   * HiveSession within a UGI.doAs, where UGI corresponds to the effective user.
   *
   * @see org.apache.hive.service.cli.thrift.ThriftCLIService#getUserName()
   * @param protocol
   * @param username
   * @param password
   * @param ipAddress
   * @param sessionConf
   * @param withImpersonation
   * @param delegationToken
   * @return
   * @throws HiveSQLException
   */
  public SessionHandle openSession(
      TProtocolVersion protocol,
      String username,
      String password,
      String ipAddress,
      Map<String, String> sessionConf,
      boolean withImpersonation,
      String delegationToken)
      throws HiveSQLException {
    HiveSession session;
    // If doAs is set to true for HiveServer2, we will create a proxy object for the session impl.
    // Within the proxy object, we wrap the method call in a UserGroupInformation#doAs
    if (withImpersonation) {
      HiveSessionImplwithUGI sessionWithUGI =
          new HiveSessionImplwithUGI(
              protocol, username, password, hiveConf, ipAddress, delegationToken);
      session = HiveSessionProxy.getProxy(sessionWithUGI, sessionWithUGI.getSessionUgi());
      sessionWithUGI.setProxySession(session);
    } else {
      session = new HiveSessionImpl(protocol, username, password, hiveConf, ipAddress);
    }
    session.setSessionManager(this);
    session.setOperationManager(operationManager);
    try {
      session.initialize(sessionConf);
      if (isOperationLogEnabled) {
        session.setOperationLogSessionDir(operationLogRootDir);
      }
      session.open();
    } catch (Exception e) {
      throw new HiveSQLException("Failed to open new session", e);
    }
    try {
      executeSessionHooks(session);
    } catch (Exception e) {
      throw new HiveSQLException("Failed to execute session hooks", e);
    }

    handleToSession.put(session.getSessionHandle(), session);

    return session.getSessionHandle();
  }