Use Connection.isClosed and IS_CLOSED_TEST

jConnect offers a default interpretation of the isClosed method that differs from the behavior defined in the JDBC 4.0 specification.

When you call Connection.isClosed, jConnect verifies that Connection.close has been called on this connection. If close has been called, jConnect returns true for isClosed. However, if Connection.close has not been called, jConnect tries to execute the sp_mda on the database. sp_mda is part of the standard metadata that jConnect users must install when they use jConnect with a database.

According to section 11.1 of the JDBC 4.0 specification:

The Connection.isClosed method is only guaranteed to return true after Connection.close has been called. Connection.isClosed cannot be called, in general, to determine whether a database connection is valid. A typical client can determine that a connection is invalid by catching the exception that is thrown when an operation is attempted.

The purpose of calling sp_mda is so that jConnect can try to execute a procedure that is known (or at least, expected) to reside on the database server. If the stored procedure executes normally, jConnect returns false for isClosed because it has verified that the database connection is valid and working. However, if the call to sp_mda results in a SQLException being thrown, jConnect catches the exception and returns true for isClosed because it appears that there is something wrong with the connection.

To force jConnect to more closely follow the standard JDBC behavior for isClosed(), set the IS_CLOSED_TEST connection property to the special value “INTERNAL.” The INTERNAL setting means that jConnect returns true for isClosed only when Connection.close has been called, or when jConnect has detected an IOException that has disabled the connection.

You can also specify a query other than sp_mda to use when isClosed is called. For example, if you intend for jConnect to attempt a select 1 when isClosed is called, set the IS_CLOSED_TEST connection property to select 1.