How to make App server to start even if database is down?

这一生的挚爱 提交于 2019-12-04 15:44:14

I would use the @Configuration annotation to make an object who's job it is to construct the beans and deal with the DB down scenario. When constructing the beans, test if the DB connections are up, if not, return a Dummy Version of your bean. This will get injected into the relevant objects. The job of this dummy bean is to really just throw an unavailable exception when called. If your app can deal with these unavailable exceptions for certain functions and show that to the user while continuing to function when the other datasources are used, you should be fine.

@Configuration
public class DataAccessConfiguration {

  @Bean
  public DataSource dataSource() {
   try {
     //create data source to your database 
     ....
     return realDataSource;
   } catch (Exception) {
     //create dummy data source
     ....
     return dummyDataSource;
   }
  }
}

This was originally a comment:

Have you tried it? You wouldn't know whether a database is down until you connect to it, so unless c3p0 prevalidates all its connections, you wouldn't know that a particular database is down until you try to use it. By that time your application will have already started.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!