[英]JDBC Microsoft SQL timeout not fired
I am using sqljdbc4.jar
for connection from my application to SQL Server via wifi. 我正在使用sqljdbc4.jar
通过wifi从我的应用程序连接到SQL Server。 The connection works fine and queries too, but sometimes the wifi falls and I need to react - try to reconnect and then inform the user about a connection fail. 连接工作正常并且也可以进行查询,但是有时wifi掉线了,我需要做出反应-尝试重新连接,然后通知用户连接失败。
The issue is, even when I set query timeout, the execution hangs in case of disconnection and no SqlTimeoutException
is fired. 问题是,即使我设置了查询超时,在断开连接的情况下执行也会挂起,并且不会触发任何SqlTimeoutException
。
The connection (username and psw deleted): 连接(删除用户名和psw):
SQLconnection = DriverManager.getConnection("jdbc:sqlserver://%address%;user=%UserName%;password=%password%;database=%db_name%");
SQLSelectStatement = SQLconnection.createStatement();
SQLSelectStatement.setQueryTimeout(2);
Execution of query: 执行查询:
String sql = "%my query%";
ResultSet rs;
try {
rs = SQLSelectStatement.executeQuery(sql);
} catch (SQLException sqle) {
ConnectSQL(); //try to connect again
rs = SQLSelectStatement.executeQuery(sql);
}
If the method throws a SQLException
, I'll inform the user and continue. 如果该方法抛出SQLException
,我将通知用户并继续。 But the exception is never thrown, even in the case of connection lost. 但是,即使在连接丢失的情况下,也永远不会抛出异常。
What can I do to resolve this? 我该怎么解决? I tried setting lock-timeout and logintimeout in connection string based on MSDN , method SQLconnection.setNetworkTimeout
fires error too (if I understand it right, it is not implemented in microsoft driver). 我尝试在基于MSDN的连接字符串中设置lock-timeout和logintimeout,方法SQLconnection.setNetworkTimeout
触发错误(如果我理解正确,则不会在Microsoft驱动程序中实现)。
The last solution I can think of is to implement some kind of timeout on my own, which will stop the query thread after a certain time. 我能想到的最后一个解决方案是自行实现某种超时,该超时将在一定时间后停止查询线程。 But I don't wont to reinvent the wheel. 但是我不会重新发明轮子。
Based on my googling and reading, setQueryTimeout
is useful only for limitting of query execution time, not for detecting of connection error. 根据我的谷歌搜索和阅读, setQueryTimeout
仅用于限制查询执行时间,而不用于检测连接错误。
I have found this great article describing timeouts in JDBC drivers. 我发现这篇很棒的文章描述了JDBC驱动程序中的超时。 All I needed was to hit socketTimeout
which is not supported in MS JDBC driver. 我所需要做的只是点击socketTimeout
,MS JDBC驱动程序不支持。
I switched to jTDS driver and rewrited my code a bit. 我切换到jTDS驱动程序并稍微重写了我的代码。 Now it works as I expected. 现在它可以按我的预期工作了。
//initialize SQL connection
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Cls_log.LogError(ex);
}
private void ConnectSQL() {
try {
String url = "jdbc:jtds:sqlserver://<ip_address>/<db_name>;instance=<SQLserver_name>;loginTimeout=5;socketTimeout=2";
SQLconnection = DriverManager.getConnection(url, <userName>, <password>);
SQLSelectStatement = SQLconnection.createStatement();
} catch (SQLException ex) {
Cls_log.LogError(ex);
}
}
Executing my query 执行我的查询
String sql = "%my query%";
ResultSet rs;
try {
rs = SQLSelectStatement.executeQuery(sql);
} catch (SQLException sqle) {
ConnectSQL(); //try to connect again
rs = SQLSelectStatement.executeQuery(sql);
}
If this throws SQLException, I inform user and offer workaround. 如果这引发SQLException,我将通知用户并提供解决方法。
此外,最新的Microsoft驱动程序也支持它: https : //github.com/Microsoft/mssql-jdbc/pull/85/files
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.