cancel() is supported, but it does not support cancelling an in-progress 
single-row insert.
cancel() supports stopping a long-running SELECT, UPDATE or DELETE, but only 
between individual rows.

timbo wrote:
> I have a test case (below) where a long running db operation is
> launched in a thread, and a separate thread invokes cancel() on the
> executing Statement. The cancel() does not seem to have any effect,
> and does not throw an exception in the calling thread or the statement
> thread. The statement is in fact successfully committed to the db.
>
> Is Statement.cancel() really supported by h2?
>
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++++++
> package persistence.perf;
>
> import java.sql.Connection;
> import java.sql.DriverManager;
> import java.sql.PreparedStatement;
> import java.sql.ResultSet;
> import java.sql.SQLException;
> import java.sql.Statement;
>
> public class CancelCreateTest extends Thread {
>
>     protected PreparedStatement ps;
>     protected boolean createDone = false;
>
>     public void run() {
>       longCreate();
>     }
>
>     private void longCreate() {
>       System.out.println("" + System.currentTimeMillis() + " begin
> create");
>       Connection conn = null;
>       try {
>           conn = getConnection();
>           conn.setAutoCommit(false);
>           ps = conn.prepareStatement("INSERT INTO RESOURCE (DATA)
> VALUES(?)");
>           ps.setBytes(1, new byte[20000000]);
>           ps.executeUpdate();
>           conn.commit();
>           System.out.println("" + System.currentTimeMillis()
>                   + " committed create");
>       } catch (SQLException sqe) {
>           sqe.printStackTrace();
>           try {
>               conn.rollback();
>               System.out.println("" + System.currentTimeMillis()
>                       + " rolled back create");
>           } catch (SQLException sqe1) {
>               sqe1.printStackTrace();
>           }
>       } finally {
>           try {
>               if (conn != null) {
>                   conn.close();
>               }
>           } catch (SQLException sqe) {
>               sqe.printStackTrace();
>           }
>           createDone = true;
>       }
>     }
>
>     private int getNumRows() throws SQLException {
>       int count = -1;
>       Connection conn = getConnection();
>       Statement stmt = conn.createStatement();
>       ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM RESOURCE");
>       if (rs.next()) {
>           count = rs.getInt(1);
>       }
>       conn.close();
>       return count;
>     }
>
>     private void setup() throws Exception {
>       Class.forName("org.h2.Driver");
>       Connection conn = getConnection();
>       Statement stmt = conn.createStatement();
>       stmt.executeUpdate("CREATE TABLE RESOURCE(DATA BLOB)");
>       stmt.close();
>       conn.close();
>     }
>
>     private void cleanup() throws SQLException {
>       Connection conn = getConnection();
>       Statement stmt = conn.createStatement();
>       stmt.executeUpdate("DROP TABLE RESOURCE");
>       stmt.close();
>       conn.close();
>     }
>
>     private Connection getConnection() throws SQLException {
>       return DriverManager.getConnection("jdbc:h2:db/test", "sa", "");
>     }
>
>     public static void main(String[] args) {
>       CancelCreateTest test = new CancelCreateTest();
>       try {
>           test.setup();
>       } catch (Exception e) {
>           e.printStackTrace();
>       }
>       test.start();
>       try {
>           Thread.sleep(300);
>       } catch (InterruptedException e) {
>       }
>       try {
>           test.ps.cancel();
>           System.out.println("" + System.currentTimeMillis()
>                   + " cancel complete");
>       } catch (SQLException sqe) {
>           sqe.printStackTrace();
>       }
>       while (!test.createDone) {
>           try {
>               Thread.sleep(500);
>           } catch (InterruptedException e) {
>           }
>       }
>       int numRows = -1;
>       try {
>           numRows = test.getNumRows();
>       } catch (SQLException sqe) {
>           sqe.printStackTrace();
>       }
>       System.out.println("" + System.currentTimeMillis() + " " + numRows
>               + " rows found");
>       try {
>           test.cleanup();
>       } catch (SQLException e) {
>           e.printStackTrace();
>       }
>     }
> }
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++++++
> output:
> 1320285691079 begin create
> 1320285691379 cancel complete
> 1320285691603 committed create
> 1320285692801 1 rows found
>
>

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/h2-database?hl=en.

Reply via email to