恢复Apache derby内存数据库

时间:2012-08-08 17:05:29

标签: database derby database-backups database-restore in-memory-database

我使用以下代码创建了内存数据库的备份:

public static void backUpDatabase(Connection conn)throws SQLException
{
String backupdirectory ="c:/mybackups/"+JCalendar.getToday();
CallableStatement cs = conn.prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)"); 
cs.setString(1, backupdirectory);
cs.execute(); 
cs.close();
System.out.println("backed up database to "+backupdirectory);
}

现在我想通过从先前备份创建内存数据库来恢复数据库。我该怎么办?

感谢。

2 个答案:

答案 0 :(得分:3)

据我所知,正常的从备份恢复机制可以很好地恢复内存数据库。因此,请在连接网址上使用restoreFrom子句,如下所述:http://db.apache.org/derby/docs/10.9/adminguide/tadminhubbkup44.html

答案 1 :(得分:3)

我所知道的最快方式:

首先,使用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")
    }

然后restoreFrom=

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

包含~300个表的数据库,其中一些样本数据在我的开发机器上以这种方式在~150ms内恢复。

这对于需要快速快速数据库恢复的junit集成测试非常有用。根据您的设置,您可能需要在两次测试之间执行一些额外的工作,例如,如果使用c3p0连接池,则需要从C3P0Registry获取所有连接并在此恢复后调用hardReset()。另一方面,其他一些池也不需要做任何事情。