@Test
  public void testQueryCancel() throws Exception {
    String udfName = SleepUDF.class.getName();
    Statement stmt1 = con.createStatement();
    stmt1.execute("create temporary function sleepUDF as '" + udfName + "'");
    stmt1.close();
    final Statement stmt = con.createStatement();
    // Thread executing the query
    Thread tExecute = new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          System.out.println("Executing query: ");
          stmt.executeQuery("select sleepUDF(t1.under_col) as u0, t1.under_col as u1, " +
              "t2.under_col as u2 from " + tableName +  "t1 join " + tableName +
              " t2 on t1.under_col = t2.under_col");
          fail("Expecting SQLException");
        } catch (SQLException e) {
          // This thread should throw an exception
          assertNotNull(e);
          System.out.println(e.toString());
        }
      }
    });
    // Thread cancelling the query
    Thread tCancel = new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(1000);
          System.out.println("Cancelling query: ");
          stmt.cancel();
        } catch (Exception e) {
          // No-op
        }
      }
    });
    tExecute.start();
    tCancel.start();
    tExecute.join();
    tCancel.join();
    stmt.close();
  }
Test the cancellation 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/cbo_rp_TestJdbcDriver2.java