Shutting down the system

In an embedded environment, when an application shuts down, it should first shut down Derby.

If the application that started the embedded Derby quits but leaves the Java Virtual Machine (JVM) running, Derby continues to run and is available for database connections.

In an embedded system, the application shuts down the Derby system by issuing the following JDBC call:

DriverManager.getConnection("jdbc:derby:;shutdown=true");

Shutdown commands always raise SQLExceptions.

When a Derby system shuts down, a message goes to the log file:

----------------------------------------------------------------
Thu Nov 04 16:32:41 EDT 2010: Shutting down Derby engine
----------------------------------------------------------------
Thu Nov 04 16:32:41 EDT 2010:
Shutting down instance a816c00e-012c-1896-832c-00002ded7282 on 
database directory C:\sampledb with class loader 
sun.misc.Launcher$AppClassLoader@7d772e 
----------------------------------------------------------------

Typically, an application using an embedded Derby engine shuts down Derby just before shutting itself down. However, an application can shut down Derby and later restart it in the same JVM session. To restart Derby successfully, the application needs to reload org.apache.derby.jdbc.EmbeddedDriver as follows:

Class.forName(org.apache.derby.jdbc.EmbeddedDriver).newInstance();

Loading the embedded driver starts Derby.

The JDBC specification does not recommend calling newInstance(), but adding a newInstance() call guarantees that Derby will be booted on any JVM.

It is also possible to shut down a single database instead of the entire Derby system. See Shutting down Derby or an individual database. You can reboot a database in the same Derby session after shutting it down.

Related concepts
Booting databases