Java程序异常退出的处理

发布于 2024-07-14 09:34:00 字数 149 浏览 13 评论 0原文

假设我有一个打开数据库连接的 Java 应用程序。 通常我会在finally块中添加一个connection.close(),但是在kill操作或任何其他异常终止的情况下该块不会被执行,不是吗? 作为一名程序员,我是否可以采取任何其他预防措施,以便在应用程序退出之前正确关闭连接?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

聊慰 2024-07-21 09:34:00

您应该查看 Java 的 Runtime.addShutdownHook() 方法 (http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread)) 。 它允许您添加一个钩子,该钩子将在虚拟机终止时被调用。 您可以使用它来调用清理例程。

不过,这适用于 TERM 信号。 KILL 信号将终止进程,并且不允许它进行任何清理(因为接收进程无法捕获或忽略 KILL 信号)。

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).

醉生梦死 2024-07-21 09:34:00

如果外部的东西杀死了你的程序,你就无能为力。 显然他们想要阻止,那你怎么阻止他们呢?

我打算建议 关闭钩子,但 Javadocs 声明:

在极少数情况下,虚拟机可能中止,即停止运行而不完全关闭。 当虚拟机从外部终止时会发生这种情况,例如使用 Unix 上的 SIGKILL 信号或 Microsoft Windows 上的 TerminateProcess 调用。 如果本机方法出现错误,例如损坏内部数据结构或尝试访问不存在的内存,则虚拟机也可能中止。 如果虚拟机中止,则无法保证是否会运行任何关闭挂钩。

(强调我的)

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)

坠似风落 2024-07-21 09:34:00

终止程序最终会使从程序到 [Oracle|SQL Server|MySQL|PostgreSQL] 服务器的 TCP 流超时。

服务器将看到它并回滚任何挂起的事务。

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.

锦爱 2024-07-21 09:34:00
  1. 您不需要在应用程序关闭时调用connection.close(),因为操作系统将自动关闭所有打开的文件。
  2. 此外,Connection 的 Finalize() 方法应该在应用程序自动关闭之前运行(如果关闭正常,而不是 ABORTed),并且应该关闭连接。
  3. 在任何情况下,您都可以注册 shutdown-hooks,执行您需要的任何清理(同样,将在正常关闭情况下运行,而不是中止)。
  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).
北恋 2024-07-21 09:34:00

当你真的被杀死时(UNIX 上的 kill -9),你无能为力。

finally-block 是您最多可以做的,请参阅SO:在 Java 中,是否保证调用“finally”块(在主方法中)? 了解详细信息。

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.

青丝拂面 2024-07-21 09:34:00

某种程度的异常终止是不可避免的。 您如何捕获服务器电源线被拉动的事件?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文