项目启动先于数据库启动时项目启动失败的解决方法
2018-03-15 22:36
260 查看
我们知道,当服务启动时会根据xml中配置的数据库信息连接数据库。假如数据库无法连接,项目就会启动失败。那么,假如数据库启动速度比项目的启动速度慢还怎么办呢?一种方法是我们可以循环加载含有数据库连接信息的xml配置文件(也就是循环连接数据库)。代码如下:List<String> configLocations = new ArrayList<String>();
configLocations.add("/../conf/appContext.xml"); //将配置文件路径字符串放入集合中
for(Integer i=1;i<60;i++){
logger.info(i+" TIME load configuration file");
try {
//加载配置文件
applicationContext = new FileSystemXmlApplicationContext(
configLocations.toArray(new String[] {}));
applicationContext.registerShutdownHook();
//配置文加载成功的话就跳出循环
logger.info(i+" TIME load configuration file SUCCESS");
break;
} catch (Exception e) {
logger.warn(i+" TIME load configuration file FAIL", e);
}
//若达到次数上限,直接抛出异常不再catch
if(i.equals(59)){
applicationContext = new FileSystemXmlApplicationContext(
configLocations.toArray(new String[] {}));
applicationContext.registerShutdownHook();
}
//若加载配置文件失败就等待5秒,等待数据库启动
Thread.sleep(5000);
}
configLocations.add("/../conf/appContext.xml"); //将配置文件路径字符串放入集合中
for(Integer i=1;i<60;i++){
logger.info(i+" TIME load configuration file");
try {
//加载配置文件
applicationContext = new FileSystemXmlApplicationContext(
configLocations.toArray(new String[] {}));
applicationContext.registerShutdownHook();
//配置文加载成功的话就跳出循环
logger.info(i+" TIME load configuration file SUCCESS");
break;
} catch (Exception e) {
logger.warn(i+" TIME load configuration file FAIL", e);
}
//若达到次数上限,直接抛出异常不再catch
if(i.equals(59)){
applicationContext = new FileSystemXmlApplicationContext(
configLocations.toArray(new String[] {}));
applicationContext.registerShutdownHook();
}
//若加载配置文件失败就等待5秒,等待数据库启动
Thread.sleep(5000);
}
相关文章推荐
- Tomcat启动时提示项目启动失败的问题的解决方法
- Oracle表空间文件损坏导致的数据库异常关闭并启动失败问题的解决方法
- SQL Server 1069 错误(由于登录失败而无法启动服务)解决方法
- 安装intel c++8.0后导致vc6编译正常的项目链接失败的一个解决方法
- SQL Server 1069 错误(由于登录失败而无法启动服务)解决方法
- VS.net 2003 打开Web项目失败的解决方法
- e2eCap-WDM Video Capture 服务启动失败解决方法
- 访问IIS元数据库失败 原因及解决方法
- e2eCap-WDM Video Capture 服务启动失败解决方法
- ASP.NET项目调试时无法启动IE的解决方法
- Struts1.2.x项目数据源配置在tomcat启动时出错解决方法
- 安装完SQL2000后启动服务器提示“由于登录失败而无法启动服务”解决方法
- ASP.NET仅配置数据库MSDE出现"用户 ’机器名/ASPNET’ 登录失败 解决方法
- e2eCap-WDM Video Capture 服务启动失败解决方法
- 访问IIS元数据库失败 原因及解决方法
- vs 2005调试程序是出现无法在web服务器上启动调试,调试失败的解决方法。
- VS.net 2003 打开Web项目失败的解决方法
- npkcrypt 服务启动失败解决方法;事件来源: Service Control Manager;事件 ID: 7000
- e2eCap-WDM Video Capture 服务启动失败解决方法
- 无法在web服务器上启动项目另一种解决方法