Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.1 » SQL Anywhere Server - Programming » Java in the database

 

Shutdown hooks in the Java VM

The SQL Anywhere Java VM ClassLoader which is used in providing JAVA in the database support allows applications to install shutdown hooks. These shutdown hooks are very 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 will now run all shutdown hooks that have been installed for that particular connection prior to unloading. It should be noted that 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 note that 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 );

Note that 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 subsequently called via an external environment call will automatically be 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.