Robinson Robinson - 11 months ago 87
Java Question

How to get a list of running statements using JDBC

I am using DBCP and JDBC to do some data moving. Sometimes, when some query is running too long, the application may be killed; but, as you know, although the application is killed, the statement which already sent to the database is not killed. Is there a way to find the list of running queries and cancel them before killing the application?

I know that you can get a list of running queries from the DB side and the kill the query, but that's not the way I want to do it. I want to find the list of running queries (statements) which were issued by the application (a specified user account to connect to the DB) through JDBC and abort the query by using


Answer Source

The JDBC APIs don't provide any way to get a list of all Statement objects, running or otherwise. However, you could keep track of all of the Statement objects yourself. For example, you could use a WeakHashMap<Statement, Statement> to hold the extant Statement objects in a way that avoids a memory / resource leaks.

The other possibility would be to use Connection.abort(...)