- The log is a sequence of records. Log of each transaction is maintained in some stable storage so that if any failure occurs, then it can be recovered from there.
- If any operation is performed on the database, then it will be recorded in the log.
- But the process of storing the logs should be done before the actual transaction is applied in the database.
Let's assume there is a transaction to modify the City of a student. The following logs are written for this transaction.
- When the transaction is initiated, then it writes 'start' log.
- When the transaction modifies the City from 'Noida' to 'Bangalore', then another log is written to the file.
- When the transaction is finished, then it writes another log to indicate the end of the transaction.
There are two approaches to modify the database:
1. Deferred database modification:
- The deferred modification technique occurs if the transaction does not modify the database until it has committed.
- In this method, all the logs are created and stored in the stable storage, and the database is updated when a transaction commits.
2. Immediate database modification:
- The Immediate modification technique occurs if database modification occurs while the transaction is still active.
- In this technique, the database is modified immediately after every operation. It follows an actual database modification.
Recovery using Log records
When the system is crashed, then the system consults the log to find which transactions need to be undone and which need to be redone.
- If the log contains the record and or , then the Transaction Ti needs to be redone.
- If log contains record<>n, Start> but does not contain the record either or , then the Transaction Ti needs to be undone.