DB Presentation Final
DB Presentation Final
CONCURRENCY
&
Resource Locking
Presented by: Huriza, Saifa, Anmol
Presented to:
Dr. Muhammad Saleem Kalru
TRANSACTION
A transaction is a logical unit of work in a database, consisting of one or more
operations that must be completed together to keep the database consistent. It must
either fully complete or entirely fail.
Selling an Item:
In a database, stock information includes a sold list and item quantity.
When an item is sold, it must be added to the sold list and its quantity
subtracted from stock. Both steps need to be completed together to
avoid errors. If either step fails, the transaction entirely fails.
2
Operations of transaction:
Two main operations of Transaction:
Read:
Accesses data from the database without changing it. This operation allows you to
view or use existing information.
Write:
Updates data in the database. This operation changes the data, records new
information. ensuring that the database reflects these updates.
3
Properties of Transaction (ACID properties):
o Atomicity:
Ensures that a transaction is fully completed or not done at all. It's an "all or nothing" rule.
o Consistency:
Consistency guarantees that changes made within a transaction are consistent with
database constrains.
o Isolation:
It insures that multiple transactions access the same data without interfering with each other .
o Durability:
Once a transaction is successfully completed, its changes are permanent and saved even if there is a
system failure.
4
CONCURRENCY:
Multiple transactions happening in a database at the same time.
Concurrency is a situation where two or more users access the same piece
of data at the same time. This is common in multi-user environments.
However, concurrent access can sometimes lead to serious problems, such
as data inconsistencies or conflicts.
6
Concurrency
Problems:
1. Lost Update Problem(W-W PROBLEM):
- Occurs when multiple transactions update the same data simultaneously, and the last update overwrites the
others, resulting in data loss.
Example: T1 T2
A=100
R(A)
A=A-50
=50
R(A)
A=A+100
=200
W(A)
W(A)
7
T1 T2
Example:
A=10 R(A)
0 A=A+50
W(A)
R(A)
ROLLBACK
FAIL
W(A)
R(A)
A=200
Resource Locking:
Example:
- User 1: Locks the item, retrieves it, reduces the value by 10,
updates
it, and unlocks it.
9
Lock
Terminology:
Implicit Lock: Automatically placed by the DBMS.
Explicit Lock: Placed by an application command.
Exclusive Lock: Prevents any access; no other user can read or
change the data.
Shared Lock: Allows reading but prevents changes.
Lock Granularity: The level at which a lock is applied (e.g.,
database, table, row.
10
Thank You