Chapter 6
Chapter 6
6. Transaction management
[email protected]
Chapter 6. Transaction management
• Transactions definition
• Concurrent access anomalies
• Transaction properties
• Transaction states
• Scheduling transactions
• Concurrency control
• Database restauration techniques
Transactions definition
• Normal execution:
• T1: X = 100 + 20 = 120
• T2: X = 120 + 10 = 130
T1 BD (X = 100) T2 T1 BD (X = 100) T2
read(X): X = 100 read(X): X = 100
X = X + 20 = 120 X = X + 20 = 120
write(X) X = 120 read(X): X = 100
read(X): X = 120 X = X + 10 = 110
X = X + 10 = 130 X = 110 write(X)
X = 130 write(X) write(X) X = 120
T1 BD (X = 100) T2 T1 BD (X = 100) T2
read(X): X = 100 read(X): X = 100
X = X + 20 = 120 X = X + 20 = 120
write(X) X = 120 write(X) X = 120
read(X): X = 120 read(X): X = 120
X = X + 10 = 130 X = X + 10 = 130
X = 130 write(X) X = 130 write(X)
Normal execution …
abort
Dirty read
Transaction properties (ACID properties)
• Example:
START TRANSACTION;
UPDATE products SET price = 5499.99 WHERE id = 1;
UPDATE products SET stock = 24 WHERE id = 2;
COMMIT;
SA SB
Non-serial scheduling
T1 T2
read(X) – R1(X)
X=X–N
write(X) – W1(X)
read(Y) – R1(Y) read(X) – R2(X)
Y=Y+N X=X+M
write(Y) – W1(Y) write(X) – W2(X)
SC
• Result: X = X – N + M; Y = Y + N;
Transaction scheduling
• Database-level locks:
• good for batch processing, but not for concurrent access
• transactions can’t access the same database even if the change different tables
• Table-level locks:
• multiple transactions can access the same database as long as they modify
different tables
• can cause bottlenecks when multiple transactions access the same table (even
if they require changing different parts of the table)
• Row-level locks:
• concurrent transactions can access the same table as long as they modify
different rows
• improves data availability, but with high overhead
Binary locks
• Exclusive lock
• Access is specifically reserved for the transaction that locked the object
• Must be used when the potential for conflict exists – when an update is
required and no locks are currently held on the data item by other transactions
• Granted if and only if no other locks are held on the data item
• Shared lock
• Concurrent transactions are granted read access on the basis of a common lock
• Issued when a transaction wants to read data and no exclusive lock is held on
that data item
• Multiple transactions can each have a shared lock on the same data item
• Any transaction that uses a multi-state locks M(X) must comply with the
following rules:
• A transaction must execute a shared or exclusive locking operation (read_lock(X) or
write_lock(X)) before doing any read operation of item X
• A transaction must execute an exclusive lock of the item X (write_lock(X)) before
performing any writing operations
• A transaction must release the lock of an item X (unlock(X)) after it has performed all
read or write operations of item X
• The lock release operation can only be executed by a transaction that owns the lock
• Two possible problems may occur
• The resulting transaction schedule may not be serializable
• The schedule may create deadlocks
Shared/exclusive (multi-state) locks
T1 T2 T1 T2 T1 T2
read(Y) lock(XY) lock(Y)
read(X) read(Y) read(Y)
X=X+Y read(X) unlock(Y)
write(X) lock(XY) lock(X)
read(X) X=X+Y read(X)
read(Y) write(X) T2 blocked unlock(X)
Y=X+Y unlock(XY) lock(X) lock(Y)
write(Y) read(X) read(X) read(Y)
read(Y) X=X+Y Y=X+Y
X = 20; Y = 30
Y=X+Y write(X) write(Y)
Correct result: X = 50; Y = 80 write(Y) unlock(X) unlock(Y)
unlock(XY) Wrong: X = 50; Y = 50
Two phase locking
DELIMITER //
CREATE PROCEDURE new_reservation(
IN first_name VARCHAR(255),
IN last_name VARCHAR(255),
IN dob DATE,
IN passport VARCHAR(255),
IN flight_schedule INT UNSIGNED,
IN class INT UNSIGNED,
IN seat VARCHAR(255),
IN price DECIMAL(10,2) UNSIGNED,
OUT reservation_id INT UNSIGNED,
OUT invoice_id INT UNSIGNED
)
Transaction example
DELIMITER //
CREATE PROCEDURE new_reservation(…)
BEGIN
DECLARE passenger_id INT UNSIGNED;
DECLARE num_seats INT UNSIGNED;
DECLARE seat_available BOOLEAN;
DECLARE already_booked BOOLEAN;
DECLARE EXIT HANDLER FOR SQLSTATE '45000'
BEGIN
ROLLBACK;
RESIGNAL;
END;
END //
DELIMITER ;
Transaction example
DELIMITER //
CREATE PROCEDURE new_reservation(…)
BEGIN
…
START TRANSACTION;
-- Check if passenger exists
-- Insert the passenger if it doesn't exist
-- Check if the passenger booked the flight
-- Check if there are any seats available
-- Check if the requested seat is available
-- Create the reservation and invoice
COMMIT;
END //
DELIMITER ;
Transaction example
Database recovery
• Deferred update recovery uses the write records in the log file,
which contain the transaction identifier (T), the item in which it is
written (X) and the value to be written (value) [write, T, X, value]
• Example: a transaction planning, a checkpoint, recorded at time tc
and the occurrence of a non-catastrophic fault at time tf
Database recovery
• To recover, the log file is read backwards from the last record until
the first checkpoint is met and:
• Transactions that have committed [commit] prior to the checkpoint (T1) are
not affected by the fault
• A list of validated transactions LTV is created, in which all transactions that
have a [commit] record entry are entered into the log file between the last
checkpoint and the end of the log file; In the example, LTV = {T2, T3}
• A list of non-valid transactions LTNV is created, containing all transactions
that have a start [start] entry in the log file, but do not have the corresponding
commit [commit]; In the example: LTNV = {T4, T5}
• The write operations of the validated transactions (T2 and T3) are executed in
the order in which they appear in the log file - write (T, X, value)
• Invalid transactions are relaunched: T4, T5
Database recovery
• If the database files have been destroyed due to disk failure, then
the database can only be restored from a backup
• The last saved copy is loaded from the backup HDD/SSD/tape
and the system restarts
• However, transactions made since the last rescue operation of the
database until the error occurred are lost
• Because the log file is much smaller than the database files, it is
customary to save it more often than the database itself
• In this situation, after uploading the last saved database copy, the
saved log file (which is more recent than the saved copy of the
database) can be used to restore all existing validated transactions