1.出现的问题
日志文件没有新的输出。
2.问题分析
dump出堆栈,如下:
"main" prio=10 tid=0x000000005de12000 nid=0x27e8 runnable [0x0000000040240000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.DataInputStream.readFully(DataInputStream.java:178)
at java.io.DataInputStream.readFully(DataInputStream.java:152)
at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(SharedSocket.java:842)
at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(SharedSocket.java:723)
- locked <0x0000000759fd9f90> (a java.util.ArrayList)
at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(ResponseStream.java:466)
at net.sourceforge.jtds.jdbc.ResponseStream.read(ResponseStream.java:135)
at net.sourceforge.jtds.jdbc.ResponseStream.readString(ResponseStream.java:290)
at net.sourceforge.jtds.jdbc.ResponseStream.readNonUnicodeString(ResponseStream.java:274)
at net.sourceforge.jtds.jdbc.TdsData.readData(TdsData.java:936)
at net.sourceforge.jtds.jdbc.TdsCore.tdsRowToken(TdsCore.java:3007)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2270)
at net.sourceforge.jtds.jdbc.TdsCore.getNextRow(TdsCore.java:765)
at net.sourceforge.jtds.jdbc.JtdsResultSet.next(JtdsResultSet.java:596)
at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207)
at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:207)
at com.jd.cis.worker.OppProductWorker.importData(OppProductWorker.java:310)
at com.jd.cis.worker.BaseWorker.importDataSource(BaseWorker.java:416)
at com.jd.cis.worker.OppProductWorker.doRun(OppProductWorker.java:276)
at com.jd.cis.worker.BaseWorker.run(BaseWorker.java:448)
at com.jd.cis.worker.OppProductWorker.main(OppProductWorker.java:564)
上面有锁,对应的代码为
statement.setFetchSize(1000);
.....
resultSet = statement.executeQuery();
while (resultSet.next()) {
......
}
查询相关文档,了解到JTDS默认的socketTimeout为0,即读取数据没有超时时间。
当数据链接意外终止的时候,JDTS可能检测不到,就会一直等待数据,阻塞进程。
在本地重现该问题,在循环体里面设置断点,进入断点后,断掉所有网络链接,取消断点,继续运行,发现程序被一致阻塞了。
当设置socketTimeout为60秒,重试,程序阻塞60秒后抛出异常。
该问题在查询SQLServer数据库,大数据量查询,分批获取的时候,容易出现
3.解决方案
为jdts的url链接中加上socketTimeout(单位秒)的设置,或在dbcp中如下设置
<property name="connectionProperties" value="characterEncoding=UTF-8;socketTimeout=60"/>
这个参数和mybatis的defaultStatementTimeout 参数是不一样的
同时Worker中加入异常捕获重试机制