### Locks and Locking Rules for Nested Transactions (Simplified)
What is a Lock?
A lock is like a "do not disturb" sign that a transaction places on data it is using. This ensures no other
transaction can change or use that data until the lock is removed. This helps to keep the data
consistent and prevents errors.
### Locking Rules for Nested Transactions
Nested transactions are like transactions within transactions. Here’s how locking works for them:
#### Goals of Locking
1. **Isolation from Other Sets**: Nested transactions should not see the unfinished work of other
nested transactions.
2. **Isolation within the Set**: Transactions within the same set should not see each other's
unfinished work.
#### How Locks Work in Nested Transactions
1. **Passing Locks Upwards**:
- When a child transaction finishes successfully, it passes its locks to its parent transaction.
- This passing up continues, so the top-level (main) transaction eventually holds all the locks
acquired by any of its nested transactions.
- This ensures that other nested transactions sets don’t interfere with each other.
2. **Parent and Child Rules**:
- A parent transaction does not run while its child transactions are running.
- If a parent has a lock, it keeps it while the child is running. The child uses the lock temporarily.
3. **Concurrent Child Transactions**:
- Child transactions at the same level can run at the same time.
- When they need the same data, they take turns using locks to keep things orderly.
#### Rules for Getting and Releasing Locks
1. **Getting a Read Lock**:
- A child transaction can read data if no other transaction is writing to it.
- Only its parent or higher-level transactions can be writing to that data.
2. **Getting a Write Lock**:
- A child transaction can write data if no other transaction is reading or writing it.
- Only its parent or higher-level transactions can have locks on that data.
3. **When a Child Transaction Finishes**:
- Its locks are passed to its parent.
- The parent keeps these locks in the same way (read or write) as the child did.
4. **When a Child Transaction Fails**:
- Its locks are released.
- If the parent already has these locks, it can keep them.
5. **Taking Turns**:
- Child transactions at the same level take turns using the locks held by their parent.
- This ensures they don’t interfere with each other’s work.
### Summary
In nested transactions, locks are used to control access to shared data, ensuring consistency and
preventing conflicts. When a child transaction finishes, it passes its locks to its parent. Parent
transactions don’t run while their children are running. Child transactions at the same level take
turns accessing data. This careful management of locks ensures that transactions do not interfere
with each other’s operations, maintaining the integrity of the data.