Question

I am using spring & hibernate. my application has 3 modules. Each module has a specific database. So, Application deals with 3 databases. On server start up, if any one of the databases is down, then server is not started. My requirement is even if one of the databases is down, server should start as other module's databases are up, user can work on other two modules. Please suggest me how can i achieve this? I am using spring 3.x and hibernate 3.x. Also i am using c3p0 connection pooling. App server is Tomcat.

Thanks!

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top