简体   繁体   English

当数据库Mysql崩溃时,如何在Java中知道

[英]When Database Mysql crashes , how do i know in java

I am using J2EE and a Mysql database and making a connection using JDBC. 我正在使用J2EE和Mysql数据库,并使用JDBC建立连接。 I want to know when the database crashes and how to handle it. 我想知道数据库何时崩溃以及如何处理。 Is it that the only way to check the database is to see if the connections establish successfully? 检查数据库的唯一方法是查看连接是否成功建立吗? Or there is other way? 还是有其他方法?

You can't and shouldn't need to know. 您不能也不应该知道。 As far as your program is concerned, either everything works perfectly or you can't get a connection. 就您的程序而言,要么一切运行正常,要么无法建立连接。

The program has to behave correctly if a connection can't be acquired, which in most cases probably involves some form of error display and ceasing of operations. 如果无法获取连接,则程序必须正确运行,这在大多数情况下可能涉及某种形式的错误显示和操作停止。 Naturally you'll want to have some form of monitoring so that someone will know to check what's wrong with the database, but it's not your program's responsibility. 当然,您将需要某种形式的监视,以便某人会知道检查数据库出了什么问题,但这不是程序的责任。

There are actually a lot of things that can go wrong in a database that might constitute a crash, a major failure or a partial failure. 实际上,数据库中有很多事情可能出错,可能会导致崩溃,重大故障或部分故障。 A table could get dropped by an admin, you could run out of space, etc, etc. These might allow you to get a connection or they might not. 管理员可能删除表,空间可能用完,等等,等等。这些可能使您可以建立连接,也可能无法建立连接。

It is probably going to be very difficult to distinguish between what constitutes a major failure (database down) and what is a partial failure that might allow the system to continue. 区分主要故障(数据库关闭)和部分故障(可能导致系统继续运行)之间的区别可能非常困难。

Obviously, if you can't connect to the database, you can't do much with your application. 显然,如果您无法连接到数据库,那么您就无法对应用程序做太多事情。 That might indicate the database has "crashed", but it could also be a network problem if your database in on another server. 这可能表明数据库已“崩溃”,但是如果您的数据库位于另一台服务器上,也可能是网络问题。 It doesn't really matter. 没关系。

Most applications that I have worked on don't do much in terms of checking for major failures. 我研究过的大多数应用程序在检查重大故障方面做得并不多。 But there are a few of strategies that might help: 但是,有些策略可能会有所帮助:

  1. Have some "sanity checks" (eg, query a known table). 进行一些“健全性检查”(例如,查询已知表)。 If the sanity checks fail, you have a critical problem. 如果健全性检查失败,那么您将遇到严重问题。
  2. Have an error count. 有错误计数。 If more than a certain number of errors occurred in a given time, you have a critical problem. 如果在给定时间内发生了一定数量以上的错误,那么您将遇到严重问题。

How do I know in my Java code when my DB crashes? 当数据库崩溃时,我怎么知道我的Java代码?

As of JDBC 4.1 (JavaSE 7) there is the Connection.setNetworkTimeout(Executor, int) method which make JDBC operations cease execution and throw an SQLException if they take longer than the timeout you set. 从JDBC 4.1(JavaSE 7)开始,存在Connection.setNetworkTimeout(Executor, int)方法,该方法使JDBC操作停止执行,并且如果它们花费的时间比您设置的超时时间长,则抛出SQLException。

To use this mechanism, set the network timeout to a number of milliseconds which you want to be the maximum amount of time you will wait to hear back from the DB. 要使用此机制,请将网络超时设置为毫秒数,这是您要等待从数据库收到回音的最大时间。 If any operations takes longer than the timeout you set, it will cease waiting and the method will throw an SQLException. 如果任何操作花费的时间超过您设置的超时时间,它将停止等待,并且该方法将引发SQLException。

An example of how setNetworkTimeout could be used: setNetworkTimeout的用法示例:

Connection conn = // get your connection however

conn.setNetworkTimeout(Executors.newSingleThreadExecutor(),
                       10 * 1000); // 10s timeout

Statement stmt = conn.createStatement();

// suppose DB crashes at this point in time

try {
    stmt.execute("SOME SQL HERE");
} catch (SQLException sqle) {
    // could be from network timeout,
    // but could also be caused by something else
    // You can check what your JDBC driver does
}

As you can see with this example, the fact that network timeouts only throw a SQLException makes the mechanism harder to work with. 从该示例可以看到,网络超时仅引发SQLException这一事实使该机制更难以使用。 I really wish the architects of JDBC 4.1 had decided to require some subclass of SQLException be thrown so that there was a portable way of telling when a network timeout occurs. 我真的希望JDBC 4.1的架构师决定要求SQLException某些子类,以便有一种可移植的方式来告知何时发生网络超时。 Something like SQLNetworkTimeoutException as a subclass of SQLException would have been nice. SQLNetworkTimeoutException这样的SQLNetworkTimeoutException作为SQLException的子类会很好。

In any case, your best bet for checking whether or not your SQLException was caused from a network timeout is by checking the message String on the SQLException , but be a bit careful doing this as different JDBC drivers will have different messages for network timeouts. 在任何情况下,用于检测是否您最好的选择SQLException是从网络超时导致通过检查在消息字符串SQLException ,但小心一点这样做,因为不同的JDBC驱动程序将对网络超时不同的消息。


Another thing you could try in addition to or besides network timeout is checking for SQLNonTransientException . 除了网络超时外,您还可以尝试的另一件事是检查SQLNonTransientException Typically the exception that will occur when there is some sort of catastrophic DB failure is the SQLNonTransientException which is the subclass of SQLException . 通常,当发生某种灾难性的DB故障时将发生的异常是SQLNonTransientException ,它是SQLException的子类。

Essentially this exception means "something went wrong that is outside of the control of your code or the JDBC driver's" . 本质上,此异常表示“代码或JDBC驱动程序无法控制的错误” So generally when you see this, it's because of a DB failure or network failure. 因此,通常情况下,这是由于数据库故障或网络故障引起的。

How do I handle a DB failure once I know something went wrong? 知道发生问题后,如何处理数据库故障?

This part is a bit tricky, especially since so many things can go wrong with a DB (it could have crashed, the network could be down/slow, etc). 这部分有些棘手,特别是因为DB可能有很多错误(它可能崩溃了,网络可能宕机了/慢了,等等)。

Option A: Show that something went wrong 选项A:表明出现了问题
In most cases I've seen, the way to "handle" this sort of failure is to acknowledge it and just present it to the user. 在我看到的大多数情况下,“处理”此类失败的方法是确认并仅将其呈现给用户。

Option B: Retry and/or failover 选项B:重试和/或故障转移
Another option is doing a fixed number of retries and/or failing over to a backup database. 另一种选择是进行固定次数的重试和/或故障转移到备份数据库。 Failing over to a backup db can be tricky, and typically this is only done if you have some sort of middleware do it for you. 故障转移到备份数据库可能很棘手,通常只有在您有某种中间件为您完成时才能完成。

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

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