Restoring Java DB Database from a Backup Copy
Apr 27, 2013
This blog post is about restoring Java DB database from a backup copy.
1. Java DB
Java DB is a relational database management system that is based on the Java programming language and SQL. This is the Oracle release of the Apache Software Foundation's open source Derby project. Java DB is included in the Java SE 7 SDK.
Java DB provides a way to backup a database and restore it from the backup copy.
A Java DB database is stored in files within a directory with the same name as that of the database. The backup database is stored at a specified location. The database can be restored from the backup copy at a specified location.
2. Restoring the Database
Restoring a database deletes the existing one, copies the backup copy and starts the restored (copied) database. In case there is no existing database, a new database is created and the backup is copied.
Restore process steps:- Shutdown the current database. This is a must.
- Get the directory path of the backup copy.
- Build the URL (a database URL of the form
jdbc:subprotocol:subname
); this is used to get a connection to the database. The path and the database name are required to build the URL. TherestoreFrom
arttribute value in the URL specifies the path and the database name. - Load the database driver (Server or Embedded; depending upon the deployment scenario).
- Open the connection using the URL.
- Use the connection object to access the database objects and the data.
The following is an example Java method to restore a database from a backup copy using JDBC:
/*
* Starts database using a backup copy. The returned Connection object
* is used to access the restored database objects and data.
* Note:
* (1) The existing database is shutdown prior to running this method.
* (2) The connection URL uses the 'restoreFrom' attribute.
* (3) The database driver used is for a Java DB database deployed with
* embedded option.
*/
public static Connection restoreDatabaseRoutine()
throws SQLException, ClassNotFoundException {
String backupPath = "D:/BackupDir/sampleDb";
String restoreUrl = "jdbc:derby:sampleDb;restoreFrom=" + backupPath;
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver); // throws ClassNotFoundException
Connection conn = DriverManager.getConnection(restoreUrl);
return conn;
}
2.1. createFrom Attribute in URL
createFrom
attribute can be used instead of the restoreFrom
attribute to restore a database. In case a database with the same name exists, the restore process is aborted with an error. The database is created and restored when there is no existing database.
2.2. Restoring Database Interactively with 'ij' Tool
ij is a command line tool included with Java DB. ij is a JDBC tool used to run interactive queries on a Java DB database.
Start ij, connect to the database with the restored database:
os prompt> ij
ij> CONNECT 'jdbc:derby:sampleDb;restoreFrom=D:/BackupDir/sampleDb';
'jdbc:derby:sampleDb;restoreFrom=D:/BackupDir/sampleDb' is the connection URL. The value of restoreFrom
attribute is the directory path to the backup copy of the sampleDb
database. Note that the database must be closed prior to restoring it.
3. Example Usage
An example usage is a Swing based desktop application that uses a database. The application has a function to restore database from a backup copy. The following is an outline of the function:
- Select a database menu option to open a dialog for the restore function.
- Open a file browser, select the directory and get the backup database path string - the directory of a backup copy of the database.
- Initiate the restore process. Display progress, complete/failure status of the process and a message for further action.
This database menu can also include an option to make an online backup.
4. References
- Apache Derby > Documentation (10.8 Manuals > Derby Reference Manual).
- Oracle's Java DB.
- Java DB Embedded Mode - article.