java – 处理Spring / Tomcat初始化失败

前端之家收集整理的这篇文章主要介绍了java – 处理Spring / Tomcat初始化失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

目前,如果我们在Tomcat上托管的Spring应用程序的数据库不可用,则上下文初始化失败,所有请求都返回404.

什么是解决这个问题的好方法?我不希望应用程序在下一个Tomcat之前不可用,我希望它在不可用时向用户显示错误消息,并在数据库可用时自动恢复(就像在Tomcat已经运行时发生数据库故障一样).

我可以将所有bean设置为lazy-init,但我不确定这是最好的解决方案吗? Tomcat不能每隔x秒/请求重试一次初始化并同时显示一个不错的错误页面吗?有什么想法吗?

数据库不可用时启动时抛出的错误示例:

Caused by: java.sql.sqlException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.sqlUtils.tosqlException(sqlUtils.java:106)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy$LazyConnectionInvocationHandler.getTargetConnection(LazyConnectionDataSourceProxy.java:401)
at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy$LazyConnectionInvocationHandler.invoke(LazyConnectionDataSourceProxy.java:376)
at com.sun.proxy.$Proxy43.getMetaData(Unknown Source)
at com.googlecode.flyway.core.dbsupport.DbSupportFactory.getDatabaseProductName(DbSupportFactory.java:103)
... 67 more
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
... 72 more
Mar 28,2013 11:19:47 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Mar 28,2013 11:19:47 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [] startup Failed due to prevIoUs errors
最佳答案
从异常堆栈跟踪中我看到您正在使用c3p0连接池.查看http://www.mchange.com/projects/c3p0/#configuring_recovery,您可以在其中配置池以重试连接.

但这会使http线程保持活动状态,并想象如果你的负载很高,你的系统就会崩溃.

最好向用户显示错误页面.在这种情况下,我会显示503错误.您可以通过将Apache Web服务器放在托管503页面的tomcat前面来完成此操作.还有其他方法可以实现这一目标.

由于您的webapp本身无法启动,因此除非您将其配置为延迟初始化,否则无法从您的webapp提供错误页面.

您还可以每隔X秒尝试在错误页面上尝试自动刷新,以便网址用户尝试访问.

原文链接:https://www.f2er.com/spring/432183.html

猜你在找的Spring相关文章