import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
public class AppServletContextListener implements ServletContextListener {
private static final Logger logger = LogManager.getLogger();
@
Override public void contextInitialized(ServletContextEvent sce) {
}
@
Override public void contextDestroyed(ServletContextEvent sce) {
// This manually deregisters JDBC driver, which prevents Tomcat from complaining about memory leaks to this class
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
logger.log(
Level.INFO, String.format("deregistering jdbc driver: %s", driver));
} catch (SQLException e) {
logger.log(Level.ERROR, String.format("Error deregistering driver %s", driver), e);
}
}
}
}