IT212 LECTURE 6
Transactions and Concurrency Control
Mrs. Memory M Lumbi
Database Transactions
• A transaction is a sequence of one or more SQL operations (like
INSERT, UPDATE, DELETE, etc.) that are executed as a single unit
of work.
• The goal is to ensure that either all operations succeed, or none of
them take effect.
Transaction Control
• Transaction control refers to the mechanisms and commands
used to manage the changes made by SQL transactions in a
database.
• It ensures that operations within a transaction are completed
successfully or rolled back if something goes wrong, preserving
the integrity, consistency, and reliability of the database.
ACID Properties
• ACID stands for Atomicity, Consistency, Isolation, and Durability.
These properties ensure reliable processing of database
transactions.
ACID Properties
1.Atomicity
1. A transaction is treated as a single unit; it either completes in full or not at all.
2. Example: If a bank transfer fails halfway, both debit and credit operations are rolled back.
2.Consistency
1. A transaction must bring the database from one valid state to another, maintaining all
predefined rules.
2. Example: If a transaction violates a constraint, it is aborted.
3.Isolation
1. Transactions are executed independently of one another, ensuring that concurrent
transactions do not affect each other.
2. Example: Two transactions should not see intermediate states of each other.
4.Durability
1. Once a transaction is committed, its changes are permanent, even in the event of a system
failure.
2. Example: Completed transactions are saved to non-volatile storage.
Transaction States
• Active: The initial state; the transaction is being executed.
• Partially Committed: The state after the final operation has been
executed but before it is committed.
• Committed: The transaction has been successfully completed
and changes are permanent.
• Failed: The transaction has encountered an error and cannot
proceed.
• Aborted: The transaction has been rolled back to maintain
database integrity.
Transaction Schedules
• Definition: A schedule is a sequence of operations from multiple
transactions.
• Types of Schedules:
• Serial Schedule: Transactions are executed one after another without
overlapping.
• Concurrent Schedule: Transactions are executed simultaneously,
leading to potential conflicts.
Transaction Schedule
• Concurrent
• T1: Read(A)
• T2: Read(B)
• T1: Write(A)
• T2: Write(B)
• Serial
• T1: Read(A), Write(A)
• T2: Read(B), Write(B)
Concurrency Control Techniques
Concurrency control ensures that database transactions are
executed in a safe manner, maintaining the ACID properties.
1.Locking
1. Definition: A mechanism to control access to data by multiple
transactions.
2. Types of Locks:
1.Shared Lock: Allows multiple transactions to read a resource but not modify it.
2.Exclusive Lock: Allows only one transaction to modify a resource.
Example
LOCK TABLE employees IN EXCLUSIVE MODE;
Timestamping
•Definition: Each transaction is assigned a unique timestamp to
determine the order of execution.
•Mechanism: Older transactions are given priority over newer
ones to maintain consistency.
•Example:
•If Transaction T1 has a timestamp of 1 and T2 has 2, T1 will
be executed first.
Timestamp
Assume we have the following variables:
-- @account_id: the ID of the account to update
-- @new_balance: the new balance to set
-- @transaction_timestamp: the timestamp of the current transaction
UPDATE accounts SET balance = @new_balance, last_updated = @transaction_timestamp
WHERE account_id = @account_id AND @transaction_timestamp > last_updated;
Explanation
UPDATE accounts: This statement updates the accounts table.
SET balance = @new_balance: Sets the new balance for the specified account.
WHERE account_id = @account_id: Specifies which account to update.
AND @transaction_timestamp > last_updated:
Ensures that the update only occurs
if the transaction's timestamp is greater than the last updated timestamp in the database.
Are We Here?
Questions
comments