Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6393812

Statement execute methods should cancel on interruption.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Future Project
    • Icon: P4 P4
    • None
    • 6
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      The JDBC specs should allow Thread.interrupt to cancel any thread invoking the one of the Statement.execute or PreparedStatement.execute methods. According to ExecutorService.shutdownNow():
      "There are no guarantees beyond best-effort attempts to stop
      processing actively executing tasks. For example, typical
      implementations will cancel via Thread.interrupt, so if any
      tasks mask or fail to respond to interrupts, they may never terminate."

      The java.sql.Statement is one of these task that will only terminate when the execute method returns or throws a SQLException. The JDBC specification only allows the executing query to be canceled via the Statement.cancel() method. If an application awaitsTermination during shutdown, it may block for a longer period of time when a Statement is being executed.

      JUSTIFICATION :
      This would allow the ExecutorService and FutureTask implementations to automatically halt executing queries via ExecutorService.shutdownNow() or Future.cancel(true). To support Statement cancellation it requires the programmer to design thread-safe adaptors to translate the Thread.interrupt status in to Statement.cancel(). This would provide the new query annotations to allow a common way to cancel execution of a query.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      When a cancellation thread invokes Thread.interrupt() on the thread executing the statement, a SQLInterruptedException should be thrown.
      The SQLInterruptedException class should document if Thread.isInterrupted() should return true/false after a SQLInterruptedException it is thrown.
      A new Connection and or Statement property should be added to turn off/on support for interruptible execution.
      ACTUAL -
      The execute method completes even if the interrupted status is set on the current thread before or during execution. Long running queries can only be stopped by calling Statement.cancel().

      ---------- BEGIN SOURCE ----------
      import java.sql.*;

      public class JDBCTest {
          
          public static void main(String[] args) {
              if(args.length >= 4) {
                  try {
                      Connection con = DriverManager.getConnection(args[0], args[1], args[2]);
                      try {
                          PreparedStatement ps = con.prepareStatement(args[3]);
                          try {
                              Thread.currentThread().interrupt(); //query should not execute.
                              ResultSet rs = ps.executeQuery();
                              if(rs.next()) {
                                  System.out.println("Has results. Interrupt not supported.");
                              }
                              else {
                                  System.out.println("No results. Interrupt not supported.");
                              }
                          }
                          finally {
                              ps.close();
                          }
                      }
                      finally {
                          con.close();
                      }
                  }
                  /*catch(SQLInterruptedException SIE) {
                   System.out.println("pass");
                  }*/
                  catch(SQLException SQLE) {
                      SQLE.printStackTrace();
                  }
              }
              else {
                  System.out.println("Requires url, user, password, and select query.");
              }
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      1. Write a Statement proxy and create a Thread to monitor the interrupt status of application thread and cancel the active query if the application thread is interrupted.
      2. Expose the Statement object and use a custom Future to hook the Future.cancel(boolean) to Statement.cancel(). The application must maintain a pool of all active tasks and cancel them in addition to invoking ExecutorService.shutdownNow().

            lancea Lance Andersen
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: