Final DBMS Unit-6
Final DBMS Unit-6
- In our example once our transaction starts from first step (step 1) its result
should not be access by any other transaction until last step (step 6) is
completed.
ACID properties of transaction (Durability)
- Once our transaction completed up to last step its result must be stored
permanently. It should not be removed if system fails.
Transaction State Diagram
Transaction State Diagram/ State Transition Diagram
- Active
□ This is the initial state.
□ The transaction stays in this state while it is executing.
- Partial Committed
□ When a transaction executes its final operation/ instruction, it is said to be in a partially committed state.
- Failed
□ Discover that normal execution can no longer proceed.
□ Once a transaction cannot be completed, any changes that it made must be undone rolling it back.
- Committed
□ The transaction enters in this state after successful completion of the transaction (after committing
transaction).
□ We cannot abort or rollback a committed transaction.
- Aborted
□ The state after the transaction has been rolled back and the database has been restored to its state
prior to the start of the transaction.
Scheduling
- Non Serial Scheduling : Schedule that interleave the execution of different transactions.
- Means second transaction is started before the first one could end and execution can switch
between the transactions back and forth.
- Equivalent Scheduling : If two schedules produce the same result after execution, they are
said to be equivalent schedule.
- They may yield the same result for some value and different results for another set of values.
Example of Serial Schedule
Example of Non Serial Schedule
Serializability
- A schedule is serializable if it is equivalent to a serial schedule.
- In serial schedules, only one transaction is allowed to execute at a time
i.e. no concurrency is allowed.
- Whereas in serializable schedules, multiple transactions can execute
simultaneously i.e. concurrency is allowed.
- Types (forms) of serializability
- Conflict serializability
- View serializability
Conflict Serializability
- If a given schedule can be converted into a serial schedule by
swapping its non-conflicting operations, then it is called as a
conflict serializable schedule.
- Let S1 and S2 be two schedules with the same set of transactions. S1 and
S2 are view equivalent if the following three conditions are satisfied, for each
data item Q
- Initial Read
- Updated Read
- Final Write
- If a schedule is view equivalent to its serial schedule then the given schedule
is said to be view serializable.
Example of View Serializability - Initial Read
- Above two schedules S1 and S3 are not view equivalent because initial
read operation in S1 is done by T1 and in S3 it is done by T2.
- Above two schedules S1 and S2 are view equivalent because initial read
operation in S1 is done by T1 and in S2 it is also done by T1.
Example of View Serializability - Updated Read
- If in schedule S1 transaction Ti executes read(Q), and that value was
produced by transaction Tj (if any), then in schedule S2 also transaction Ti
must read the value of Q that was produced by transaction Tj.
- Above two schedules S1 and S3 are not view equal because, in S1, T3 is
reading A that is updated by T2 and in S3, T3 is reading A which is
updated by T1.
- Above two schedules S1 and S2 are view equal because, in S1, T3 is
reading A that is updated by T2 and in S2 also, T3 is reading A which is
updated by T2.
Example of View Serializability - Final Write
- If Ti performs the final write on the data value in S1, then it also performs the final
write on the data value in S2.
- Above two schedules S1 and S3 are not view equal because final write operation in
S1 is done by T3 and in S3 final write operation is also done by T1.
- Above two schedules S1 and S2 are view equal because final write operation in S1
is done by T3 and in S2 also the final write operation is also done by T3.
-
Two phase commit protocol
Two phase commit protocol ensures that all participants perform the same action
(either to commit or to rollback a transaction).
It is designed to ensure that either all the databases are updated or none of them,
so that the databases remain synchronized.
In two phase commit protocol there is one node which is act as a coordinator or
controlling site and all other participating node are known as cohorts or participant or
slave.
Coordinator (controlling site) – the component that coordinates with all the
participants.
Cohorts (Participants/Slaves) – each individual node except coordinator are
participant.
As the name suggests, the two phase commit protocol involves two phases.
Commit request phase OR Prepare phase
Commit/Abort phase
Two phase commit protocol
Prepar
e
Phase
Commit
Phase
Coordinator
Coordinator
Participant
Send inform
send
send
“ack” to to
reply
inform
do commit
request
whether asking
ready for
commit ready
todone
to commit
commit
or not or not
Two phase commit protocol Commit Request Phase (Obtaining Decision)
- In any of above situations, data in the database may become inconsistent or lost.
- For example, if a transaction has completed 30 out of 40 write instructions to the database when the
DBMS crashes, then the database may be in an inconsistent state as only part of the transaction’s
work was completed.
- Database recovery is the process of restoring the database and the data to a consistent state.
- This may include restoring lost data up to the point of the event (e.g. system crash).
Log based recovery method
- The log is a sequence of log records, which maintains information about update activities on the
database.
- A log is kept on stable storage (i.e HDD).
- Log contains
-Start of transaction
- Transaction-id
- Record-id
- Type of operation (insert, update, delete)
- Old value, new value
- End of transaction that is committed or aborted.
Log based recovery method
- When transaction Ti starts, it registers itself by writing a record <Ti start> to the log.
- Before Ti executes write(X), a log record <Ti, X, V1, V2> is written, where V1 is the value of X
before the write (the old value), and V2 is the value to be written to X (the new value).
- When Ti finishes it last statement, the log record <Ti commit> is written.
- Undo of a log record <Ti, X, V1, V2> writes the old value V1 to X
- Redo of a log record <Ti, X, V1, V2> writes the new value V2 to X
- Deferred database modification : Updates (changes) to the database are deferred (postponed)
until the transaction commits.
- If transaction is not committed, then no need to do any undo operations. Just restart the transaction.
If transaction is committed, then we need to do redo the updates of the transaction.Only Redo
operation is performed.
Problems with Deferred and Immediate Updates
(Checkpoints)
- Searching the entire log is time consuming.
□ Immediate database modification
▪ When transaction fail log file is used to undo the updates of transaction.
□ Deferred database modification
▪ When transaction commits log file is used to redo the updates of transaction.
- To reduce the searching time of entire log we can use check point.
- It is a point which specifies that any operations executed before it are done correctly and stored
safely (updated safely in database).
- At this point, all the buffers are force-fully written to the secondary storage (database).
- Checkpoints are scheduled at predetermined time intervals.
- It is used to limit:
□ Size of transaction log file
□ Amount of searching
H- oAw
t failure time:
the checkpoint works when failure occurs
- Ignore the transaction T1 as it has already been committed before checkpoint.
- Redo transaction T2 and T3 as they are active after checkpoint and are committed before failure.
- Undo transaction T4 as it is active after checkpoint and has not committed
Page Table Structure
- Two pages - page 2 & 5 - are affected by a transaction and copied to new physical pages. The
current page table points to these pages.
- The shadow page table continues to point to old pages which are not changed by the transaction.
So, this table and pages are used for undoing the transaction.
Shadow paging technique
● It ensures that the database remains in a consistent state even when multiple transactions are
executed simultaneously.
● Why concurrency control is important?
● In a multi-user database environment, several transactions may try to access the same data
simultaneously. Without proper concurrency control, this can lead to various problems such as:
• Lost Update
• Dirty Read
• Non-Repeatable Read
Introduction to Concurency Control
• Lost Updates: When two transactions update the same data simultaneously, and one of the
updates is lost.
• Dirty Reads: When a transaction reads data that has been modified by another transaction but
not yet committed.
• Non-Repeatable Reads: When a transaction reads the same data multiple times and finds
different values because another transaction has updated the data in between.
Concurency Control Mechanism
● To handle the issues that arise from concurrent transactions, several concurrency control mechanisms
are employed. Protocols are used to maintain ACID properties of all the transaction.
● Lock-Based Protocols:
● Locks are mechanisms that restrict access to data items to prevent conflicting operations from occurring
simultaneously.
Types of Locks
Shared Lock (S-lock):
• Allows multiple transactions to read a data item.
• Prevents any transaction from modifying the data item.
• Multiple transactions can hold shared locks on the same data item simultaneously.
Finally, each file has child nodes called records and no record is present in more than one file.
Timestamp based Protocol
Lock based protocols ensure conflict serializability but it causes 2 problems:
➢ Deadlock and
➢ Starvation
Timestamp-Based Protocols
❑The Timestamp methods for concurrency control does not need any locks and
therefore there are no deadlocks.
❑ Locking methods generally prevent conflicts by making transactions to wait.
❑ Timestamp methods do not make the transactions wait.
❑In this method, Transactions involved in a conflict are simply rolled back and
restarted.
● The most commonly used concurrency protocol is the timestamp based protocol. This protocol uses
either system time or logical counter as a timestamp.
● Lock-based protocols manage the order between the conflicting pairs among transactions at the
time of execution, whereas timestamp-based protocols start working as soon as a transaction is
Timestamp based Protocol
❑ Every transaction has a timestamp associated with it, and the ordering is determined by the age of the
transaction. A transaction created at 0002 clock time would be older than all other transactions that come
after it. For example, any transaction 'y' entering the system at 0004 is two seconds younger and the
priority would be given to the older one.
❑In addition, every data item is given the latest read and write-timestamp. This lets the system know
when the last ‘read and write’ operation was performed on the data item.
Introduction - Deadlock
● Deadlock in a database management system (DBMS) is a situation where
two or more processes are blocked, waiting for each other to release
a resource that they need to proceed with their execution.
● This creates a circular wait, where each process is waiting for a resource
that is held by another process, leading to a complete blockage of the
system.
● Deadlocks can occur in multi-user, multi-tasking environments, where
multiple processes are accessing shared resources simultaneously and
can cause decreased system performance or system failure if not
resolved.
Introduction - Deadlock
Introduction - Deadlock - Example
● Detection
○ Wait-for graph
● Prevention
○ Resource ordering
○ Timeout
○ Wait-die
○ Wound-wait
Deadlock Detection
● Deadlock detection is a technique used in DBMS to identify and resolve
deadlocks.
● In this technique, the system continuously checks for deadlocks and, if
found, takes appropriate action to resolve the deadlock.
● When a transaction waits indefinitely to obtain a lock, The database
management system should detect whether the transaction is involved in a
deadlock or not.
● The lock manager maintains a Wait for the graph to detect the deadlock
cycle in the database.
Deadlock Detection - Wait for Graph
● A transaction waits for a lock only for a specified amount of time. After the
wait time is out and the transaction is rolled back. (No deadlock!)
Thank You