Don Rhummy
Don Rhummy

Reputation: 25820

Is there a reliable (proper?) way for code to know if a SQLException was due to a connection error?

I know that an exception to due a connection error usually has a string with "Connection closed" or something similar, but that's not really a good way to know if it was a connection problem that cause the sql error instead of a data integrity issue.

For example, what if the code is executing multiple statements and the connection dies between statement 1 and 2? How cna I know I know it's a connection issue and not a problem with the sttement?

EXAMPLE:

try
{
    conn.createStatement().execute( sql );

    //Imagine it dies here

    conn.createStatement().execute( sql2 );
}
catch (SQLException e)
{
    //Something like one of these
    if ( e.getErrorCode() === SQLException.CONNECTION_ERROR ) {}
    if ( e instance of SQLCOonnectionException ) {}
}

Upvotes: 0

Views: 199

Answers (1)

Atul
Atul

Reputation: 1566

I think you are looking for something which is explained here https://docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html

Oracle has also an given example how you can retrieve a lot of information like error code, SQLstate etc. from an SQLException.

Upvotes: 1

Related Questions