views:

526

answers:

6

Hello all,

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  A: 

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.

Johannes Weiß
+2  A: 

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.

Quassnoi
+3  A: 

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 now allow it to do any cleanup.

Chris Dail
+1  A: 

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 the TerminateProcess 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)

Michael Myers
+2  A: 
  1. You shouldn't need to call connection.close() on application shut-down, since all open files will be closed automatically by the operating system.
  2. 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.
  3. 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).
Avi
finalize typically will not run in normal application shutdown. (And Windows 95(!) wont close your sockets after an abnormal exit!)
Tom Hawtin - tackline
+1  A: 

Some level of abnormal termination is unavoidable. How would you catch the event of the power cable being pulled on the server?

Chase Seibert