private void testKillQueryInternal(String user, String killUser, boolean useTag, final ExceptionHolder stmtHolder,
      final ExceptionHolder tKillHolder) throws Exception {
    Connection con1 = BaseJdbcWithMiniLlap.getConnection(miniHS2.getJdbcURL(testDbName), user, "bar");
    Connection con2 = BaseJdbcWithMiniLlap.getConnection(miniHS2.getJdbcURL(testDbName), killUser, "bar");

    final Statement stmt2 = con2.createStatement();
    final HiveStatement stmt = (HiveStatement) con1.createStatement();
    final StringBuffer stmtQueryId = new StringBuffer();

    // Thread executing the query
    Thread tExecute = new Thread(() -> {
      try {
        String query =
            "select sleepMsUDF(t1.int_col, 10), t1.int_col, t2.int_col " + "from " + tableName + " t1 join " + tableName
                + " t2 on t1.int_col = t2.int_col";
        LOG.info("Executing query: " + query);
        stmt.execute("set hive.llap.execution.mode = none");
        stmt.execute("use " + testDbName);

        if (useTag) {
          stmt.execute("set hive.query.tag = " + tag);
        }
        // The test table has 500 rows, so total query time should be ~ 500*100ms = 50 seconds
        stmt.executeAsync(query);
        Thread.sleep(1000);
        stmtQueryId.append(stmt.getQueryId());
        stmt.getUpdateCount();
      } catch (SQLException | InterruptedException e) {
        stmtHolder.throwable = e;
      }
    });

    tExecute.start();

    // wait for other thread to create the stmt handle
    int count = 0;
    while (++count <= 10) {
      try {
        tKillHolder.throwable = null;
        Thread.sleep(2000);
        String queryId;
        if (useTag) {
          queryId = tag;
        } else {
          if (stmtQueryId.length() != 0) {
            queryId = stmtQueryId.toString();
          } else {
            continue;
          }
        }
        System.out.println("Killing query: " + queryId);
        stmt2.execute("kill query '" + queryId + "'");
        stmt2.close();
        break;
      } catch (SQLException e) {
        LOG.warn("Exception when kill query", e);
        tKillHolder.throwable = e;
      }
    }

    tExecute.join();
    try {
      stmt.close();
      con1.close();
      con2.close();
    } catch (Exception e) {
      // ignore error
      LOG.warn("Exception when close stmt and con", e);
    }
  }
Test CLI kill command of a query that is running.
We spawn 2 threads - one running the query and
the other attempting to cancel.
We're using a dummy udf to simulate a query,
that runs for a sufficiently long time.
@throws Exception
itests/hive-unit/src/test/java/org/apache/hive/jdbc/TestKillQueryWithAuthorizationDisabled.java