如何abort/cancel HSQLDB查询

How to abort/cancel HSQLDB query

我有一个 Java SE 应用程序,它使用 Hibernate 提供的 JPA 和一个 HSQL 数据库。应用程序使用 EntityManagers 查询数据库以获取信息,以便在屏幕上绘制图表。当查询正在执行时,图表区域有一个典型的旋转图标,让用户知道它正在工作。有些查询需要很长时间,我希望具有 abort/cancel 查询的功能。我知道我可以打开 EntityManager 并获取 Hibernate Session 并调用 cancelQuery(),但这不会执行任何操作,因为 HSQLDB 驱动程序不支持 Statement 接口上的 cancel() 方法 (http://hsqldb.org/doc/2.0/apidocs/org/hsqldb/jdbc/JDBCStatement.html#cancel())。有没有其他方法可以通过 API 或重新设计我的应用程序?无法取消 运行 查询很麻烦。

您可以使用具有管理员权限的单独连接并在不同的会话中终止事务:

ALTER SESSION <session number> RELEASE

http://hsqldb.org/doc/2.0/guide/sessions-chapt.html#snc_statements

在 HSQLDB 的最新 Subversion 修订版中,此语句还会终止目标会话中 运行 的任何 SELECT 语句。

您可以使用 SESSION_ID() 找到会话的 ID 并使用它来释放会话。

此外,可以在执行查询之前使用 JDBCStatement.setQueryTimeout(n)。

不久的将来可能会支持 cancel() 方法。

At the Moment there is no way to end a long-running Statement on a HSQLDB Server. The ALTER SESSION Method provided by Fredt does not seem to work in the current RC2 for 2.3.4 (and also in the svn revision 5511), it just leads to a transaction rollback:serialization failure AFTER the select statement has finished working. So the only way to deal with this Problem is setting the Timeout. This is not an actual Solution to the Problem, though.

更新:在 SVN 存储库中最新版本的 HSQLDB(修订版 5627)和即将发布的 2.3.4 版本中,现在可以立即取消语句。还有一个新的方法,ALTER SESSSION END STATEMENT,它可以在不回滚的情况下中止事务。有关详细信息,请参阅 https://sourceforge.net/p/hsqldb/bugs/1436/