关闭 tomcat 时报错了,提示可能是内存泄露(附上部分代码)
UserDao 只有一写了一个 findAll 方法,启动的时候,打log,获取数据正常,能找到所有的用户,但关闭时debug 区有错误,提示可能是内存泄露
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; } }
63
收起
正在回答 回答被采纳积分+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
walkingway
2018-03-11 22:50:30
搜到个老外的解决办法,试过了起作用,老师看看为啥会这样 https://www.ralph-schuster.eu/2014/07/09/solution-to-tomcat-cant-stop-an-abandoned-connection-cleanup-thread/
walkingway
2018-03-11 19:59:28
具体代码我贴到 github 上了 https://github.com/walkingway/issueproject 老师请看下
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星