SqlTimeoutException expected: Got SQLServerException
See original GitHub issueDriver version or jar name
6.2.2.jre8
SQL Server version
SQL Server 2014 (Not important)
Client operating system
MacOS
Java/JVM version
1.8.0_121 (Not important)
Table schema
NA
Problem description
The java Statement-interface says:
@throws SQLTimeoutException when the driver has determined that the * timeout value that was specified by the {@code setQueryTimeout} * method has been exceeded and has at least attempted to cancel * the currently running {@code Statement} ResultSet executeQuery(String sql) throws SQLException;
But mssql-jdbc throws a com.microsoft.sqlserver.jdbc.SQLServerException: The query has timed out.
instead. I think this is an old issue, since the non open source version also has this incorrect implementation of the api. I have looked at the code, and it is a tiny bit complicated to find a solution since the IOBuffer that is now throwing this exception has no idea for the cause, it only knows that the command is interrupted.
I understand that this i an API change for the driver. But catching an exception and reading the text in the message to find out if we have a timing issue is is not a good practice.
Expected behavior and actual behavior
Should do what the java API has as a documented behaviour and throw a java.sql.SQLTimeoutException
when statement setQueryTimeout is set.
Repro code
I was inspired by #525 and made this:
public static void main(String[] args) throws Exception {
DriverManager.class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=mydatabase", "sa", "Password"
Statement statement = connection.createStatement();
statement.setQueryTimeout(1);
try {
statement.execute("WAITFOR DELAY '00:00:2'; SELECT 1");
} catch (java.sql.SQLTimeoutException e) {
System.out.println("Jey!");
} catch (java.sql.SQLException e) {
System.err.println("This is not the exception you are looking for: " + e.toString());
}
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
Hi @eivinhb ,
Thank you for creating the issue, it is indeed a deficiency in the driver. We will address the issue in one of the upcoming preview releases.
Fixed in PR https://github.com/Microsoft/mssql-jdbc/pull/641.