How can I abort a running JDBC transaction?

后端 未结 4 1073
温柔的废话
温柔的废话 2020-11-30 03:42

Is there a way to prematurely abort a transaction? Say, I have sent a command to the database which runs five minutes and after four, I want to abort it.

Does JDBC d

4条回答
  •  半阙折子戏
    2020-11-30 04:25

    I am guessing what you want to do is prevent your application blocking on long running queries / transactions. To this end, JDBC supports the concept of a query time out. You can set the query timeout using this:

    java.sql.Statement.setQueryTimeout(seconds)
    

    And handle the SQLException thrown by the execute() method by rolling back the transaction (of course, that would only work if you have autocommit set to false, and your JDBC driver supports Statement.cancel()).

提交回复
热议问题