web应用运行一段时间就死机了

@JFinal

服务器: Linux  Tomcat8.5.37

最近web应用加了一个新功能,查询产线工人一段时间的产量报表,供手机APP展示,现在动不动就死机,不知是否是数据量太大及我的代码写的有问题,某些内存没有释放造成呢?以前没有这个现象。一死机就影响到门禁的刷码系统,上下班人数有六七百人,门禁系统也属同一个web应用,服务端也是在同一台服务器上。哪位大神帮看看,谢谢。


19-Apr-2019 17:54:08.550 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [taverp] created a ThreadLocal with key of type [com.jfinal.template.io.WriterBuffer$1] (value [com.jfinal.template.io.WriterBuffer$1@43cc1ba8]) and a value of type [com.jfinal.template.io.ByteWriter] (value [com.jfinal.template.io.ByteWriter@6b7143e0]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

19-Apr-2019 17:54:08.550 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [taverp] created a ThreadLocal with key of type [com.jfinal.template.io.WriterBuffer$1] (value [com.jfinal.template.io.WriterBuffer$1@43cc1ba8]) and a value of type [com.jfinal.template.io.ByteWriter] (value [com.jfinal.template.io.ByteWriter@4843147d]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

19-Apr-2019 17:54:08.550 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [taverp] created a ThreadLocal with key of type [com.jfinal.template.io.WriterBuffer$1] (value [com.jfinal.template.io.WriterBuffer$1@43cc1ba8]) and a value of type [com.jfinal.template.io.ByteWriter] (value [com.jfinal.template.io.ByteWriter@622eff9]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

19-Apr-2019 17:54:08.550 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [taverp] created a ThreadLocal with key of type [com.jfinal.template.io.WriterBuffer$1] (value [com.jfinal.template.io.WriterBuffer$1@43cc1ba8]) and a value of type [com.jfinal.template.io.ByteWriter] (value [com.jfinal.template.io.ByteWriter@36f9d3ef]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

19-Apr-2019 17:54:08.562 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [taverp] created a ThreadLocal with key of type [com.jfinal.template.io.WriterBuffer$1] (value [com.jfinal.template.io.WriterBuffer$1@43cc1ba8]) and a value of type [com.jfinal.template.io.ByteWriter] (value [com.jfinal.template.io.ByteWriter@3e473ac4]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

19-Apr-2019 17:54:08.563 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [taverp] created a ThreadLocal with key of type [com.jfinal.template.io.WriterBuffer$1] (value [com.jfinal.template.io.WriterBuffer$1@43cc1ba8]) and a value of type [com.jfinal.template.io.ByteWriter] (value [com.jfinal.template.io.ByteWriter@7c29d512]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.


评论区

fox

2019-04-19 18:25

2019-04-19 17:54:08
[ERROR]-[Thread: http-nio-80-exec-6]-[com.jfinal.core.ActionHandler.handle()]: /mbslipjjf/find_day_output
com.jfinal.plugin.activerecord.ActiveRecordException: com.alibaba.druid.pool.DataSourceDisableException
at com.jfinal.plugin.activerecord.DbPro.execute(DbPro.java:734)
at com.jfinal.plugin.activerecord.DbPro.execute(DbPro.java:720)
at com.jfinal.plugin.activerecord.Db.execute(Db.java:501)
at com.demo.mouldcup.SlipMouldCupJJFControll.find_day_output(SlipMBJJFControll.java:210)
at sun.reflect.GeneratedMethodAccessor60.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.jfinal.aop.Invocation.invoke(Invocation.java:73)
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:83)
at com.demo.common.MyContextPathHandler.handle(MyContextPathHandler.java:31)
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:73)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.alibaba.druid.pool.DataSourceDisableException
at com.alibaba.druid.pool.DruidDataSource.takeLast(DruidDataSource.java:1616)
at com.alibaba.druid.pool.DruidDataSource.getConnectionInternal(DruidDataSource.java:1218)
at com.alibaba.druid.pool.DruidDataSource.getConnectionDirect(DruidDataSource.java:1086)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1066)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1056)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:104)
at com.jfinal.plugin.activerecord.Config.getConnection(Config.java:203)
at com.demo.mouldcup.MouldCup_JJF_OutputCallback.call(MouldCup_JJF_OutputCallback.java:29)
at com.jfinal.plugin.activerecord.DbPro.execute(DbPro.java:732)
... 29 more

fox

2019-04-19 18:30

conn=DbKit.getConfig("sql2000").getConnection();

我这行代码有问题?

fox

2019-04-19 18:31

public Object call(Connection conn) throws SQLException {
// TODO Auto-generated method stub
conn=DbKit.getConfig("sql2000").getConnection(); //
CallableStatement proc=conn.prepareCall("{call Sp_Find_x(?,?,?,?,?)}");
proc.setString(1, dt1);
proc.setString(2, dt2);
proc.setString(3, workno);

proc.executeQuery();

rs=proc.getResultSet();
conn=null;
return rs;
}

JFinal

2019-04-20 00:29

你的代码 DbKit.getConfig("sql2000").getConnection() 是自己获取了 Connection 对象,所以需要在 finally 块中关闭它,否则死掉是迟早的事

要这样:
try {
...
}
finally {
DbKit.getConfig("sql2000").close(conn);
}

如果你是通过 DbKit.getConfig(...).getDataSource().getConnection() 获取到的数据源,可以这样来关闭:
try {
...
}
finally {
conn.close();
}

优先使用第一种获取 connection 对象

JFinal

2019-04-20 00:31

connection 未关闭属于资源泄漏,并发高的时候 JVM 会挂掉

最后,建议切换到 jfinal undertow,体验更爽

fox

2019-04-20 13:20

又挂掉了,好的,我改试试。

fox

2019-04-21 12:44

@Jfinal 如果close掉报以下错误,我很多地方调用存储过程都是用这种方式,如何才能正确close,请指正,谢谢!

com.jfinal.plugin.activerecord.ActiveRecordException: java.sql.SQLException: connection holder is null
at com.jfinal.plugin.activerecord.Model.save(Model.java:457)
at com.demo.mech.UG_mach_service_record_WXBY.saveWx(UG_mach_service_record_WXBY.java:43)
at com.demo.mech.MechControll.wxByNo(MechControll.java:141)
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:498)
at com.jfinal.aop.Invocation.invoke(Invocation.java:73)
at com.jfinal.plugin.activerecord.tx.Tx.intercept(Tx.java:77)

代码如下:
1. ---
public class sqd_newno_Callback implements ICallback {

public String tecdh=null;
public String no=null;
private String newNo="";

CallableStatement proc=null;
Connection conn2;

@Override
public Object call(Connection conn) throws SQLException {
// TODO Auto-generated method stub
try {
conn2=DbKit.getConfig("sql2000").getConnection();
proc=conn2.prepareCall("{call new_no_NEW(?,?)}");
proc.setString(1, "保养申请单");
proc.registerOutParameter(2, java.sql.Types.VARCHAR);
proc.execute();
newNo=proc.getString(2);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{

}

no=newNo;
return no;
}

public void close()
{
try {
proc.close();
conn2.close(); //测试发现这句注释掉就不报错
conn2=null;
System.out.println("conn close.");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

2. 调用处
dh=Mach_service_record_WXBY.dao.getNewNo();
flag=mach_service_record_WXBY.dao.saveWx(dh, dept, vSuper,vContent, rsn,opr,machNo);


3. getNewNo方法代码如下:

public String getNewNo()
{
String dh=null;
Mech_bysqd_newno_Callback callback=new Mech_bysqd_newno_Callback();
Db.execute(callback);
dh=callback.no;
callback.close(); // 在此处外部调用clolse
return dh;
}

JFinal

2019-04-21 15:37

callback.close() 这是在另一个线程关闭 connection ?

Db.execute 方法内部已然关闭过 connection, 无需自行关闭

使用 jfinal 给定的 API 操作数据库,永远不要自己关闭 connection , 因为 jfinal 全在 finally 中关闭过

唯一需要关闭 connection 的情况是:
你自己调用 getConnection() 方法获取过 connection

你自己获取的不在 jfinal 的管控范围内

fox

2019-04-21 15:48

@Jfinal callback.close() 是这个sqd_newno_Callback 回调类中定义的一个close方法,如下:


public class sqd_newno_Callback implements ICallback {

public String tecdh=null;
public String no=null;
private String newNo="";

CallableStatement proc=null;
Connection conn2;

@Override
public Object call(Connection conn) throws SQLException {
// TODO Auto-generated method stub
try {
conn2=DbKit.getConfig("sql2000").getConnection();
proc=conn2.prepareCall("{call new_no_NEW(?,?)}");
proc.setString(1, "保养申请单");
proc.registerOutParameter(2, java.sql.Types.VARCHAR);
proc.execute();
newNo=proc.getString(2);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{

}

no=newNo;
return no;
}

public void close()
{
try {
proc.close();
conn2.close(); //测试发现这句注释掉就不报错
conn2=null;
System.out.println("conn close.");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

JFinal

2019-04-21 16:32

@fox 最终的 connection.close() 动作并没有发生成 finally 块中,异常发生一次资源就泄漏一次

fox

2019-04-21 18:36

@Jfinal 关键是不能在 call 中内部关闭,否则在外部调用时获取不到结果集,提示connection已经为null了,所以我写了一个close方法,是在外部调用时把ResultList获取到时才调用此方法关闭的。


public Object call(Connection conn) throws SQLException {
// TODO Auto-generated method stub
try {
conn2=DbKit.getConfig("sql2000").getConnection();
proc=conn2.prepareCall("{call new_no_NEW(?,?)}");
proc.setString(1, "保养申请单");
proc.registerOutParameter(2, java.sql.Types.VARCHAR);
proc.execute();
newNo=proc.getString(2);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
conn2.close(); // 在call 回调中关闭,外部就获取不到结集了,有什么方法,即能在此关闭,外部又能获取到这个调用存储过程的结果集。
conn2=null;
}

JFinal

2019-04-21 20:23

@fox 这里面没有别的信息可提供,只有两点:
1:自行 getConnection() 的 Connection 对象确保在 finally 中关闭
2:处理好多线程共享 connection 的问题

一眼看去你的代码比较凌乱,重新做好设计

fox

2019-04-21 23:01

好的,谢谢。