即使数据库关闭,如何使App Server启动? [英] How to make App server to start even if database is down?

查看:107
本文介绍了即使数据库关闭,如何使App Server启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用spring&冬眠.我的应用程序有3个模块.每个模块都有一个特定的数据库.因此,应用程序处理3个数据库.在服务器启动时,如果任一数据库关闭,则不会启动服务器. 我的要求是,即使其中一个数据库关闭,服务器也应在其他模块的数据库启动时启动,用户可以在其他两个模块上工作.请建议我如何做到这一点? 我正在使用spring 3.x和hibernate3.x.另外,我正在使用 c3p0连接池. 应用服务器是 Tomcat .

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.

谢谢!

推荐答案

我将使用@Configuration注释使一个对象的工作是构造bean和处理DB down场景.构造bean时,请测试数据库连接是否已建立,如果不是,则返回bean的虚拟版本.这将注入到相关对象中.这个虚拟bean的工作实际上是在调用时抛出一个不可用的异常.如果您的应用程序可以处理某些功能无法使用的异常,并在使用其他数据源时继续向用户显示该功能,那么您应该没事.

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

这篇关于即使数据库关闭,如何使App Server启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆