Checking database availability with Spring MVC to force a redirect
I'm implementing Spring MVC but would like to redirect if one of my database goes offline. Is there any easy way to check this? The application uses several databases (one for authent开发者_如何学Cication etc.) so I need a graceful way of handling database issues.
Thanks,
For every dataSource
run something like:
new JdbcTemplate(dataSource).queryForInt("SELECT 1").
Your dataSource
might be configured to test connection before returning it, so in some cases it won't even reach query when database is down. To make the code simple, wrap this code in aspect around all your controllers.
More clean but a bit less flexible solution is to use some custom exception mapper that will catch database exceptions (Spring provides nice JDBC exceptions abstraction layer) and redirect appropriately.
Try to connect to the database, and if you can't, then redirect.
精彩评论