Closing the connection to a data source in an SQLJ application

When you have finished with a connection to a data source, you need to close the connection to the data source. Doing so releases the data server resources and SQLJ resources for the associated ConnectionContext object immediately.

About this task

If you do not close a ConnectionContext object after you use it, unexpected behavior might occur if a Java™ finalizer closes the ConnectionContext object. Examples of the unexpected behavior are:
  • An ObjectClosedException on the underlying ResultSet or Statement objects
  • Agent hangs in Db2® stored procedures

Procedure

To close the connection to the data source, use one of the ConnectionContext.close methods.
  • If you execute ConnectionContext.close() or ConnectionContext.close(ConnectionContext.CLOSE_CONNECTION), the connection context, as well as the connection to the data source, are closed.
  • If you execute ConnectionContext.close(ConnectionContext.KEEP_CONNECTION) the connection context is closed, but the connection to the data source is not.

Example

The following code closes the connection context, but does not close the connection to the data source.
…
ctx = new EzSqljctx(con0);          // Create a connection context object
                                    // from JDBC connection con0
…                                   // Perform various SQL operations 
 EzSqljctx.close(ConnectionContext.KEEP_CONNECTION);
                                    // Close the connection context but keep
                                    // the connection to the data source open