Keywords: HSQLDB, Data, Files

Appendix C. Hsqldb Database Files and Recovery

## Appendix C. Hsqldb Database Files and Recovery

This text is based on HypersonicSQL documentation, updated to reflect the latest version 1.8.0 of HSQLDB.

$Date: 2005/07/01 17:06:32 $

The Standalone and Client/Server modes will in most cases use files to store all data to disk in a persistent and safe way. This document describes the meaning of the files, the states and the procedures followed by the engine to recover the data.

A database named 'test' is used in this description. The database files will be as follows.

Database Files

test.properties

  Contains the entry 'modified'. If the entry 'modified' is set to 'yes' then the database is either running or was not closed correctly (because the close algorithm sets 'modified' to 'no' at the end).

test.script

  This file contains the SQL statements that makes up the database up to the last checkpoint - it is in synch with `test.backup`.

test.data

  This file contains the (binary) data records for CACHED tables only.

test.backup

  This is compressed file that contains the complete backup of the old `test.data` file at the time of last checkpoint.

test.log

  This file contains the extra SQL statements that have modified the database since the last checkpoint (something like the 'Redo-log' or 'Transaction-log', but just text).

  In the above list, a checkpoint results from both a CHECKPOINT command and a SHUTDOWN command.

## States

Database is closed correctly

State after using the command `SHUTDOWN`

- The `test.data` file is fully updated.

- The `test.backup` contains the compressed `test.data` file.

- The `test.script` contains the information in the database, excluding data for CACHED and TEXT tables.

- The `test.properties` contains the entry 'modified' set to 'no'.

- There is no `test.log` file.

Database is closed correctly with SHUTDOWN SCRIPT

State after using the command `SHUTDOWN SCRIPT`

- The `test.data` file does not exist; all CACHED table data is in the `test.script` file

- The `test.backup` does not exist.

- The `test.script` contains the information in the database, including data for CACHED and TEXT tables.

- The `test.properties` contains the entry 'modified' set to 'no'.

- There is no `test.log` file.

Database is aborted

This may happen by sudden power off, Ctrl+C in Windows, but may be simulated using the command SHUTDOWN IMMEDIATELY.

Aborted Database state

- The `test.properties` still containes 'modified=yes'.

- The `test.script` contains a snapshot of the database at the last checkpoint and is OK.

- The `test.data` file may be corrupt because the cache in memory was not written out completely.

- The `test.backup` file contains a snapshot of `test.data` that corresponds to `test.script`.

- The `test.log` file contain all information to re-do all changes since the snanapshot. As a result of abnormal termination, this file may be partially corrupt.

## Procedures

The database engine performs the following procedures internally in different circumstances.

### Clean Shutdown

Procedure C.1. Clean Hsqldb database shutdown

1. The `test.data` file is written completely (all the modified cached table rows are witten out) and closed.

2. The `test.backup.new` is created (containing the compressed `test.data` file)

3. The file `test.script.new` is created using the information in the database (and thus shrinks because no UPDATE and DELETE statements; only INSERT).

4. The entry 'modified' in the properties file is set to 'yes-new-files'

5. The file `test.script` is deleted

6. The file `test.script.new` is renamed to `test.script`

7. The file `test.backup` is deleted

8. The file `test.backup.new` is renamed to `test.backup`

9. The entry 'modified' in the properties file is set to 'no'

10. The file `test.log` is deleted

### Startup

Procedure C.2. Database is opened

1. Check if the database files are in use (by checking a special `test.lck` file).

2. See if the `test.properties` file exists, otherwise create it.

3. If the `test.properties` did not exist, then this is a new database. Create the empty `test.log` to append new commands.

4. If it is an existing database, check in the `test.properties` file if 'modified=yes'. This would mean last time it was not closed correctly, and thus the `test.data` file may be corrupted or incomplete. In this case the 'REPAIR' algorithm is executed (see below), before the database is opened normally.

5. Otherwise, if in the `test.properties` file 'modified=yes-new-files', then the (old) `test.backup` and `test.script` files are deleted and the new `test.script.new` file is renamed to `test.script`.

6. Open the `test.script` file and execute the commands.

7. Create the empty test.log to append new commands.

### Repair

The current `test.data` file is corrupt, but with the old `test.data` (from the `test.backup` file and `test.script`) and the current `test.log`, the database is made up-to-date. The database engine takes these steps:

Procedure C.3. Database Repair

1. Restore the old `test.data` file from the backup (uncompress the `test.backup` and overwrite `test.data`).

2. Execute all commands in the `test.script` file.

3. Execute all commands in the `test.log` file. If due to corruption, an exception is thrown, the rest of the lines of command in the `test.log` file are ignored.

4. Close the database correctly (including a backup).

Appendix B. First JDBC Client Example |  | Appendix D. Running Hsqldb with OpenOffice.org 1.1.x