Using Connection.isClosed and IS_CLOSED_TEST

According to section 11.1 of the JDBC 2.1 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 if a database connection is valid or invalid. A typical client can determine that a connection is invalid by catching the exception that is thrown when an operation is attempted.”

jConnect offers a default interpretation of the isClosed method that is different from the behavior that is defined in the spec. When you call Connection.isClosed, jConnect first 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 next tries to execute the sp_mda stored procedure on the database. The sp_mda stored procedure is part of the standard metadata that jConnect users must install when they use jConnect with a database.

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, then jConnect returns false for isClosed because we have 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.

If you intend to force jConnect to more closely follow the standard JDBC behavior for isClosed(), you can do so by setting 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, you can set the IS_CLOSED_TEST connection property to select 1.