Restore Apache derby In-memory database Restore Apache derby In-memory database database database

Restore Apache derby In-memory database


The fastest way I am aware of:

First, use shutdown=true:

boolean gotSQLExc = false;    try {        DriverManager.getConnection("jdbc:derby:memory:testdb;shutdown=true");    } catch (SQLException se) {        if ( se.getSQLState().equals("08006") )            System.out.println("Database shut down normally");        else            System.out.println("Database did not shut down normally")    }

Then restoreFrom=

    Connection connection = DriverManager.getConnection("jdbc:derby:memory:testdb;restoreFrom=path/to/backup/file");    connection.close();

A db containing ~300 tables with some sample data restored in ~150ms on my dev machine this way.

This is really useful for junit integration testing where fast fast db restore is needed. Depending on your setup, you may need to perform some additional work in-between tests, for example if using c3p0 connection pool, you need to get all connections from C3P0Registry and call hardReset() after this restore. On the other hand, nothing needs to be done with some other pools.


As far as I know, the normal restore-from-backup mechanisms will work just fine to restore an in-memory database. So use the restoreFrom clause on your connection URL, as described here: http://db.apache.org/derby/docs/10.9/adminguide/tadminhubbkup44.html