4

Is there any way to make the program go through the shutdown hook if the user forces java to close (through the task manager or by closing the corresponding batch file).

My program currently runs and executes well, if the user closes the GUI then it goes through a set of steps to disconnect from the database. However, if the user closes the Java or the batch file (running side by side with the GUI) then the connection to the database isn't closed.

Is it possible to somehow force the connection closed and maybe even delete something from the tables? The batch file will probably not be an issue when I jar the program but the process killing still will.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Cody
  • 870
  • 4
  • 21
  • 38

1 Answers1

7

Nope.

The shutdown hook will react to Ctrl+C, normal closes, user logouts, and normal system shut downs (which request a graceful shutdown of all applications).

However, if someone is force-closing the app it's assumed that that's what you actually want - immediate termination with no further notice. This question has been asked many times, and is confused by the behavior of many applications that, when they are asked to force-close, they actually take a long time to finally terminate. This is because in the OS's efforts to release all resources, some resources (especially certain I/O and/or file resources) don't let go immediately.

In testing an app that starts, and is intended to be running until a graceful shutdown (e.g. server software) you should run it at the console/command-line, and press Ctrl+C to stop the program (which will run the shutdown hook) rather than using Task Manager, or KILL -9.

Furthermore, there's nothing Java could even do about it if it wanted to. A force-close happens at the OS level, at which point it releases the memory, file and I/O handles in use, etc. Java does not (nor does any other program) have control over a force-close. At this point, the OS is taking charge.

jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • Yah, I understand that, but is there any other method for ensuring this, through another program or something that runs side-by-side. Like viruses for example, if force closed they just re-open. I'm not making a virus obviously but just a database closure before exit. – Cody Nov 02 '11 at 00:43
  • 2
    +1 for pointing out that the termination is occurring at the OS level. – Tim Bender Nov 02 '11 at 00:43
  • 1
    @Cody, your database server should close the connection after a timeout. As for any temporary or working data in the database, try using transactions. The monitor process thing is just a rabbit hole. – Tim Bender Nov 02 '11 at 00:45
  • You could probably hack something together (such as monitoring the list of running processes, and using the termination to trigger a reaction), but the point I'm trying to make is that it is **by design** that the force close is a dead-end. Trying to work around, or react to a force close is NOT what you want to do. It's a force close. Like death, it's part of life, and trying to fight it will only take you down paths that try to fight agains the way the OS/application life cycle is supposed to run. Most questions that ask this don't fully grasp this reality. – jefflunt Nov 02 '11 at 00:48
  • The only exception to this sort of behavior I can think of (there may be others) is software/infrastructure monitoring systems designed to watch for force closes/crashes, and do some automated task as a result (e.g. restart services, send a notification to an admin, etc.). – jefflunt Nov 02 '11 at 00:49
  • well in a way that is the idea, but if a user logs in to the his session through the program, and he is not removed from the list of logged in users he will not be able to re-login until his name is manually removed. (Thats how my program works) – Cody Nov 02 '11 at 01:07
  • Hm, I supported a banking app that had what sounds like similar functionality. The idea in that case was to prevent the same user account from being able to be logged in from multiple stations (in this case, Teller stations). In the case of that app, since it was considered a security measure, users just had to deal with remembering to log themselves out, or they had to call an admin to force them out. I.E. the bank put the burden of "doing the right thing" on the user. – jefflunt Nov 02 '11 at 01:13