Shutdown Hooks in the Java VM

The SAP Sybase IQ Java VM ClassLoader which is used in providing JAVA in the database support allows applications to install shutdown hooks. These shutdown hooks are similar to the shutdown hooks that applications install with the JVM Runtime.

When a connection that is using JAVA in the database support executes a STOP JAVA statement or disconnects, the ClassLoader for that connection runs all shutdown hooks that have been installed for that particular connection prior to unloading. For regular JAVA in the database applications that install all Java classes within the database, the installation of shutdown hooks should not be necessary. The ClassLoader shutdown hooks should be used with extreme caution and should only be used to clean up any system-wide resources that were allocated for the particular connection that is stopping Java. Also, jdbc:default JDBC requests are not allowed within shutdown hooks since the jdbc:default connection is already closed prior to the ClassLoader shutdown hook being called.

To install a shutdown hook with the SQL Anywhere Java VM ClassLoader, an application must include sajvm.jar in the Java compiler classpath and it needs to execute code similar to the following:

SDHookThread hook = new SDHookThread( ... );
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
((ianywhere.sa.jvm.SAClassLoader)classLoader).addShutdownHook( hook );

The SDHookThread class extends the standard Thread class and that the above code must be executed by a class that was loaded by the ClassLoader for the current connection. Any class that is installed within the database and that is later called via an external environment call is automatically executed by the correct SQL Anywhere Java VM ClassLoader.

To remove a shutdown hook from the SQL Anywhere Java VM ClassLoader list, an application will need to execute code similar to the following:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
((ianywhere.sa.jvm.SAClassLoader)classLoader).removeShutdownHook( hook );

The above code must be executed by a class that was loaded by the ClassLoader for the current connection.