简体   繁体   中英

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 statement.cancel() .

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(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM