如何中止正在运行的JDBC事务? [英] How can I abort a running JDBC transaction?

查看:257
本文介绍了如何中止正在运行的JDBC事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法提前中止交易?比如说,我已经向数据库发送了一个命令,运行五分钟,四分之后,我想中止它。

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.

JDBC是否定义了一种方法来发送停止你正在做这个连接向DB发信号?

Does JDBC define a way to send a "stop whatever you are doing on this connection" signal to the DB?

推荐答案

正如詹姆斯所提到的, Statement.cancel()将取消正在运行的Statement的执行(选择,更新等)。 JDBC文档特别指出Statement.cancel()可以安全地从另一个线程运行,甚至建议在超时线程中调用它。

As mentioned by james, Statement.cancel() will cancel the execution of a running Statement (select, update, etc). The JDBC docs specifically say that Statement.cancel() is safe to run from another thread and even suggests the usage of calling it in a timeout thread.

取消声明后,你仍然坚持回滚交易的工作。这是 not 记录为从另一个线程运行是安全的。 Connection.rollback()应该在主线程中执行所有其他JDBC调用。在取消的Statement.execute ...()调用使用JDBCException(由于取消)完成后,您可以处理它。

After canceling the statement, you're still stuck with the job of rolling back the transaction. That is not documented as being safe to run from another thread. The Connection.rollback() should happen in the main thread doing all the other JDBC calls. You can handle that after the canceled Statement.execute...() call completes with a JDBCException (due to the cancel).

这篇关于如何中止正在运行的JDBC事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆