How to force a Java thread to close a thread-local database connection
问题 When Using a thread-local database connection, closure of the connection is required when the thread exists. This I can only do if I can override the run() method of the calling thread. Even that is not a great solution, since at time of exit, I don't know if a connection has ever been opened by that thread. The problem is in fact more general: How to force a thread to call some finalisation method of a thread-local object when it exits. I looked at the sources of java 1.5 and found that the