Blaze
Blaze

Reputation: 59

How do you implement an error handling for when MySQL loses connection?

Trying to error handling for when an application is running but database(MySQL)lose connection.

Currently the error shows "ERR_TOO_MANY_REDIRECTS"

Could this be because I implemented an CustomErrorController with the code below?

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";
    }
}

The above code was so that when a user enters a URL that does not exist, the user gets redirected to the top page.

Anyways, I'm wondering how I could implement error handling feature for when MySQL loses connection. Want to show like a MySQL lost connection error.

On my console in Spring Boot I get:

java.net.ConnectException: Connection refused (Connection refused)

Should I create a try catch block in my CustomErrorController? Im not sure what goes inside the try block...

Upvotes: 0

Views: 158

Answers (1)

Umesh Rajbhandari
Umesh Rajbhandari

Reputation: 1282

You did not show which Controller redirects to /error. Most probably the Controller that handles /todoLists redirects to /error in case of ConnectionException. If that is true then what is happening is /todoLists redirects to /error and /error redirects to /todoLists. Because of this the browser is complaining about too many redirects problem.

Upvotes: 1

Related Questions