SSM+多数据源(oracle+mysql)+tomcat在服务停止时报错:[Abandoned connection cleanup thread] has failed to stop

今天进行SSM框架整合并使用多数据源配置(oracle+mysql),具体配置如下:
1、spring.xml



    
    

    
    
        
    

    
    
    
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
        
        
        
        
        
        
        
        
        
        
    
    
    
        
            
                
                
            
        
        
    

    
    
        
        
    
    
    
        
        
    

    
    
        
    
    

服务在启动和使用(切换数据源查询)中都没有问题,但在关闭服务的时候,会出现两个错误:
1、数据库的(mysql和oracle)驱动无法释放。

2、 The web application [] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it.  This is very likely to create a memory leak

针对驱动无法释放的问题,可以通过设置ServlectContextListner,在服务停止前,释放所有的Driver
import org.apache.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;

/**
 * Created by lenovo on 2018/2/27.
 */
public class DriverMangerListner implements ServletContextListener {
    private final static Logger logger = Logger.getLogger(DriverMangerListner.class);
    public void contextInitialized(ServletContextEvent sce) {

    }

    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("[DriverMangerListner]:-------DriverManager deregisterDriver start...");
        //com.mysql.jdbc.AbandonedConnectionCleanupThread.uncheckedShutdown();
        Enumeration enumeration = DriverManager.getDrivers();
        while (enumeration.hasMoreElements()) {
            try {
                DriverManager.deregisterDriver(enumeration.nextElement());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        logger.debug("[DriverMangerListner]:-------DriverManager deregisterDriver end...");
    }
}

针对问题2,网上查找的描述是mysql的驱动包不能加载到项目里面,需要放到tomcat的lib下,这个方法也确实可行。
问题2的第二种解决方案:在上面的listner中,添加一行代码

com.mysql.jdbc.AbandonedConnectionCleanupThread.uncheckedShutdown();

以上问题的产生原因还未知,等待后续更新...

你可能感兴趣的:(框架整合)