I have a webapp running on Tomcat that uses Spring to inject dependencies. (This is a GWT application, but I donβt think it matters a lot for the solution I'm looking for.)
My web.xml file has the following format:
<web-app> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatch</servlet-name> <servlet-class>com.example.my.gwt.dispatch.DispatchServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatch</servlet-name> <url-pattern>/my_gwt/dispatch</url-pattern> </servlet-mapping> ... more servlets ... </web-app>
One of my Spring configurations is connecting to the database through Hibernate:
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${db.driver}" /> <property name="url" value="${db.url}" /> <property name="username" value="${db.username}" /> <property name="password" value="${db.password}" /> </bean> <bean id="databaseSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="datasource" /> <property name="packagesToScan"> <array> <value>com.example.my.gwt.model</value> </array> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>
If the database is unavailable, this throws an org.h2.jdbc.JdbcSQLException, so Spring initialization will not continue, so the rest of the webapp cannot be used. Going to the webapp URL results in an HTTP 503 Unavailable Service error.
I want to do this in order to catch this error and display the page to the user (when they first go to the application), explaining what the problem is probably and suggested corrections. How can i do this?
I tried using the custom class ContextLoaderListener, which delegates the one specified in the XML above, but catches any exceptions. This allows me to catch an exception, but not so much what I can do - web.xml still points to a user request for a servlet that does not start after Spring failed to initialize. Is there a way to change the webapp configuration when I catch this exception so that it does not try to load servlets from web.xml and possibly modify the welcome file to point to the error page? Or is there another way I can get webapp to gracefully handle this exception?
thanks
source share