0

I am using transaction to move data from one place to another. Some tables are huge, I am tuning the application, but sometimes I don't want wait, I just kill the application. But the problem is that the query is already sent to the DB, although I kill my application(codes), the transaction(query) is not canceled. Is there any way to cancel or roll back the transaction when I kill my application?

I know that I can login to the DB server and kill the query. That's not the method I want to use. I am asking how to rollback or cancel the query in the codes when the application is killed?

buqing
  • 925
  • 8
  • 25

1 Answers1

3

You can use Statement.cancel() to cancel running statements. In a standalone Java application you can register a shutdown hook which is called when the Java VM is terminated to close the open statements.

Runtime.getRuntime().addShutdownHook(new Thread() {
          public void run() {
              for (Statement s : list of running statements) {
                 s.cancel();
              }
          }
    });
Community
  • 1
  • 1
Michael Koch
  • 1,152
  • 11
  • 17
  • Another quesiton, how can I get the list of running statements using jdbc? – buqing Nov 05 '15 at 19:23
  • I don't know offhand if there is an API for this or if you will have to maintain the list by yourself. I suggest posting a new StackOverflow question for this. – Michael Koch Nov 05 '15 at 19:39