jTDS JDBC 在应该抛出异常时没有抛出异常
我在使用 jTDS JDBC 驱动程序时遇到以下代码问题。一切正常,查询没有问题。但如果连接失败,我不会收到错误/异常。我尝试过输入错误的 IP、禁用本地网络连接、提供错误的端口号等,但没有成功。我真的需要知道连接何时失败。
似乎一切都停在了这一行:“con = java.sql.DriverManager.getConnection(url, id, pass);” (但只有当它确实应该抛出异常时......)
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
java.sql.Connection con = null;
String url= "jdbc:jtds:sqlserver://x.x.x.x/DATABASE";
String id= "seret";
String pass = "secret";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
System.out.println("Connecting to database...");
con = java.sql.DriverManager.getConnection(url, id, pass);
System.out.println("Connected?")
//Program never gets here, but does not close either.
if(con.isValid(1000)) System.out.println("Does not work either...");
if(con!=null) con.close();
}
}
I have a problem with the following code using the jTDS JDBC Driver. Everything works, and queries is no problem. But I don`t get an error/exception if the connection is failing. I have tried to enter a false IP, disable local network connection, provide false port number etc., but no luck. I really need to know when the connection fails.
It seems that everything stops at the line: "con = java.sql.DriverManager.getConnection(url, id, pass);" (But only when it really should throw an exception...)
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
java.sql.Connection con = null;
String url= "jdbc:jtds:sqlserver://x.x.x.x/DATABASE";
String id= "seret";
String pass = "secret";
Class.forName("net.sourceforge.jtds.jdbc.Driver");
System.out.println("Connecting to database...");
con = java.sql.DriverManager.getConnection(url, id, pass);
System.out.println("Connected?")
//Program never gets here, but does not close either.
if(con.isValid(1000)) System.out.println("Does not work either...");
if(con!=null) con.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道为什么你没有得到例外。在将 SQL Server 2008 和 SQL Server 2000 与 jTDS 1.2.4 一起使用时,我确实收到了 SQLException (SQLState=S1000)。
如果升级 jTDS 驱动程序没有帮助,您可以尝试将“;loginTimeout=20”附加到 URL 字符串。所以它看起来像:
然后重新运行您的应用程序并等待至少 20 秒。希望您会遇到超时异常。
如果loginTimeout 设置没有帮助,您还可以使用socketTimeout 设置。尽管请参阅有关使用 socketTimeout 的影响的 jTDS 常见问题解答。基本上,您希望将其设置得比您期望应用程序执行的最长查询更长。
I'm not sure why you don't get an exception. I do get a SQLException (SQLState=S1000) when using SQL Server 2008 and SQL Server 2000 with jTDS 1.2.4.
If upgrading your jTDS driver doesn't help you might try appending ";loginTimeout=20" to your URL string. So it would look like:
Then rerun your application and wait at least 20 seconds. Hopefully you will get a timeout exception.
If the loginTimeout setting didn't help you can also play with the socketTimeout setting. Although see the jTDS FAQ concerning the implications of using socketTimeout. Basically you want to set it longer than the longest query you expect your application to execute.