Table of Contents
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 test.script, test.properties, test.data and test.backup.
Database Files
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).
This file contains the SQL statements that makes up the database up to the last checkpoint - it is in synch with test.backup.
This file contains the (binary) data records for CACHED tables only.
This is compressed file that contains the complete backup of the old test.data file at the time of last checkpoint.
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.
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.
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.
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.
The database engine performs the following procedures internally in different circumstances.
Procedure C.1. Clean Hsqldb database shutdown
The test.data file is written completely (all the modified cached table rows are witten out) and closed.
The test.backup.new is created (containing the compressed test.data file)
The file test.script.new is created using the information in the database (and thus shrinks because no UPDATE and DELETE statements; only INSERT).
The entry 'modified' in the properties file is set to 'yes-new-files'
The file test.script is deleted
The file test.script.new is renamed to test.script
The file test.backup is deleted
The file test.backup.new is renamed to test.backup
The entry 'modified' in the properties file is set to 'no'
The file test.log is deleted
Procedure C.2. Database is opened
Check if the database files are in use (by checking a special test.lck file).
See if the test.properties file exists, otherwise create it.
If the test.properties did not exist, then this is a new database. Create the empty test.log to append new commands.
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.
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.
Open the test.script file and execute the commands.
Create the empty test.log to append new commands.
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
Restore the old test.data file from the backup (uncompress the test.backup and overwrite test.data).
Execute all commands in the test.script file.
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.
Close the database correctly (including a backup).