jTDS JDBC 在应该抛出异常时没有抛出异常

发布于 2024-12-20 17:36:56 字数 933 浏览 1 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷迭香的记忆 2024-12-27 17:36:56

我不知道为什么你没有得到例外。在将 SQL Server 2008 和 SQL Server 2000 与 jTDS 1.2.4 一起使用时,我确实收到了 SQLException (SQLState=S1000)。

如果升级 jTDS 驱动程序没有帮助,您可以尝试将“;loginTimeout=20”附加到 URL 字符串。所以它看起来像:

String url= "jdbc:jtds:sqlserver://x.x.x.x/DATABASE;loginTimeout=20";

然后重新运行您的应用程序并等待至少 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:

String url= "jdbc:jtds:sqlserver://x.x.x.x/DATABASE;loginTimeout=20";

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文