1

I`m using the following method to close my connection to java DB:

public void shutdownDisconnect() {

    if(connectionExists) {
        String databaseURL = getDBurl();
        dbProperties.put("shutdown", "true");
        try {
            DriverManager.getConnection(databaseURL, dbProperties);
            System.out.println("success"); 
        } catch (SQLException ex) {
           //shutdown always results in an SQLException
            System.out.println(ex); 
           //This exeption is:java.sql.SQLNonTransientConnectionException: DERBY SQL error: SQLCODE: -1, SQLSTATE: 08006, SQLERRMC: Database 'databasename' shutdown.
        }
        connectionExists = false;
    }
}

My first System.out.println doesn`t print anything which means try is not performed, and I get above indicated exception.

As I know "A successful shutdown always results in an SQLException" but does it mean the try statement is not performed? Thanks for your help.

Caffeinated
  • 11,982
  • 40
  • 122
  • 216
Jamol
  • 3,768
  • 8
  • 45
  • 68

2 Answers2

3

Your try block is performed otherwise the exception would not be thrown.

Unfortunately that's the way the shutdown with Derby works.

1

The error is thrown at

DriverManager.getConnection(databaseURL, dbProperties);

That's why:

System.out.println("success");

is not executed.

System.out.println(ex); 

Is printing

java.sql.SQLNonTransientConnectionException: DERBY SQL error: SQLCODE: -1, SQLSTATE: 08006, SQLERRMC: Database 'databasename' shutdown.

so you see: the catch-block is handled.

If you add System.out.println("success"); as the first statement in the catch block it will be printed.

oers
  • 18,436
  • 13
  • 66
  • 75