如果假設我在初始化#的ServletContextListener contextInitialized)數據源(如:怎麼辦時的ServletContextListener#contextInitialized()失敗
package com.myapp.listeners;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.sql.DataSource;
public class MyAppListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent event) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
try{
InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource) initialContext.lookup("jdbc/myDB");
event.getServletContext().setAttribute("datasource", dataSource);
}catch(NamingException ne){
// TODO WHAT TO DO HERE?
}
}
}
所以,如果初始化數據源失敗是什麼?我如何防止Tomcat部署此應用程序,因爲沒有數據源,我沒有應用程序的部署意義。
可能值得一讀:http://stackoverflow.com/questions/4031275/prefered-way-to-handle-java-exceptions-in-servletcontextlistener – DaveH 2014-10-28 16:05:24