Professional Documents
Culture Documents
By Yaregal N.
1
Agendas
| Database recovery
01
What do you understand from the diagram?
02
What is a Transaction?
| Action, or series of actions, carried out by a single user or application program, which
accesses or changes contents of database. (i.e. Logical unit of work on the database.)
| Examples include ATM transactions, credit card approvals, flight reservations, hotel
check-in, phone calls, supermarket canning, academic registration and billing.
03
Transaction processing system
| A system that manages transactions and controls their access to a DBMS is called a
TP monitor.
| Transaction processing systems are systems with large databases and hundreds
of concurrent users executing database transactions.
04
Single-User Vs Multi-User Systems
Single-User Multi-User
Access restricted to a single user at a time Access can share by multiple user at a time
06
Desirable Properties of Transactions
07
Cont…..
08
Cont…..
09
States of a Transactions
3. Murderous Termination: When the DBMS or the system force the execution
to abort for any reason. And hence, rolled back.
10
States of a Transactions
11
Ways of Transaction Execution: Serially
| Thus, Transaction Ti completes and writes its results to the database then
only the next transaction Tj is scheduled for execution.
| This means at one time there is only one transaction is being executed in
the system.
13
Ways of Transaction Execution: Serially
| Advantages:
• Correct execution (if the input is correct then output will be correct)
• Fast execution (since all the resources are available to the active)
| Disadvantages:
14
Examples of Serial Execution:
Time T1 T2
read (X) {X = 10}
X := X+N {X = 11}
write (X) {X = 11}
read (Y) {Y = 6}
Y := Y+N {Y = 7}
write (Y) {Y = 7}
read (X) {X = 11}
X := X+N {X = 12}
write (X)
X := X+N {X = 11}
X := X+N {X = 11}
| In serial execution T2 read the value of X written by T1 (i.e., 11) but in concurrent
execution T2 read the same value of X (i.e., 10) as T1 did and the update made by T1
| This is the reason the final value of X is one less than what is produced by serial 18
Why Concurrent Execution?
Reasons:
| Many systems have an independent component to handle I/O like DMA
(Direct Memory Access) module.
| CPU may process other transactions while one is in I/O operation
19
Problems Associated with Concurrent Transaction Processing
| Example:- T2 increases 100 making it 200 but then aborts the transaction before it
is committed. T1 gets 200, subtracts 10 and make it 190. But the actual balance
should be 90
22
Cont……
| Inconsistent Analysis Problem: Occurs when transaction reads several values but second
transaction updates some of them during execution and before the completion of the first.
| Example:-T2 would like to add the values of A=10, B=20 and C=30. after the
values are read by T2 and before its completion, T1 updates the value of B to be 50.
At the end of the execution of the two transactions T2 will come up with the sum
of 60 while it should be 90 since B is updated to 50.
23
Serializability
24
Schedule
| The scheduler component of a DBMS must ensure that the individual steps of
different transactions preserve consistency.
25
Cont…..
| Serial Schedule: a schedule where the operations of each transaction are executed
consecutively without any interleaved operations from other transactions.
| In other words, want to find non-serial schedules that are equivalent to some
serial schedule. Such a schedule is called serializable.
26
Serialization
| If two transactions either read or write completely separate data items, they do not
conflict and order is not important.
| If one transaction writes a data item and another reads or writes the same data
item, order of execution is important
| Possible solution: Run all transactions serially.
| This is often too restrictive as it limits degree of concurrency or parallelism in
system. 27
Test of Serializability
| Constrained write rule: transaction updates data item based on its old value,
which is first read by the transaction.
| Under the constrained write rule we can use precedence graph to test for
Serializability.
| A precedence graph is composed of:
• Nodes to represent the transactions, and
• Arc(directed edge) to connect nodes indicating the order of execution for
the transactions
| So that the directed edge TiTj implies, in an equivalent serial execution Ti must be
executed before Tj.
28
Precedence Graph for Schedule S
| The set of edges is used to contain all edges Ti ->Tj for which one of the three
conditions holds:
1. Create a node Ti->Tj ,if Ti executes write(Q) before Tj executes
read(Q)
2. Create a node Ti->Tj ,if Ti executes read(Q) before Tj executes
write(Q)
3. Create a node Ti->Tj ,if Ti executes write(Q)before Tj executes
write(Q)
Example 1:
29
Precedence Graph for Schedule S
| The precedence graph for the schedule looks like:
30
Precedence Graph for Schedule S
| Example2:
31
Precedence Graph for Schedule S
| Example2 precedence graph for S2:
The precedence graph for schedule S2 contains no cycle that's why ScheduleS2 is
serializable.
32
Concurrency Control Techniques
33
Concurrency Control Technique?
| When more than one transactions are running simultaneously there are chances
of a conflict to occur which can leave database to an inconsistent state.
34
Cont….
Example:
| In concurrent execution environment, if T1 conflicts with T2 over a data
item A, then the existing concurrency controller decides if T1 or T2 should
get A and which transaction should be rolled-back or waits 35
Cont…….
| Three basic concurrency control techniques are:
1. Locking methods
• Pessimistic and conservative approaches: since they
2. Time stamping delay transactions in case they conflict with other
transactions.
3. Optimistic
• Allows us to proceed and check conflicts at the end.
36
Locking Method
| Locking method is a mechanism for preventing simultaneous access on a shared
resource for a critical operation.
| Lock prevents another transaction from modifying item or even reading it, in the
case of a write lock.
37
Types of a Lock
38
Binary Locks
| Can have two states or values: locked and unlocked (or 1 and 0).
| We refer to the current value (or state) of the lock associated with item X as
lock(X).
| Too restrictive for database items because at most one transaction can hold a lock
on a given item
39
Cont…
| Lock and Unlock operations are as follows:
40
Cont…
| Every transaction in a binary lock must obey the following rules:
| Allow several transactions to access the same item x if they all access x for
reading purpose
| A lock associated with an item X, LOCK(X), has three possible states: "read-
locked," "write-locked," or "unlocked."
42
Cont…..
43
Cont….
44
Cont…
| When we use the shared/exclusive locking scheme, the system must enforce the
following rules:
3. A transaction T must issue the operation unlock(X) after all read_item(X) and
write_item(X) operations are completed in T.
45
Cont…
46
Lock Conversion
| Sometimes, it is desirable to relax condition 4 and 5 in the coding list in order to
allow lock conversions. That is:
Under certain conditions, a transaction that already holds a lock on item X is allowed to
convert the lock from one lock state to another.
For example, it is possible for a transaction T to issue a read_lock (X) and then later on to
upgrade the lock by issuing a write_lock(x) operation
If T is the only transaction holding a read lock on x at the time it issues the write_lock (x)
operation, the lock can be upgraded ;otherwise, the transaction must wait.
It is also possible for a transaction T to issue a write_lock(X) and then later on to downgrade
the lock by issuing a read_lock(X) operation
47
Lock Conversion
| Lock upgrade: change existing read lock to write lock.
o If Ti has a read-lock (X) and Tj has no read-lock (X) (i j) then convert read-lock (X) to write-
lock (X) else force Ti to wait until Tj unlocks X
48
Example: Locking Method
| T1 and T2 are two transactions. They are executed under locking as follows. T1
locks A in exclusive mode. When T2 wants to lock A, it finds it locked by T1 so
T2 waits for Unlock on A by T1. When A is released then T2 locks A and begins
execution.
| Suppose a lock on a data item is applied, the data item is processed and it is
unlocked immediately after reading/writing is completed as follows.
49
Example: Locking Method
50
Two-Phase Locking (2PL)
| A transaction is said to follow two phase locking protocol if all locking
operations (either read_lock or write_lock) precede the first unlock operation in
the transaction.
Hence the 2PL protocol allows avoiding the three problems of concurrent
execution. 51
Locking methods: Problems
A. Dead Lock: A deadlock that may result when two (or more) transactions are
each waiting for locks held by the other to be released.
Only one way to break deadlock: abort one or more of the transactions in
the deadlock.
1. Deadlock prevention.
If a transaction waits for the predefined period of time in idle mode, the
DBMS will assume that deadlock occurred and it will abort and restart the
transaction.
53
Time-stamping Methods
• Time stamp (TS): Is a unique identifier created by the DBMS to identify a
transaction
54
Cont……
Rules for permitting execution of operations in Time-stamping Method:
Suppose that Transaction Ti issues Read(A)
o If TS(Ti) < WTS(A): this implies that Ti needs to read a value of A which was already
overwritten. Hence the read operation must be rejected and Ti is rolled back.
o If TS(Ti) >= WTS(A): then the read is executed and RTS(A) is set to the maximum of RTS(A)
and TS(Ti).
Suppose that Transaction Ti issues Write(A)
o If TS(Ti) < RTS(A): then this implies that the value of A that Ti is producing was previously needed and
it was assumed that it would never be produced. Hence, the Write operation must be rejcted and Ti is
rolled back.
o If TS(Ti) < WTS(A): then this implies that Ti is attempting to Write an object value of A. hence, this write
operation can be ignored.
o Otherwise the Write operation is executed and WTS(A) is set to the maximum of WTS(A) or TS(Ti).
55
Optimistic Technique
| In this technique, Serializability is checked only at the time of commit and
transactions are aborted in case of non-serializable schedules
| In this scheme, updates in the transaction are not applied directly to the database
item until it reaches its commit point
| Three phases:
1. Read phase
2. Validation phase
3. Write phase 56
Cont….
1. Read phase: A transaction can read values of committed data items. However,
updates are applied only to local copies (versions) of the data items (in database
cache)
57
Database Recovery
58
Definition
| Database recovery is the process of restoring database to a correct state in the
event of a failure.
| A database recovery is the process of eliminating the effects of a failure from the
database.
59
Purpose of Database Recovery
| To bring the database into the last consistent state, which existed prior to the
failure.
| To preserve transaction properties (Atomicity, Consistency, Isolation and
Durability).
| Example:
o If the system crashes before a fund transfer transaction completes its
execution, then either one or both accounts may have incorrect value. Thus,
the database must be restored to the state before the transaction modified any
of the accounts.
60
Types of Database Recovery
| The database may become unavailable for use due to:
3. Media failure: failure of non-volatile storage media (mainly disk). Disk head
crash, power disruption, etc.
61
Cont…….
| The basic steps in performing a recovery are:
1. Isolating the database from other users. Occasionally, you may need to drop
and re-create the database to continue the recovery.
62
Transaction Log
| Execution history of concurrent transactions.
| For recovery from any type of failure data values prior to modification (BFIM -
BeFore Image) and the new value after modification (AFIM – AFter Image) are
required.
| These values and other information is stored in a sequential file called
Transaction log.
o Logging facility: that keeps track of current state of transactions and database
changes.
o Checkpoint facility: that enables updates to database in progress to be made
permanent.
o Recovery manger: This allows the DBMS to restore the database to a
consistent state following a failure.
64
Check pointing
| Randomly or under some criteria, the database flushes its buffer to database disk
to minimize the task of recovery.
| A checkpoint record is written into the log periodically at that point when the
system writes out to the database on disk all DBMS buffers that have been
modified.
| The following steps defines a checkpoint operation:
o Suspend execution of transactions temporarily.
o Force write modified buffer data to disk.
o Write a [checkpoint] record to the log, save the log to disk.
o Resume normal transaction execution.
| During recovery, redo or undo is required to transactions appearing after
[checkpoint] record.
65
Recovery Techniques
1. Deferred Update
o Updates are not written to the database until after a transaction has reached
its commit point.
o If transaction fails before commit, it will not have modified database and so
no undoing of changes required.
o May be necessary to redo updates of committed transactions as their effect
may not have reached database.
o If a transaction aborts, ignore the log record for it. And do nothing with
transaction having a “transaction start” and “Transaction abort” log records
o The redo operations are made in the order they were written to log. 66
Recovery Techniques
2. Immediate Update/ Update-In-Place
o Updates are applied to database as they occur.
o Need to redo updates of committed transactions following a failure.
o May need to undo effects of transactions that had not committed at time of failure.
o Essential that log records are written before write to database. Write-ahead log protocol.
o If no "transaction commit" record in log, then that transaction was active at failure and must
be undone.
o Undo operations are performed in reverse order in which they were written to log.
67
Recovery Techniques
3. Shadow Paging
o Maintain two page tables during life of a transaction: current page and shadow page table.
o When transaction starts, two pages are the same.
o Shadow page table is never changed thereafter and is used to restore database in event of
failure.
o During transaction, current page table records all updates to database.
o When transaction completes, current page table becomes shadow page table.
o No log record management
o However, it has an overhead of managing pages i.e. page replacement issues have to be
handled.
68
Many Thanks!!!