chapter 3 transaction processing concepts
chapter 3 transaction processing concepts
1. ACID Properties
ACID stands for Atomicity, Consistency, Isolation, and Durability. These properties ensure that
transactions are processed reliably.
Atomicity: Ensures that all operations within a transaction are completed. If any
operation fails, the entire transaction fails, and the database state is unchanged.
o Example: If a bank transfer involves deducting money from one account and
adding it to another, if the deduction succeeds but the addition fails, both
operations must roll back to ensure money is not lost.
Consistency: Ensures that a transaction brings the database from one valid state to
another without violating any integrity constraints.
o Example: If a transaction updates a student’s grade in a grading system, the grade
must be consistent with the overall grading policy (like not allowing scores to
exceed a certain maximum).
Isolation: Ensures that transactions are executed in isolation from one another.
Concurrent transactions should not interfere with each other.
o Example: If two transactions are trying to update the account balance of the same
bank account simultaneously, isolation ensures that one transaction completes
before the other starts, preventing incorrect balances from being reflected.
Durability: Ensures that once a transaction has been committed, it will remain persistent,
even in the event of a system failure.
o Example: When a transaction that updates the inventory count is committed, that
change will not be lost even if the system crashes immediately afterward.
2. Transaction States
Concurrency control mechanisms ensure that multiple transactions can occur in a system without
leading to inconsistencies, using techniques such as:
Locking: Transactions acquire locks on data resources to prevent other transactions from
modifying them while they are being processed.
o Example: A transaction that is updating customer information locks the customer
record to prevent other transactions from reading or writing to it until the update
is complete.
Timestamping: Assigns a unique timestamp to each transaction to determine the order of
execution.
o Example: In a timestamp-waiting protocol, if a transaction attempts to access an
item held by a later transaction, it will wait until that transaction is completed.
4. Types of Transactions
Schedules
Schedule S1:
T1: R(A)
T1: W(A)
T2: R(B)
T2: W(B)
Concurrent Schedule: A schedule where the operations of two or more transactions are
interleaved. This can improve system performance but can also introduce complexity and
potential inconsistencies.
Example:
2.
o Schedule S2:
o T1: R(A)
o T2: R(B)
o T1: W(A)
o T2: W(B)
o
Recoverability
Recoverability refers to the ability of the database system to return to a consistent state after a
transaction failure, such as a crash or an error. A schedule is considered recoverable if,
whenever a transaction T1 reads a value written by another transaction T2, T1 commits only
after T2 commits. This ensures that the result of T1 can be safely used and that T2's changes are
durable.
1. Strict: A strict schedule ensures that no transaction can read or write to a data item until
the previous transaction that wrote that item has committed. This is a very strong form of
consistency.
o Example:
Schedule S3 (Strict):
T1: W(A)
T1: C
T2: R(A)
T2: W(B)
T2: C
Example:
Schedule S4 (Recoverable):
T1: W(A)
T2: R(A)
T1: C
T2: C
Cascadeless: A cascadeless schedule is one where transactions can only read data written by
committed transactions. This guarantees that a failure of one transaction does not lead to
cascading rollbacks.
Example:
3.
o Schedule S5 (Cascadeless):
o T1: W(A)
o T1: C
o T2: R(A)
o T2: C
o
Schedule S6 (Non-Recoverable):
T1: W(A) (T1 writes A)
T2: R(A) (T2 reads A)
T1: C (T1 commits)
T2: W(A) (T2 writes A)
T1: R(B) (T1 tries to read B)
Serializability of Schedules
Serializability is a key concept in database management systems that ensures that the concurrent
execution of transactions yields the same results as if the transactions were executed serially (one
after the other, without overlapping in time). There are two main types of serializability: conflict
serializability and view serializability.
1. Conflict Serializability
Two schedules are conflict equivalent if they can be transformed into one another by swapping
non-conflicting operations. A schedule is conflict serializable if it is conflict-equivalent to a
serial schedule.
Example:
T1:
o Read(A)
o Write(A)
T2:
o Read(A)
o Write(A)
Schedule S1:
2. View Serializability
Example:
T1:
o Read(X)
o Write(X)
T2:
o Read(X)
o Write(X)
Schedule S3:
In this schedule:
However, it is important to note that view serializability is generally more complex to check than
conflict serializability.
Summary
Conflict Serializability: Focuses on the order of conflicting operations and ensures that
they can be reordered into a way that is consistent with a serial execution.
View Serializability: Ensures that a transaction sees the same data it would in a serial
execution and that the final state matches a serial execution's outcome.
Both types are essential for ensuring consistency in concurrent transaction processing in
databases.
Transaction support in SQL is essential for maintaining data integrity and ensuring that database
operations are executed reliably. A transaction is a sequence of operations performed as a single
logical unit of work. A transaction is generally characterized by four properties, often referred to
as ACID properties:
1. Atomicity: Ensures that all operations within a transaction are completed successfully or none
are.
2. Consistency: Ensures that a transaction brings the database from one valid state to another
valid state.
3. Isolation: Ensures that the operations in a transaction are isolated from other operations
happening concurrently.
4. Durability: Ensures that once a transaction is committed, it will remain so, even in the event of a
system failure.
Example
Let’s look at a practical example involving a banking application. We will transfer money from
one account to another, demonstrating the use of transactions.
Sample Data
Now, let’s create a stored procedure that transfers money from Alice to Bob. The transaction will
ensure both the debit from Alice's account and the credit to Bob's account are completed
successfully or rolled back if there is an issue.
DELIMITER //
-- Start a transaction
START TRANSACTION;
DELIMITER ;
To transfer, for instance, $100 from Alice to Bob, you would execute:
Explanation