简体   繁体   English

如何在JDBC中检测Oracle数据库关闭?

[英]How to detect Oracle database shutdown in JDBC?

In an application, we have used JDBC to connect to Oracle database. 在一个应用程序中,我们已使用JDBC连接到Oracle数据库。 We have attached. 我们已附上。 The issue is to identify through a callback/listener, that the database was shutdown. 问题是通过回调/侦听器确定数据库已关闭。

Currently, we have the the following listener attached: 当前,我们附加了以下侦听器:

Connection eventListener = new ConnectionEventListener() {
    @Override
    public void connectionErrorOccurred(ConnectionEvent event){
        // log - connection error
    }

    @Override
    public void connectionClosed(ConnectionEvent event) {
        // log - connection closed
    }
}

None of this events are triggered when the database is shutdown. 关闭数据库时,不会触发任何事件。

The idea is to detect the database shutdown and then the database start , and have a mechanism to inform the users, and to either poll for the database (configurable) or suspend the service (the application). 这个想法是先检测数据库关闭 ,然后再检测 数据库启动 ,并具有通知用户的机制,并轮询数据库(可配置)或挂起服务(应用程序)。

I'm afraid your request is not fully defined. 恐怕您的要求没有完全定义。 For example in case of RAC cluster you might configure the application to respond to ONS messages, and your application can safely migrate all the connections to another database node. 例如,对于RAC群集,您可以将应用程序配置为响应ONS消息,并且您的应用程序可以安全地将所有连接迁移到另一个数据库节点。 So even if one database node is down, your application can still work smoothly. 因此,即使一个数据库节点发生故障,您的应用程序仍可以正常运行。

For brief overview look at JBOSS source code, there is a class called exception sorter, which lists various useful Oracle errors codes. 为了简要概述JBOSS源代码,有一个称为异常排序器的类,该类列出了各种有用的Oracle错误代码。 For example , but I think the current JBOSS'es code base has even longer list. 例如 ,但是我认为当前JBOSS的代码库甚至更长。

Here is a list of error codes, I collected. 是我收集的错误代码列表。 All of the error codes express a situation, where connection to the database is not possible (for some reason). 所有错误代码均表示无法连接数据库的情况(由于某种原因)。

PS: the DCD (dead connection detection) is a different topic. PS:DCD(死连接检测)是另一个主题。

You can not properly specify the problem. 您不能正确指定问题。 You could only get connection or fail to get connection, the reason (unable to connect, db shutdown etc) could not be specified, you gonna get same exceptions with all these cases. 您只能获得连接或无法获得连接,原因(无法连接,数据库关闭等)无法指定,在所有这些情况下,您都会得到相同的异常。

您可能需要事务,因此如果查询失败,它将给您一个错误,您可以随后检查连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM