UNIT 3- Transaction
UNIT 3- Transaction
Atomicity:
By this, we mean that either the entire transaction takes place at
once or doesn’t happen at all. There is no midway i.e. transactions do
not occur partially. Each transaction is considered as one unit and
either runs to completion or is not executed at all. It involves the
following two operations.
Abort: If a transaction aborts, changes made to the database are not
visible.
Commit: If a transaction commits, changes made are visible.
Atomicity is also known as the ‘All or nothing rule’.
Example: If a transaction involves transferring money from Account A
to Account B, both the debit from Account A and the credit to
Account B must succeed. If one operation fails, neither operation
should take effect.
Consistency:
This means that integrity constraints must be maintained so that the
database is consistent before and after the transaction. It refers to
the correctness of a database. This means that integrity constraints
must be maintained so that the database is consistent before and
after the transaction. It refers to the correctness of a database.
Example: A transaction should not leave the database in an
inconsistent state, where business rules or integrity constraints are
violated.
Isolation:
ensures that the operations of one transaction are not visible to
other transactions until the transaction is complete. Even though
transactions may execute concurrently, the intermediate states of a
transaction are not seen by others, preventing issues like "dirty
reads," "non-repeatable reads," and "phantom reads."
Example: If two users are concurrently accessing the same data, the
database ensures that one user's transaction does not interfere with
the other's, even if both are working with the same records.
Durability
Durability ensures the permanency of something. It ensures that the
data after the successful execution of the operation becomes
permanent in the database. The durability of the data should be so
perfect that even if the system fails or leads to a crash, the database
still survives. The database must ensure that the results of a
committed transaction are not lost.
Example: After a bank transfer transaction is committed, the changes
(like the updated balance in both accounts) will survive system
crashes and will be recoverable when the system is restored.
Transaction States
A transaction passes through several states during its lifetime:
1. Active: The transaction is being executed, and operations are
being performed.
2. Partially Committed: The transaction has completed its final
operation but has not yet been committed to the database.
3. Committed: The transaction has successfully completed all its
operations and changes are permanently saved to the
database.
4. Failed: The transaction could not complete due to some error,
and it will need to be rolled back.
5. Rolled Back: In case of a failure, the transaction is undone, and
the database is restored to its state before the transaction
began.
Examples of Transactions
1. Bank Transfer:
o Suppose a customer wants to transfer money from their
savings account to their checking account. The transaction
involves two main steps:
Debit the savings account.
Credit the checking account.
o If either of these operations fails (e.g., due to insufficient
funds or a system crash), the entire transaction must be
rolled back to avoid inconsistent states.
2. Online Shopping:
o In an online shopping system, a transaction may involve
selecting items, applying discounts, updating inventory,
and charging the customer. If any step fails (e.g., payment
failure), the entire transaction should fail, and no partial
changes (like reduced stock) should be saved.
Concurrency Control
Concurrency control in a DBMS refers to the mechanisms used to
ensure that multiple transactions can be executed concurrently
(multiple users can access and use the same database at one time,
which is known as the concurrent execution) without violating the
ACID properties. Specifically, concurrency control primarily focuses
on the Isolation property, which ensures that the operations of one
transaction are isolated from those of other concurrent transactions.
This is essential in multi-user environments where many users’
accesses and modify the database simultaneously.
The thing is that the simultaneous execution that is performed
should be done in an interleaved manner, and no operation should
affect the other executing operations, thus maintaining the
consistency of database.
Concurrency Control Problems
In a database transaction, the two main operations
are READ and WRITE operations. So, there is a need to manage these
two operations in the concurrent execution if these operations are
not performed in an interleaved manner, and the data may become
inconsistent.
1 Lost Update:
Occurs when two transactions read the same data and update it
based on the read value. One of the updates is lost when the
other transaction overwrites it.
Example: Two bank customers both try to withdraw money
from the same account. One transaction overwrites the other’s
update, leading to a lost transaction.
2 Dirty Read:
A transaction reads data that has been modified by another
transaction but has not yet been committed. If the second
transaction is rolled back, the first transaction has read invalid
data.
Example: A transaction reads a price that is being updated, but
before the update is committed, the transaction reads the
uncommitted value. If the update is rolled back, the transaction
has used incorrect data.
3 Non-Repeatable Read:
A transaction reads the same data twice but gets different
results due to another transaction modifying the data between
the reads.
Example: A transaction reads a customer’s account balance, but
before it reads it again, another transaction updates the
balance. The two reads yield different results, causing
inconsistency.
Types of Locks:
1. Shared lock:
o It is also known as a Read-only lock. In a shared lock, the data
item can only read by the transaction.
o It can be shared between the transactions because when the
transaction holds a lock, then it can't update the data on the
data item.
Example: If two transactions T1 and T2 both want to read the
same data item, both can acquire a shared lock and read the data.
However, no transaction can modify the data while it holds a
shared lock.
2. Exclusive lock:
o In the exclusive lock, the data item can be both reads as well as
written by the transaction.
o This lock is exclusive, and in this lock, multiple transactions do
not modify the same data simultaneously, ensuring that no
other transaction can read or modify it.
Example: If transaction T1 has an exclusive lock on a data item,
transaction T2 cannot read or modify that data item until T1
releases the lock.
Transaction T1:
o Growing phase: from step 1-3
o Shrinking phase: from step 5-7
o Lock point: at 3
Transaction T2:
o Growing phase: from step 2-6
o Shrinking phase: from step 8-9
o Lock point: at 6
Lock Point: The Point at which the growing phase ends, i.e.,
when a transaction takes the final lock, it needs to carry on its
work.