package com.kmall.common.listener;


import com.alibaba.druid.proxy.DruidDriver;

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;

/**
 * @author huangyq
 * @version 1.0
 * 2019-02-28 15:00
 */

@WebListener
public class ContextFinalizer implements ServletContextListener {
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        Enumeration<Driver> 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 {
//            DruidDriver.getInstance().resetStat().shutdown();
//        } catch (InterruptedException e) {
//            System.out.println("ContextFinalizer:SEVERE problem cleaning up: " + e.getMessage());
//            e.printStackTrace();
//        }
    }
    @Override
    public void contextInitialized(ServletContextEvent sce) {
    }
}