tomcat服务器宕机解决方案

  package cn.listener;

  import java.sql.Driver;

  import java.sql.DriverManager;

  import java.sql.SQLException;

  import java.util.Enumeration;

  import javax.servlet.ServletContextEvent;

  import javax.servlet.ServletContextListener;

  import javax.servlet.annotation.WebListener;

  import com.mysql.jdbc.AbandonedConnectionCleanupThread;

  @WebListener

  public class ContextFinalizer implements ServletContextListener {

  public void contextInitialized(ServletContextEvent sce) {

  }

  public void contextDestroyed(ServletContextEvent sce) {

  Enumeration drivers = DriverManager.getDrivers();

  Driver d = null;

  while (drivers.hasMoreElements()) {

  try {

  d = drivers.nextElement();

  DriverManager.deregisterDriver(d);

  System.out.println(String.format("ContextFinalizer:Driver %s deregistered", d));

  } catch (SQLException ex) {

  System.out.println(String.format("ContextFinalizer:Error deregistering driver %s", d) + ":" + ex);

  }

  }

  try {

  AbandonedConnectionCleanupThread.shutdown();

  } catch (InterruptedException e) {

  System.out.println("ContextFinalizer:SEVERE problem cleaning up: " + e.getMessage());

  e.printStackTrace();

  }

  }

  }