question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SqlTimeoutException expected: Got SQLServerException

See original GitHub issue

Driver 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:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ulviicommented, Feb 26, 2018

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.

0reactions
peterbaecommented, Mar 27, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Facing com.microsoft.sqlserver.jdbc.SQLServerException
A read time out is a command time which is raised by the client component, when the execution of a query takes longer...
Read more >
Examples with SQLTimeoutException - java.sql ...
Examples with SQLTimeoutException used on opensource projects java.sql. ... currentTimeMillis() - startTime) + " ms, but expected to be greater than 1000"); ...
Read more >
SQL server JDBC does not throw Lock timeout exception but ...
In SSMS studio, it works as expected, the connection 2: lock timeout. But with JDBC, the connection 2: lock wait for a few...
Read more >
the Query has timed out, when trying to open any item from ...
So the corresponding data classes got created. Now, when i am trying to open ... I checked in Context.xml file, the values are...
Read more >
JDBC Exception Handling - How To Handle SQL Exceptions
In JDBC, we may get exceptions when we execute or create the query. Exceptions that occur due to the Database or Driver come...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found