How to force a Java thread to close a thread-local database connection

后端 未结 9 1123
鱼传尺愫
鱼传尺愫 2021-02-13 12:41

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

9条回答
  •  生来不讨喜
    2021-02-13 13:35

    I'm looking at the same problem. Looks like so far you have to use finalize(), though it is now deprecated. As the tasks are submitted to some Executor, you'd never know when a thread exactly exits, unless the Executor shows you, that means you have control of the Executor to some extend.

    For example, if you build the Executor by extending ThreadPoolExecutor, you may override the afterExecution() and the terminated() methods to accomplish it. The former for a thread exiting abnormally while the latter for normally.

提交回复
热议问题