【问题标题】:How do you implement an error handling for when MySQL loses connection?当 MySQL 失去连接时,如何实现错误处理?
【发布时间】:2018-01-30 03:21:19
【问题描述】:

尝试在应用程序正在运行但数据库(MySQL)失去连接时进行错误处理。

目前错误显示“ERR_TOO_MANY_REDIRECTS”

这可能是因为我使用下面的代码实现了一个 CustomErrorController 吗?

package com.teamlab.todolist.web;

import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.net.ConnectException;


@Controller
public class CustomErrorController implements ErrorController {

    @GetMapping("/error")
    String backToTopePage() {
        return "redirect:/todoLists";
    }

    public String getErrorPath() {
        return "/todoLists";
    }
    try {

    } catch (ConnectException e) {
        return "error/database";
    }
}

上面的代码是这样的,当用户输入一个不存在的 URL 时,用户会被重定向到首页。

无论如何,我想知道如何在 MySQL 失去连接时实现错误处理功能。想要显示为 MySQL 丢失连接错误。

在我的 Spring Boot 控制台上,我得到:

java.net.ConnectException:连接被拒绝(连接被拒绝)

我应该在我的 CustomErrorController 中创建一个 try catch 块吗?我不确定 try 块中的内容...

【问题讨论】:

    标签: java spring spring-mvc spring-boot


    【解决方案1】:

    您没有显示哪个控制器重定向到 /error。在发生 ConnectionException 的情况下,处理 /todoLists 的控制器很可能会重定向到 /error。如果这是真的,那么正在发生的事情是 /todoLists 重定向到 /error 并且 /error 重定向到 /todoLists。因此,浏览器抱怨重定向问题太多。

    【讨论】:

      猜你喜欢
      • 2018-04-19
      • 1970-01-01
      • 2021-07-23
      • 2016-10-11
      • 2013-02-18
      • 2016-03-03
      • 1970-01-01
      • 2020-09-26
      • 2020-02-07
      相关资源
      最近更新 更多