java.net.SocketException:Socket关闭:处理连接中断

时间:2018-05-25 08:27:02

标签: java jdbc jtds

我与数据库服务器的连接很少,而且耗时很长的查询,有时失败,但有异常:

Caused by: java.sql.SQLException: I/O Error: Socket closed
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2481)
    at net.sourceforge.jtds.jdbc.TdsCore.getNextRow(TdsCore.java:805)
    at net.sourceforge.jtds.jdbc.JtdsResultSet.next(JtdsResultSet.java:611)
Caused by: java.net.SocketException: Socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:152)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.io.DataInputStream.readFully(DataInputStream.java:195)
    at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(SharedSocket.java:885)
    at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(SharedSocket.java:731)
    at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(ResponseStream.java:477)
    at net.sourceforge.jtds.jdbc.ResponseStream.read(ResponseStream.java:146)
    at net.sourceforge.jtds.jdbc.TdsData.readData(TdsData.java:901)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsRowToken(TdsCore.java:3175)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2433)

如何处理连接中断?目前我必须手动重新运行操作,直到它成功执行,也许它可以在jdbc驱动程序级别完成

ps socketTimeout属性似乎不会影响此

1 个答案:

答案 0 :(得分:0)

您确定这是因为查询/程序需要很长时间吗?由于Socket关闭错误通常意味着网络连接本身出现问题,基本上是您的驱动程序无法控制的......

总的来说,我建议转向基于池的机制,它可以更好地控制持久层交互。