问题
Suppose I have a Java application that opens a database connection. Normally I would add a connection.close() in a finally block, but this block wouldn't be executed in the case of a kill operation, or any other abnormal termination, would it? Are there any other precautions that I, as a programmer, can make in order to close the connection properly before the application exits?
回答1:
You should look at the Runtime.addShutdownHook() method for Java (http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)). It allows you to add a hook that will be called when the virtual machine terminates. You can use it to call a cleanup routine.
That would be for a TERM signal though. A KILL signal will kill the process and not allow it to do any cleanup (because the KILL signal cannot be caught or ignored by the receiving process).
回答2:
If something external kills your program, there's nothing you can do about it. Obviously they wanted to stop it, so how can you prevent them?
I was going to suggest a shutdown hook, but the Javadocs state:
In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the
SIGKILL
signal on Unix or theTerminateProcess call
on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.
(emphasis mine)
回答3:
Killing a program will eventually timeout a TCP
stream from your program to your [Oracle|SQL Server|MySQL|PostgreSQL]
server.
The server will see it and rollback any pending transactions.
回答4:
- You shouldn't need to call connection.close() on application shut-down, since all open files will be closed automatically by the operating system.
- Also, the Connection's finalize() method should be run before application shut-down automatically (if the shut-down is normal, not ABORTed), and that should close the connection.
- In any case, you can register shutdown-hooks, to do any clean-up you require (again, will be run in normal shutdown cases, not ABORTs).
回答5:
When you really get killed (kill -9
on UNIX), you can not do anything against that.
A finally
-block is the most you can do, see SO: In Java, is the “finally” block guaranteed to be called (in the main method)? for details.
回答6:
Some level of abnormal termination is unavoidable. How would you catch the event of the power cable being pulled on the server?
来源:https://stackoverflow.com/questions/590899/handling-abnormal-java-program-exits