Transaction Management
Transaction Management
MANAGEMENT
1.
A Transaction…
2
Definition
3
Definition
4
2.
ACID Model
5
ATOMICITY
6
Consistency Preservation
7
Isolation/Independence
8
Durability (or Permanency)
9
Serializability
10
3.
MySQL Transaction
11
Statements use in MySQL Transactions
13
Savepoint
17
Example:
18
Example:
◎ Initialize a Transaction
19
Example:
◎ Changes:
20
Example:
21
“
“The ROLLBACK TO SAVEPOINT statement rolls back a
transaction to the named savepoint without terminating the
transaction. Modifications that the current transaction made to
rows after the savepoint was set are undone in the rollback,
but InnoDB does not release the row locks that were stored in
memory after the savepoint. (For a new inserted row, the lock
information is carried by the transaction ID stored in the row;
the lock is not separately stored in memory. In this case, the
row lock is released in the undo.)”
22
5.
Lock and Unlock
Tables
23
Lock and Unlock Tables
25
6.
Concurrency
Problems
26
Lost Updates
27
Example (Lost Updates)
28
Example (Lost Updates)
◎ Correct Result
29
Example (Lost Updates)
30
Uncommited Dependency
31
Example (Uncommited Dependency)
32
Example (Uncommited Dependency)
33
Example (Uncommited Dependency)
34
Inconsistent Retrievals
35
Example (Inconsistent Retrievals)
36
Example (Inconsistent Retrievals)
◎ Transaction Corrections
37
Example (Inconsistent Retrievals)
◎ Inconsistent Retrievals
38
7.
Scheduler
39
Scheduler
41
8.
Concurrency Control
with Locking
Methods
42
Concurrency Control with Locking Methods
44
Lock Granularity
◎ Database-level lock
○ the entire database is locked, thus preventing the
use of any tables in the database by transaction T2
while transaction T1 is being executed.
○ This level of locking is good for batch processes,
but it is unsuitable for multiuser DBMSs.
45
Lock Granularity
◎ Table-level lock
○ the entire table is locked, preventing access to any
row by transaction T2 while transaction T1 is using
the table.
○ If a transaction requires access to several tables,
each table may be locked.
○ However, two transactions can access the same
database as long as they access different tables.
46
Lock Granularity
◎ Page-level lock
○ the DBMS will lock an entire diskpage.
○ A diskpage, or page, is the equivalent of a
diskblock, which can be described as a directly
addressable section of a disk.
○ Page-level locks are currently the most frequently
used multiuser DBMS locking method.
47
Lock Granularity
◎ Row-level lock
○ is much less restrictive than the locks discussed
earlier.
○ The DBMS allows concurrent transactions to
access different rows of the same table even when
the rows are located on the same page.
○ Modern DBMS automatically escalate a lock from
row level to page level lock when the application
session requests multiple locks on the same page.
48
Lock Granularity
◎ Field-level lock
○ allows concurrent transactions to access the same
row as long as they require the use of different
fields (attributes) within that row.
49
Lock Types
50
Lock Types
◎ Binary Locks
○ 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.
○ As a rule, a transaction must unlock the object
after its termination.
51
Lock Types
◎ Binary Locks
52
Lock Types
◎ Shared/Exclusive Locks
○ The labels “shared” and “exclusive” indicate the
nature of the lock.
○ An exclusive lock exists when access is reserved
specifically for the transaction that locked the
object.
○ The exclusive lock must be used when the
potential for conflict exists.
53
Lock Types
◎ Shared/Exclusive Locks
○ A shared lock exists when concurrent 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.
54
Lock Types
◎ Shared/Exclusive Locks
○ A shared lock is issued when a transaction wants to
read data from the database and no exclusive lock is
held on that data item.
○ An exclusive lock is issued when a transaction wants to
update (write) a data item and no locks are currently
held on that data item by any other transaction.
○ Using the shared/exclusive locking concept, a lock can
have three states: unlocked, shared (read), and
exclusive (write).
55
9.
Two-phase Locking
56
Two-phase Locking
57
Two-phase Locking
58
Two-phase Locking
59
Two-phase Locking
60
10.
Deadlocks
61
Deadlocks
62
Deadlocks
63
Deadlocks
64
10.
Concurrency Control
with Timestamping
Method
65
Timestamping
66
Timestamping
◎ Wait/Die Scheme
○ If the transaction requesting the lock is the older of
the two transactions, it will wait until the other
transaction is completed and the locks are
released.
○ If the transaction requesting the lock is the
younger of the two transactions, it will die (roll
back) and is rescheduled using the same time
stamp.
67
Timestamping
◎ Wait/Die Scheme
○ in the wait/die scheme, the older transaction
waits for the younger to complete and release its
locks.
68
Timestamping
◎ Wound/Wait Scheme
○ If the transaction requesting the lock is the older of
the two transactions, it will preempt (wound) the
younger transaction (by rolling it back).
○ If the transaction requesting the lock is the
younger of the two transactions, it will wait until
the other transaction is completed and the locks
are released.
69
Timestamping
◎ Wound/Wait Scheme
○ in the wound/wait scheme, the older transaction
rolls back the younger transaction and
reschedules it.
70
Optimistic Approach
◎ Read Phase
○ the transaction reads the database, executes the
needed computations, and makes the updates to a
private copy of the database values.
○ All update operations of the transaction are
recorded in a temporary update file, which is not
accessed by the remaining transactions.
72
Optimistic Approach
◎ 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.
73
Optimistic Approach
◎ Write Phase
○ the changes are permanently applied to the
database.
74
11.
Database Recovery
Management
75
Database Recovery
76
Critical Events
◎ Hardware/software failures.
◎ Human-caused incidents.
◎ Natural disasters.
77
Transaction Recovery
◎ write-ahead-log protocol
○ ensures that transaction logs are always written
before any database data are actually updated
○ This protocol ensures that, in case of a failure, the
database can later be recovered to a consistent
state, using the data in the transaction log.
78
Transaction Recovery
79
Transaction Recovery
◎ Database buffers
○ are temporary storage areas in primary memory
used to speed up disk operations.
○ To improve processing time, the DBMS software
reads the data from the physical disk and stores a
copy of it on a “buffer” in primary memory.
80
Transaction Recovery
◎ Database checkpoints
○ are operations in which the DBMS writes all of its
updated buffers to disk.
81