关闭 tomcat 时报错了,提示可能是内存泄露(附上部分代码)

关闭 tomcat 时报错了,提示可能是内存泄露(附上部分代码)

UserDao 只有一写了一个 findAll 方法,启动的时候,打log,获取数据正常,能找到所有的用户,但关闭时debug 区有错误,提示可能是内存泄露

http://img1.sycdn.imooc.com//climg/5aa513a60001755914611033.jpg

SqlSessionFactoryUtils

public class SqlSessionFactoryUtils {
    private static String RESOURCE = "mybatis-config.xml";
    private static SqlSessionFactory sqlSessionFactory;
    private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();

    public static void initSqlSessionFactory() {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream(RESOURCE);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    }

    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public static void close() {
        SqlSession session = threadLocal.get();

        if (session != null) {
            System.out.println(session);
            session.close();
            threadLocal.set(null);
        }
    }
}

UserDao

public class UserDao {

    private List<User> listUsers;
    private SqlSession sqlSession;

    public SqlSession getSession() {
        sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
        return sqlSession;
    }

    public List<User> findAll() {
        try {
            listUsers = getSession().selectList("findAll");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.close();
        }

        return listUsers;
    }
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
提问者 walkingway 2018-03-12 22:19:17

用了老外的代码后报错减少到了 1 条(之前是3条)前两个错没有了

容器销毁中
12-Mar-2018 22:15:58.466 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
12-Mar-2018 22:15:58.469 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
12-Mar-2018 22:15:58.470 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
12-Mar-2018 22:15:58.472 SEVERE [main] org.apache.catalina.util.LifecycleBase.handleSubClassException Failed to destroy component [Connector[HTTP/1.1-8080]]
 org.apache.catalina.LifecycleException: Protocol handler destroy failed
	at org.apache.catalina.connector.Connector.destroyInternal(Connector.java:994)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:322)
	at org.apache.catalina.core.StandardService.destroyInternal(StandardService.java:543)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:322)
	at org.apache.catalina.core.StandardServer.destroyInternal(StandardServer.java:860)
	at org.apache.catalina.util.LifecycleBase.destroy(LifecycleBase.java:322)
	at org.apache.catalina.startup.Catalina.stop(Catalina.java:756)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:717)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:353)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:493)
Caused by: java.io.IOException: Thread signal failed
	at sun.nio.ch.NativeThread.signal(Native Method)
	at sun.nio.ch.ServerSocketChannelImpl.implCloseSelectableChannel(ServerSocketChannelImpl.java:292)
	at java.nio.channels.spi.AbstractSelectableChannel.implCloseChannel(AbstractSelectableChannel.java:234)
	at java.nio.channels.spi.AbstractInterruptibleChannel.close(AbstractInterruptibleChannel.java:115)
	at sun.nio.ch.ServerSocketAdaptor.close(ServerSocketAdaptor.java:137)
	at org.apache.tomcat.util.net.NioEndpoint.unbind(NioEndpoint.java:313)
	at org.apache.tomcat.util.net.AbstractEndpoint.destroy(AbstractEndpoint.java:1164)
	at org.apache.coyote.AbstractProtocol.destroy(AbstractProtocol.java:611)
	at org.apache.catalina.connector.Connector.destroyInternal(Connector.java:990)
	... 13 more


  • 把ThreadLocal相关代码去掉试试
    2018-03-13 10:04:01
好帮手慕珊 2018-03-12 10:54:23

http://img1.sycdn.imooc.com//climg/5aa5eb67000138fb09760136.jpg

这两点就是原因,因为MySQL连接关闭的问题。所以,自己写代码去撤销连接。

祝学习愉快!

  • 提问者 walkingway #1
    用了老外的代码只解决了 12-Mar-2018 22:00:08.903 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 12-Mar-2018 22:00:08.903 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [org.apache.ibatis.datasource.unpooled.UnpooledDataSource.DriverProxy] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. 12-Mar-2018 22:00:08.905 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. 以上两行错误,但没有解决下面这行 12-Mar-2018 22:00:08.915 SEVERE [main] org.apache.catalina.util.LifecycleBase.handleSubClassException Failed to destroy component [Connector[HTTP/1.1-8080]] org.apache.catalina.LifecycleException: Protocol handler destroy failed
    2018-03-12 22:16:29
提问者 walkingway 2018-03-11 22:50:30
提问者 walkingway 2018-03-11 19:59:28

具体代码我贴到 github 上了 https://github.com/walkingway/issueproject 老师请看下

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师