Home > Software design >  Stop Spring Application If Database Connection is not available
Stop Spring Application If Database Connection is not available

Time:07-02

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?

CodePudding user response:

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

CodePudding user response:

  SpringApplication.run(Your_Application.class, args).close();

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

  • Related