Stop Spring Application If Database Connection is not available

Viewed 112

In my spring boot application, we are connecting to our oracle database. After trying to connect to DB 2-3 times, I want my application to shut down automatically if the DB connection is failed. How can I do it?

3 Answers

There are few ways but one quick way is to use exit method of SpringApplication.

public YourClass{

@Autowired
ApplicationContext ctx;

public dbconnectivityCheck{
  //if connection attempts failed
  SpringApplication.exit(ctx);
}
}
  SpringApplication.run(Your_Application.class, args).close();

Use this to close the context and release all resources as well.

Here is the answer for my question, basically ApplicationContext and CommandLineRunner did the magic here. Please check the image.

Related