在迭代ResultSet时,JDBC丢失了连接

时间:2021-03-12 11:50:56

In my application I connect to MSSQL database via TCP/IP using Spring and JdbcDAOSupport. It works fine when the connection is stable, but when I unplug the ethernet cable while iterating through the result set the application suspends. It doesn't throw any exceptions.

在我的应用程序中,我使用Spring和JdbcDAOSupport通过TCP / IP连接到MSSQL数据库。它在连接稳定时工作正常,但是当我在迭代结果集时拔掉以太网电缆时,应用程序暂停。它不会抛出任何异常。

JdbcTemplate jdbc = getJdbcTemplate();
return jdbc.query(sql, mapper, someArgs);

where mapper is my own RowMapper class. I have tried using Connection and PreparedStatement and it doesn't solve the problem. Does anybody have any solution for this or have similar problems?

mapper是我自己的RowMapper类。我尝试过使用Connection和PreparedStatement,但它没有解决问题。有没有人有这方面的解决方案或有类似的问题?

1 个解决方案

#1


3  

The application hangs, because TCP/IP was designed with bad connections in mind. When a packet doesn't reach its destination, the sender simply retries with an exponential back-off. If this behavior is not desirable, you configure the socket blocking timeout (SO_TIMEOUT).

应用程序挂起,因为TCP / IP的设计考虑了错误的连接。当数据包未到达目的地时,发送者只需以指数退避重试。如果不希望出现此行为,请配置套接字阻塞超时(SO_TIMEOUT)。

Unfortunately, the SQL Server JDBC driver does not have an option to configure the socket timeout, so it will block indefinitely.

不幸的是,SQL Server JDBC驱动程序没有配置套接字超时的选项,因此它将无限期地阻塞。

As Nathan Hughes indicates in his comment, the jTDS driver does have an option to configure a socketTimeout, so you could try that driver instead.

正如Nathan Hughes在评论中指出的那样,jTDS驱动程序确实可以选择配置socketTimeout,因此您可以尝试使用该驱动程序。

#1


3  

The application hangs, because TCP/IP was designed with bad connections in mind. When a packet doesn't reach its destination, the sender simply retries with an exponential back-off. If this behavior is not desirable, you configure the socket blocking timeout (SO_TIMEOUT).

应用程序挂起,因为TCP / IP的设计考虑了错误的连接。当数据包未到达目的地时,发送者只需以指数退避重试。如果不希望出现此行为,请配置套接字阻塞超时(SO_TIMEOUT)。

Unfortunately, the SQL Server JDBC driver does not have an option to configure the socket timeout, so it will block indefinitely.

不幸的是,SQL Server JDBC驱动程序没有配置套接字超时的选项,因此它将无限期地阻塞。

As Nathan Hughes indicates in his comment, the jTDS driver does have an option to configure a socketTimeout, so you could try that driver instead.

正如Nathan Hughes在评论中指出的那样,jTDS驱动程序确实可以选择配置socketTimeout,因此您可以尝试使用该驱动程序。