Unit 5 TM
Unit 5 TM
5.1 TRANSACTION:
A transaction is an event which occurs on the database. Collection of operations that form a single
logical unit of work are called transaction.
A transaction is one or more SQL statements that make up a unit of work performed against the
database, and either all the statements in a transaction are committed as a unit or all the statements
are rolled back as a unit.
This unit of work typically satisfies a user request and ensures data integrity. For example, when you
use a computer to transfer money from one bank account to another, the request involves a
transaction: updating values stored in the database for both accounts.
T1: Read A;
A = A – 100;
Write A;
Read B;
B = B + 100;
Write B;
The Four Properties of Transactions every transaction, for whatever purposes it is being used ,has
the following four properties. Taking the initial letters of these four properties we collectively call
them the ACID Properties.
1|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
Eg: Suppose before execution of transaction Ti the values of accounts A & B are 1000$ and 2000$
respectively. Now suppose during execution of transaction Ti, a failure occurs. Consider a failure
occurs after write(A) operation but before write(B), then the values reflected in the database are
950$ and 2000$. Thus,the sum A+B no longer preserved. To avoid this, atomicity should be ensured.
To ensure atomicity database system keeps track of the old values of any data on which a
transaction performs a write. If the transaction does not complete its execution, the database
restores the old values. Atomicity is handled by transaction management component.
2) CONSISTENCY: The database must remain in a consistent state after any transaction. No
transaction should have any adverse effect on the data residing in the database. If the database was
in a consistent state before the execution of a transaction, it must remain consistent after the
execution of the transaction as well.
Eg: Transaction T1 transfers $100 from Account A to Account B. Both Account A and Account B
contains $500 each before the transaction.
Transaction T1
Read (A)
A=A-100
Write (A)
Read (B)
B=B+100
Write (B)
Consistency Constraint:
Before Transaction execution Sum = A + B Sum = 500 + 500 Sum = 1000
After Transaction execution Sum = A + B Sum = 400 + 600 Sum = 1000
Before the execution of transaction and after the execution of transaction SUM must be equal.
3) ISOLATION: In a database system where more than one transaction are being executed
simultaneously and in parallel, the property of isolation states that all the transactions will be carried
out and executed as if it is the only transaction in the system. No transaction will affect the existence
of any other transaction.
Eg: The database is said to be temporarily inconsistent while the transaction of transfer funds from A
to B is executing and at the same time second concurrently running transaction reads at this
intermediate point and computes A+B it will observe an inconsistent value which results in
inconsistent state even after both transactions have completed. To avoid this problem of concurrent
execution, transactions should be executed in isolation. The isolation property of a transaction
ensures that the concurrent execution of transaction results in a system state that could have been
obtained if transactions are executed serially i.e one after the other.
4) DURABILITY:Durability ensures that any transaction committed to the database will not be
lost.Durability is ensured through the use of database backups and transaction logs that facilitate the
restoration of committed transactions in spite of any subsequent software or hardware failures. The
database should be durable enough to hold all its latest updates even if the system fails or restarts. If
a transaction updates a chunk of data in a database and commits, then the database will hold the
2|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
modified data. If a transaction commits but the system fails before the data could be written on to
the disk, then that data will be updated once the system springs back into action.
The following are the different states in transaction processing in a Database System.
1. Active: This is the initial state. The transaction stay in this state while it is executing.
2. Partially Committed: This is the state after the final statement of the transaction is executed.
3. Failed: After the discovery that normal execution can no longer proceed.
5. 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.
5. Committed: The state after successful completion of the transaction. We cannot abort or rollback
a committed transaction.
1) Transaction identifier : is the unique identifier of the transaction that performs the write
operation.
2) Data item identifier: is the unique identifier of the data item written.
3) Old value : is the value of the data item prior to the write.
4) New value : is the value of the data item that it will have after the write.
It is represented as :
<Ti start> : Transaction Ti is started
<Ti, Xj, V1, V2> : Transaction Ti has performed a write on data item Xj, Xj had value V1 before the
write and will have value V2 after the write.
<Ti commit> : Transaction Ti has committed
<Ti abort> : Transaction Ti has aborted
3|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
Transaction control commands manage changes made by DML commands. The following describes
the nature of transactions:
All transactions have a beginning and an end.
A transaction can be saved or undone.
If a transaction fails in the middle, no part of the transaction can be saved to the database.
Transactional control is the ability to manage various transactions that may occur within a relational
database management system. There are three commands used to control transactions :
i) Commit ii)Rollback iii)Savepoint
1) COMMIT : The commit command saves all transactions to the database since the last
COMMIT or ROLLBACK command happens.
Syntax : commit[work];
The keyword commit is the only mandatory part of the syntax. Keyword work is optional.
Eg: SQL> delete from emp
Where emp_age>60;
This deletes the record of those employee whose age is above 60years. Though the changes
are reflected on database they are not actually saved in database, rather they are stored in
temporary area. To store changes permanently in the database commit command is used.
SQL>commit work;
The above command will made changes permanently in database, since last commit or
rollback command was issued.
2) ROLLBACK:The rollback command is the transactional control command used to undo
transactions that have not already been saved to the database. The rollback command can
only be used to undone the transactions since the last COMMIT or ROLLBACK command was
issued.
Syntax : rollback[work];
The keyword rollback is the only mandatory part of the syntax. Keyword work is optional.
Eg: SQL> delete from emp
Where emp_age>60;
This deletes the record of those employee whose age is above 60years. Though the changes
are reflected on database they are not actually saved in database, rather they are stored in
temporary area. To discard changes permanently in the database commit command is used.
SQL>rollback work;
The above command will discard changes made on database, since last commit or rollback
command was issued.
3) SAVEPOINT:Savepoints offer a mechanism to rollback portions of transactions.
General eg :Consider that a person walking and after passing some distance the road got split
into two tracks. The person is not sure to choose which track, so before randomly selecting
one track he will make a signal flag, so that if the track was not the right one he can rollback
to signal flag and select the right track. In this example the signal flag becomes the savepoint.
Syntax :savepointname_of_savepoint;
4|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
Eg: Before deleting the records of employee we should create a savepoint and then use
delete command to delete employee records.
SQL>savepointDeleteEmp;
SQL>delete from emp
Where emp_age>60;
After some time, if manager orders to not delete employer’s record the changes can undone
by using the rollback to savepoint command.
SQL>rollback to DeleteEmp;
It willrollback the changes made to Emp table.
Final value of A=450. The credit of T1 is missing (lost update) from the account.
5|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
T1(Credit) T2(Debit)
Read(A) {A=500}
A:A+100 {A=600}
Write(A) {A=600}
T1 failed to complete Read(A) {A=500}
A:A+100 {A=600}
Write(A) {A=600}
Eg: Here T1 and T2 reads A=500. Now, T1 modifies A to 600 when, T2 reads it gets value of
600 this should not be the case because T2 in the same execution should get only one value
of A (500 or 600 but not both) which is happening after the commit.
SCHEDULAR:
When multiple transactions are executing concurrently, then the order of execution of operations
from the various transactions is known as schedule.
1) Serial Schedule 2) Non-Serial Schedule
Serial Schedule: Transactions are executed one by one without any interleaved operations from
other transactions.
Non-Serial Schedule: A schedule where the operations from a set of concurrent transactions are
interleaved.
SERIALIZABILITY:
What is Serializability?
A given non serial schedule of n transactions is serializable,if it is equivalent to some serial schedule.
i.e. this non serial schedule produce the same result as of the serial schedule. Then the given non
serial schedule is said to be serializable. A schedule that is not serializable is called a non-serializable.
6|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
7|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
locks. As soon as the transaction releases its first lock, the third phase starts. In this phase,
the transaction cannot demand any new locks; it only releases the acquired locks.
Eg : lock X(B);
Read B;
B: = B – 100;
Write B;
lock X(A);
Read A;
A: = A + 100;
Write A;
unlock(B);
unlock(A);
Two-phase locking has two phases: one is growing, where all the locks are being acquired
by thetransaction; and the second phase is shrinking, where the locks held by the
transaction are being released. To claim an exclusive (write) lock, a transaction must first
acquire a shared (read) lock and then upgrade it to an exclusive lock.
iv)Strict Two-Phase Locking:The first phase of Strict-2PL is same as 2PL. After acquiring all
the locks in the first phase, the transaction continues to execute normally. But in contrast to
2PL, Strict-2PL does not release a lock after using it. Strict-2PL holds all the locks until the
commit point and releases all the locks at a time.Strict-2PL does not have cascading abort as
2PL does.
8|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
Thomas' Write Rule: This rule states if TS(Ti) < W-timestamp(X), then the operation is rejected
and Ti is rolled back. Timestamp ordering rules can be modified to make the schedule view
serializable. Instead of making Ti rolled back, the 'write' operation itself is ignored.
Allow data items to be of various sizes and define a hierarchy of data granularities, where
the small granularities are nested within larger ones.Can be represented graphically as a tree
(but don't confuse with tree-locking protocol).When a transaction locks a node in the tree
explicitly, it implicitly locks all the node's descendents in the same mode.
The levels, starting from the coarsest (top) level aredatabase,area,file and record.
9|Page
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
5.7. DEADLOCKS:
In a multi-process system, deadlock is an unwanted situation that arises in a shared resource
environment, where a process indefinitely waits for a resource that is held by another process.
For example, assume a set of transactions {T0, T1, T2, ...,Tn}. T0 needs a resource X to complete its
task. Resource X is held by T1, and T1 is waiting for a resource Y, which is held by T2. T2 is waiting for
resource Z, which is held by T0. Thus, all the processes wait for each other to release resources. In
this situation, none of the processes can finish their task. This situation is known as a deadlock.
Deadlocks are not healthy for a system. In case a system is stuck in a deadlock, the transactions
involved in the deadlock are either rolled back or restarted.
Deadlock Prevention:
To prevent any deadlock situation in the system, the DBMS aggressively inspects all the operations,
where transactions are about to execute. The DBMS inspects the operations and analyzes if they can
create a deadlock situation. If it finds that a deadlock situation might occur, then that transaction is
never allowed to be executed.
There are deadlock prevention schemes that use timestamp ordering mechanism of transactions in
order to predetermine a deadlock situation.
i)Wait-Die Scheme: In this scheme, if a transaction requests to lock a resource (data item), which is
already held with a conflicting lock by another transaction, then one of the two possibilities may
occur:
If TS(Ti) < TS(Tj) — that is Ti, which is requesting a conflicting lock, is older than Tj — then Ti
is allowed to wait until the data-item is available.
If TS(Ti) > TS(tj) — that is Ti is younger than Tj — then Ti dies. Ti is restarted later with a
random delay but with the same timestamp. This scheme allows the older transaction to
wait but kills the younger one.
ii)Wound-Wait Scheme: In this scheme, if a transaction requests to lock a resource (data item),
which is already held with conflicting lock by another transaction, one of the two possibilities may
occur:
If TS(Ti) < TS(Tj), then Ti forces Tj to be rolled back — that is Ti wounds Tj. Tj is restarted
later with a random delay but with the same timestamp.
If TS(Ti) > TS(Tj), then Ti is forced to wait until the resource is available. This scheme allows
the younger transaction to wait; but when an older transaction requests an item held by a
younger one, the older transaction forces the younger one to abort and release the item. In
both the cases, the transaction that enters the system at a later stage is aborted.
10 | P a g e
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
Deadlock Avoidance:
Aborting a transaction is not always a practical approach. Instead, deadlock avoidance mechanisms
can be used to detect any deadlock situation in advance. Methods like "wait-for graph" are available
but they are suitable for only those systems where transactions are lightweight having fewer
instances of resource. In a bulky system, deadlock prevention techniques may work well.
Wait-for Graph:This is a simple method available to track if any deadlock situation may arise. For
each transaction entering into the system, a node is created. When a transaction Ti requests for a
lock on an item, say X, which is held by some other transaction Tj, a directed edge is created from Ti
to Tj. If Tj releases item X, the edge between them is dropped and Ti locks the data item. The system
maintains this wait-for graph for every transaction waiting for some data items held by others. The
system keeps checking if there's any cycle in the graph.
T1 T2
1) Transaction failure:
Logical errors: transaction cannot complete due to some internal error condition.
System errors: the database system must terminate an active transaction due to an error
condition (e.g., deadlock)
2) System crash: a power failure or other hardware or software failure causes the system to crash.
Fail-stop assumption: non-volatile storage contents are assumed to not be corrupted by system
crash. Database systems have numerous integrity checks to prevent corruption of disk data
3) Disk failure: a head crash or similar disk failure destroys all or part of disk storage
11 | P a g e
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
To ensure atomicity despite failures, we first output information describing the modifications to
stable storage without modifying the database itself. We study two approaches:
12 | P a g e
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
Idea: maintain two page tables during the lifetime of a transaction –the current page table,
and the shadow page table. Store the shadow page table in nonvolatile storage, such that
state of the database prior to transaction execution may be recovered. Shadow page table is
never modified during execution
To start with, both the page tables are identical. Only current page table is used for data
item accesses during execution of the transaction. Whenever any page is about to be written
for the first time
A copy of this page is made onto an unused page.
The current page table is then made to point to the copy
The update is performed on the copy
Advantages of shadow-paging over log-based schemes:
o no overhead of writing log records
o recovery is trivial
CHECKPOINT:
Keeping and maintaining logs in real time and in real environment may fill out all the memory space
available in the system. As time passes, the log file may grow too big to be handled at all. Checkpoint
is a mechanism where all the previous logs are removed from the system and stored permanently in
a storage disk. Checkpoint declares a point before which the DBMS was in consistent state, and all
the transactions were committed.
When a system with concurrent transactions crashes and recovers, it behaves in the following
manner:
The recovery system reads the logs backwards from the end to the last checkpoint.
It maintains two lists, an undo-list and a redo-list.
If the recovery system sees a log with <Tn, Start> and <Tn, Commit> or just <Tn, Commit>, it
puts the transaction in the redo-list.
If the recovery system sees a log with <Tn, Start> but no commit or abort log found, it puts
the transaction in the undo-list. All the transactions in the undo-list are then undone and
their logs are removed. All the transactions in the redo-list and their previous logs are
removed and then redone before saving their logs.
REMOTE BACKUP:
Remote backup provides a sense of security in case the primary location where the database is
located gets destroyed. Remote backup can be offline or realtime or online. In case it is offline, it is
maintained manually.
13 | P a g e
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
Online backup systems are more real-time and lifesavers for database administrators and
investors. An online backup system is a mechanism where every bit of the real-time data is
backed up simultaneously at two distant places. One of them is directly connected to the
system and the other one is kept at a remote place as backup.
As soon as the primary database storage fails, the backup system senses the failure and
switches the user system to the remote storage. Sometimes this is so instant that the users
can’t even realize a failure.
ARIES:
ARIES is a state of the art recovery method
i)Incorporates numerous optimizations to reduce overheads during normal processing
and to speed up recovery
ii)Unlike the advanced recovery algorithm, ARIES
Uses log sequence number (LSN) to identify log records
Stores LSNs in pages to identify what updates have already been applied to a
database page.
Physiological redo.
Dirty page table to avoid unnecessary redos during recovery
14 | P a g e
5. TRANSACTION MANAGEMENT & CONCURRENCY CONTROL
SQL>CONNECT "USERNAME"/"PASSWORD";
EXAMPLE:
CREATING A USER
SQL>CONNECT SYSTEM/MANAGER;
SQL>CREATE USER CSE2 IDENTIFIED BY CSECSE;
SQL>GRANT DBA TO CSE2;
SQL>CONNECT CSE2/CSECSE;
SQL>REVOKE DBA FROM CSE2;
15 | P a g e