Unit 02
Unit 02
Unit-2
Transaction Management and Concurrency Control
❖ What is a Transaction?
Transaction Properties
❖ Concurrency Control
Lost Updates
Uncommitted Data
Inconsistent Retrievals
Transaction Properties
- Each individual transaction must display atomicity, consistency, isolation, and durability.
These properties are sometimes referred to as the ACID test.
- In addition, when executing multiple transactions, the DBMS must schedule the parallel
execution of the transaction’s operations. The schedule of such transaction’s operations
must show the property of serializability.
• Atomicity
- It requires that all operations/SQL requests of a transaction be either completed or
aborted.
• Consistency
- Consistency indicates the stability of the database’s consistent state.
- A transaction takes a database from one consistent state to another consistent state. When
a transaction is completed, the database must be in a consistent state.
- If any of the transaction parts violates an integrity constraint, the entire transaction is
aborted.
• Isolation
- Isolation means that the data used during the execution of a transaction cannot be used by
a second transaction until the first one is completed.
• Durability
- Durability ensures that once transaction changes are done (committed), they cannot be
undone or lost, even in the event of a system failure.
• Serializability
- Serializability ensures that the schedule for the concurrent execution of the transactions
produces consistent results.
- This property is important in multiuser and distributed databases, where multiple
transactions are likely to be executed concurrently.
Example
Consider this normal execution of two transactions T1 and T2.
T1: Purchase 100 units and add the PROD_QOH (Product Quantity on Hand)
T2: Sell 30 units and subtract from PROD_QOH (Product Quantity on Hand)
Time Transaction Step Stored Value
1 T1 Read PROD_QOH 35
2 T1 PROD_QOH = 35+100
3 T1 Write PROD_QPH 135
4 T2 Read PROD_QOH 135
5 T2 PROD_QOH = 135-30
6 T2 Write PROD_QOH 105
- The first transaction (T1) has not yet been committed when the second transaction (T2) is
executed.
- Therefore, T2 still operates on the value 35, and its subtraction yields 5 in memory.
- In the meantime, T1 writes the value 135 to disk, which is promptly overwritten by T2.
- In short, the addition of 100 units is “lost” during the process.
Time Transaction Step Stored Value
1 T1 Read PROD_QOH 35
2 T2 Read PROD_QOH 35
3 T1 PROD_QOH = 35 + 100
4 T2 PROD_QOH = 35 - 30
5 T1 Write PROD_QOH (Lost Update) 135
6 T2 Write PROD_QOH 5
Uncommitted Data
- The phenomenon of uncommitted data occurs when two transactions, T1 and T2, are
executed parallel and the first transaction (T1) is rolled back after the second transaction
- Under normal circumstances, the serial execution of those transactions produces the
correct answer.
Time Transaction Step Stored Value
1 T1 Read PROD_QOH 35
2 T1 PROD_QOH = 35+100
3 T1 Write PROD_QPH 135
4 T1 ROLLBACK 35
5 T2 Read PROD_QOH 35
6 T2 PROD_QOH = 35-30
7 T2 Write PROD_QOH 5
- Below table shows how the uncommitted data problem can arise when the ROLLBACK
is completed after T2 has begun its execution.
Time Transaction Step Stored Value
1 T1 Read PROD_QOH 35
2 T1 PROD_QOH = 35+100
3 T1 Write PROD_QPH 135
4 T2 Read PROD_QOH 135
(Read Uncommitted Data)
5 T2 PROD_QOH = 135-30
6 T1 ROLLBACK 35
7 T2 Write PROD_QOH 105
Inconsistent Retrievals
- Inconsistent retrievals occur when a transaction accesses data before and after another
transaction(s) finish working with such data.
- For example, an inconsistent retrieval would occur if transaction T1 calculated some
summary (aggregate) function over a set of data while another transaction (T2) was
updating the same data.
- The problem is that the transaction might read some data before they are changed and
other data after they are changed, thereby producing inconsistent results.
Example
1. T1 calculates the total quantity on hand of the products stored in the PRODUCT table.
2. At the same time, T2 updates the quantity on hand (PROD_QOH) for two of the
PRODUCT table’s products.
Sub: Database Management System – II (BCA-Sem-4) Unit-2 Page 4
By: Chirag SIR
SELFSTUDYPRO
PROD_CODE PROD_QOH PROD_QOH
Before After
P1 100 100
P2 120 120
P3 70 70+10 = 80
P4 35 35-10 = 25
P5 100 100
P6 30 30
Lock Manager
- Most multiuser DBMSs automatically initiate and enforce locking procedures.
- All lock information is managed by a lock manager, which is responsible for assigning
and policing the locks used by the transactions.
Lock Granularity
- Lock granularity indicates the level of lock use.
- Locking can take place at the following levels: database, table, page, row, or even field
(attribute).
• Database-level Lock
- In database-level lock entire database is locked by the transaction.
- For example: if transaction T1 has locked the database, then transaction T2 will wait until
T1 unlocks the database.
- T2 cannot access any other tables within the database. Thus, two transaction cannot
access the same database at the same time.
• Table-level Lock
- In table-level lock entire table is locked and rest of the database can be locked by other
transaction.
- For example: Transaction T1 has locked the table product, the simultaneous transaction
T2 can lock another table customer.
• Page-level Lock
- In a page-level lock, the DBMS will lock an entire disk page. A disk page, or page, is the
equivalent of a disk block, which can be described as a directly addressable section of a
disk.
- A page has a fixed size, such as 4K, 8K, or 16K.
- A table can span several pages, and a page can contain several rows of one or more
tables.
- Page-level locks are currently the most frequently used multiuser DBMS locking method.
• Row-level Lock
- A row-level lock is much less restrictive than the other locks.
- The DBMS allows concurrent transactions to access different rows of the same table even
when the rows are located on the same page.
- Although the row-level locking approach improves the availability of data, its
management requires high overhead because a lock exists for each row in a table of the
database involved in a conflicting transaction.
Types of Locks
- Regardless of the level of locking, the DBMS may use different lock types which are as
follows: Binary or Shared/Exclusive.
• Binary Lock
- A binary lock has only two states: locked (1) or unlocked (0).
- If an object - that is, a database, table, page, or row – is locked by a transaction, no other
transaction can use that object.
- If an object is unlocked, any transaction can lock the object for its use.
- Every database operation requires that the affected object be locked.
- As a rule, a transaction must unlock the object after its termination.
- Therefore, every transaction requires a lock and unlock operation for each data item that
is accessed.
- Such operations are automatically managed and scheduled by the DBMS; the user does
not need to be concerned about locking or unlocking data items.
Time Transaction Step Stored Value
1 T1 Lock Product
2 T1 Read PROD_QOH 35
3 T1 PROD_QOH = 35+100
4 T1 Write PROD_QOH 135
5 T1 Unlock Product
6 T2 Lock Product
7 T2 Read PROD_QOH 135
8 T2 PROD_QOH = 135-30
9 T2 Write PROD_QOH 105
10 T2 Unlock Product
o Shared Lock
- A shared lock exists when parallel transactions are granted read access on the basis of a
common lock.
- A shared lock produces no conflict as long as all the concurrent transactions are read-
only.
Validation Phase
- During the validation phase, the transaction is validated to ensure that the changes made
will not affect the integrity and consistency of the database.
- If the validation test is positive, the transaction goes to the write phase.
- If the validation test is negative, the transaction is restarted and the changes are discarded.
Write Phase
- During the write phase, the changes are permanently applied to the database.
• Differential Backup
- Instead of copying the entire database again and again, only last modifications done to the
database are considered for backup.
- The recovery of transactions, recovery techniques also apply to the database and to the
system after some type of critical error has occurred.
- Critical events can cause a database to become nonoperational and compromise the
integrity of the data.
- Examples of critical events are:
• Hardware/Software Failure
- A failure of this type could be a hard disk media failure, a bad capacitor on a
motherboard, or a failing memory bank.
- Other causes of errors under this category include application program or operating
system errors that cause data to be overwritten, deleted, or lost.
• Human-Caused Incident
- This type of event can be categorized as unintentional or intentional.
- An unintentional failure is caused by carelessness by end users. Such errors include
deleting the wrong rows from a table, pressing the wrong key on the keyboard, or shutting
down the main database server by accident.
- Intentional events are of a more severe nature and normally indicate that the company
data are at serious risk. Under this category are security threats caused by hackers trying
to gain unauthorized access to data resources and virus attacks caused by dissatisfied
employees trying to compromise the database operation and damage the company.
• Natural Disasters
- This category includes fires, earthquakes, floods, and power failures.
Transaction Recovery
- Database transaction recovery uses data in the transaction log to recover a database from
an inconsistent state to a consistent state.
- The database recovery process involves bringing the database to a consistent state after a
failure.
- Transaction recovery procedures generally make use of deferred-write techniques and
write-through techniques.
• Deferred-Write Techniques
- The transaction operations do not immediately update the physical database. Instead, only
the transaction log is updated.
- The database is physically updated only after the transaction reaches its commit point,
using information from the transaction log.
• Write-Through Techniques
- The database is immediately updated by transaction operations during the transaction’s
execution, even before the transaction reaches its commit point.