Transaction Management
Transaction Management
What is a transaction
• A database transaction is a logical unit that generally
includes several low-level steps. If one step of the
transaction fails, the whole transaction fails. A database
transaction is used to create, update, or retrieve data.
• Think of a database transaction as a series of
operations performed within a DBMS. The transaction
system ensures that the data in the database always
remains in a reliable and consistent state.
• Commit is an operation that makes all changes
made during the current transaction
permanent. Once a commit operation is
executed, the changes are saved to the
database, and the transaction is considered
complete.
• Rollback is an operation that undoes all
changes made during the current
transaction. If an error occurs or if certain
conditions are not met, a rollback can be
issued to revert the database to its
previous state before the transaction
began.
Properties of Transactions
Initial State:
Account 1: $1000
Account 2: $2000
Total Assets: $3000
Transactions:
Transaction A: Reads account balances to calculate
the total assets.
Transaction B: Updates the account balance of
Account 2.
1. Step 1 (T1):
1. Transaction A starts and reads the balance of Account 1: $1000.
2. The current balances are $1000 for Account 1 and $2000 for Account 2.
2. Step 2 (T2):
1. Transaction B starts and updates the balance of Account 2 from $2000 to
$2500.
2. Now, the balances are $1000 for Account 1 and $2500 for Account 2.
3. Step 3 (T3):
1. Transaction A continues and reads the balance of Account 2: $2500.
2. It sees the updated balance for Account 2, which was modified by Transaction
B.
4. Step 4 (T4):
1. Transaction A calculates the total assets as $1000 (Account 1) + $2500
(Account 2) = $3500.
2. This is inconsistent because the actual total assets should have been
calculated based on a consistent snapshot of $1000 + $2000 = $3000 before
Transaction B's update.
• The problem here is that Transaction A's read
operations are affected by the committed
changes of Transaction B, leading to a
situation where Transaction A's analysis of the
total assets is based on partially inconsistent
data.
Solution: Using Proper Isolation Levels
Deadlock Detection
• Databases use various techniques to detect
deadlocks. Once a deadlock is detected, the
system typically chooses one of the
transactions to roll back and releases its locks
to break the cycle.
Detecting and Resolving Deadlocks
Deadlock Prevention
• To prevent deadlocks, databases can use several strategies:
1. Timeouts:
1. Set a timeout period for a transaction to wait for a lock. If the lock is not
acquired within the timeout period, the transaction is rolled back.
2. Lock Ordering:
1. Ensure that all transactions acquire locks in a predetermined order to
avoid circular wait conditions.
3. Deadlock Detection and Resolution:
1. Periodically check for deadlocks in the system and resolve them by
rolling back one of the transactions involved.
4. Avoiding Long Transactions:
1. Keep transactions short and release locks as soon as possible to
reduce the chance of deadlocks.
Detecting and Resolving Deadlocks
Deadlock Prevention
• To prevent deadlocks, databases can use several strategies:
1. Timeouts:
1. Set a timeout period for a transaction to wait for a lock. If the lock is not
acquired within the timeout period, the transaction is rolled back.
2. Lock Ordering:
1. Ensure that all transactions acquire locks in a predetermined order to
avoid circular wait conditions.
3. Deadlock Detection and Resolution:
1. Periodically check for deadlocks in the system and resolve them by
rolling back one of the transactions involved.
4. Avoiding Long Transactions:
1. Keep transactions short and release locks as soon as possible to
reduce the chance of deadlocks.
Database failures
• Database failures can occur due to various
reasons, which can broadly be categorized
into hardware failures, software failures,
human errors, and external factors. Here are
some common reasons for database failures:
Database failures
• Hardware Failures:
– Disk Crashes: Physical damage to storage devices
can lead to data loss.
– Power Failures: Unexpected power outages can
cause data corruption if transactions are not
properly committed.
– Memory Failures: Issues with the system memory
can corrupt in-memory data.
Database failures
• Software Failures:
– Bugs in DBMS: Software bugs or vulnerabilities in
the database management system can cause
failures.
– Operating System Crashes: OS-level crashes can
interrupt database operations.
– Application Errors: Faulty application logic
interacting with the database can lead to data
inconsistencies.
Database failures
• Human Errors:
– Accidental Deletion: Unintended deletion of data
or databases.
– Configuration Errors: Incorrect configuration
settings can lead to database failures.
– Unauthorized Access: Malicious activities by
unauthorized users can cause data loss or
corruption.
Database failures
• External Factors:
– Natural Disasters: Events such as earthquakes,
floods, or fires can physically damage data centers.
– Cyber Attacks: Hacking, viruses, and ransomware
can corrupt or delete data.
Database Recovery Ways
• Recovering from a database failure involves
various strategies and techniques to ensure
data integrity and availability. Here are some
common database recovery methods:
Database Recovery Ways
• Backup and Restore:
• Regular backups of the database are taken,
and in the event of a failure, the database can
be restored to the last known good state using
these backups.
Database backup Types: