tomcat 监听器启动线程导致tomcat只允许线程的run方法,不能访问项目

项目简介:Jsp3.0+dbcp连接池1.4+tomcat8.0

因连接池泄露,有些connection无法关闭,想可以手动kill掉那些连接时间超过2分钟的Connection连接。然后写了一个线程,将它挂在ServletContextListener监听器下,然后tomcat一启动,tomcat会被该线程卡住,出现一直执行线程,而不继续加载项目的情况,导致项目无法访问,想请问下,这种情况该怎么办。怎么改这种模式才是对的。

tomcat启动错误:

信息: Deploying web application directory F:\tomcat-8.0\webapps\host-manager
一月 05, 2017 3:17:32 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory F:\tomcat-8.0\webapps\host-manager has finished in 41 ms
一月 05, 2017 3:17:32 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory F:\tomcat-8.0\webapps\manager
一月 05, 2017 3:17:32 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory F:\tomcat-8.0\webapps\manager has finished in 24 ms
一月 05, 2017 3:17:32 下午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory F:\tomcat-8.0\webapps\ROOT
一月 05, 2017 3:17:33 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
kill 5 ;
kill 6 ;
kill 7 ;
kill 5 ;
kill 6 ;
kill 7 ;

代码如下:

ServletContextListener监听器类

package com.hebeu.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ApplicationListener implements ServletContextListener{
    Thread thread = new Thread(new KillConn());

    public void contextInitialized(ServletContextEvent sce) {
        while (true) {
            try {
                thread.run();
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
        
    public void contextDestroyed(ServletContextEvent sce) {
        
    }
    
}

KillConn.java线程

package com.hebeu.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class KillConn implements Runnable{
    
    public void run() {
        String fingSql = "SELECT CONCAT('kill ',id,' ;')'sql' FROM information_schema.`PROCESSLIST` WHERE TIME>10";
        ArrayList<String> listSql = new ArrayList<String>();
        Connection conn = null;
        try {
            conn = JDBCUtil.getConn();
            conn.setAutoCommit(false);
            ResultSet rs = JDBCUtil.findOne(fingSql, conn);
            while(rs.next())
            {
                listSql.add(rs.getString(1));
            }
            for (String string : listSql) {
                System.out.println(string);
//                JDBCUtil.doSql(string, conn);
            }
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally{
            JDBCUtil.closeAll(null, null, conn);
        }
        
    }
}


未之未央丿
浏览 2409回答 0
0回答
打开App,查看更多内容
随时随地看视频慕课网APP