【发布时间】:2018-11-27 10:26:24
【问题描述】:
在我们正在开发的应用程序中,用户可以通过在文本字段中输入任意 JDBC 连接 URL 来连接到外部 RDBMS。我们的一位客户报告说,当他不小心尝试使用 MySQL JDBC URL 连接到 Microsoft SQL Server 时,我们的应用程序服务器(无限期地)冻结在 0% CPU。
下面的 Java sn -p 说明了这种情况:
public static void main(String[] args){
// note: the application running on localhost:1433 is ACTUALLY
// an MS SQL Server instance!
String jdbcUrl = "jdbc:mysql://localhost:1433/my-db";
// enable JDBC Driver Manager logging
DriverManager.setLogWriter(new PrintWriter(System.err));
// set a timeout of 5 seconds for connecting (which is blissfully ignored!)
DriverManager.setLoginTimeout(5);
// open the connection (which should fail, but freezes instead)
try (Connection c = DriverManager.getConnection(jdbcUrl)){
System.out.println("This should never be reached due to wrong JDBC URL.");
}catch(Exception e){
System.out.println("This is expected (but never printed).");
}
System.out.println("This is never printed either.");
}
运行 sn-p:
- 在 localhost:1433 上运行 SQL Server 实例(内容无关紧要)
- 拥有 MariaDB JDBC 驱动程序版本 2.2.5。 (最新)在您的类路径中。
问题:
- 1) 这可能是 MariaDB JDBC 驱动程序中的错误吗?谷歌搜索没有发现这方面的任何信息。
- 2) 我应该如何解决这个问题?我不希望我的服务器在用户意外插入无效的 JDBC URL 时冻结。
我尝试了其他几个 JDBC 驱动程序(MySQL、DB2、Oracle...),它们都很好地处理了这个问题,只有 MariaDB JDBC 驱动程序冻结了 JVM。
【问题讨论】: