H2 Database: Abort Query

2019-07-15 19:57发布

问题:

I have a long running select query on an embedded H2 Database and want to allow the user to cancel the query.

How can this be done? I cannot find anything about this.

[UPDATE] To be more specific I'm running my query using JPA. How can the query be stopped?

回答1:

H2 supports a query timeout setting. You can set this in the database URL as follows: jdbc:h2:~/db/test;query_timeout=10000. (Maybe this is not the right approach for you, but it might be for others that read this question.)

You can also cancel a query running in another connection (session) using the cancel_session function. But for this you need to enable the multi-threaded mode, which is currently not recommended for production use (it is still experimental in version 1.3.175).



回答2:

If you have a few queries which might take very long, then don't use JPA for them. Create your own statement so you can cancel it.

Alternatively, get the source code for a JDBC proxy like log4jdbc and add code that allows you to get connections and statements per thread. That would allow you to get the statement after JPA has sent it to the proxy.



标签: sql jpa jdbc h2