The fastest way I know about:
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 recovered in ~ 150 ms on my dev machine this way.
This is really useful for testing junit integration where fast, fast db recovery is required. Depending on your installation, you may need to do additional additional work between tests, for example, if you use the c3p0 connection pool, you need to get all the connections from C3P0Registry and call hardReset () after this recovery. On the other hand, nothing needs to be done with some other pools.
source share