Handling Spring / Tomcat Initialization Errors

Currently, if the database of our Spring application hosted on Tomcat is unavailable, context initialization fails and all requests return 404.

What would be a good way to overcome this problem? Instead of the application being unavailable until the next Tomcat, I would like it to display an error message to the user when it is unavailable and automatically restored when the database is available (for example, if the database crashes when Tomcat is already running).

I can install all the beans on lazy-init, but I'm not sure what the best solution is? Can Tomcat try to re-initialize every x seconds / requests and show a decent error page? Any ideas on this?

An example of errors that occur at startup when the database is unavailable:

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 
+6
source share
1 answer

From the exception stack trace, I see that you are using the c3p0 connection pool. Take a look at http://www.mchange.com/projects/c3p0/#configuring_recovery , where you can configure the pool to try connecting again.

But this will keep the HTTP flow active and imagine, if you have a high load, your system will go down.

It is best to show the error page to the user. In this case, I would show error 503. You can do this by placing the Apache web server in front of your tomcat, where you place page 503. There may be other ways to achieve this.

Since your web printer does not start itself, you cannot serve the error page from your web application unless you configure it to be lazy init.

You can also try auto-updating on the error page every X seconds when the url user tried to visit.

+1
source

All Articles