Unit-4 Transactions
Unit-4 Transactions
Transactions
Transaction
1.The transaction is a set of logically related operation. It contains a
group of tasks.
X's Account
Open_Account(X)
Old_Balance = X.balance
New_Balance = Old_Balance - 800
X.balance = New_Balance
Close_Account(X)
Y's Account
Open_Account(Y)
Old_Balance = Y.balance
New_Balance = Old_Balance + 800
Y.balance = New_Balance
Close_Account(Y)
Operations of Transaction:
Following are the main operations of transaction:
1. R(X);
2. X = X - 500;
3. W(X);
o The first operation reads X's value from database and stores it in
a buffer.
o The second operation will decrease the value of X by 500. So
buffer will contain 3500.
o The third operation will write the buffer's value to the database.
So X's final value will be 3500.
Active state
o The active state is the first state of every transaction. In this state,
the transaction is being executed.
o Example: Insertion or deletion or updating a record is done here.
But all the records are still not saved to the database.
Partially committed
o In the partially committed state, a transaction executes its final
operation, but the data is still not saved to the database.
o In the total mark calculation example, a final display of the total
marks step is executed in this state.
Committed
Failed state
o If any of the checks made by the database recovery system fails,
then the transaction is said to be in the failed state.
o In the example of total mark calculation, if the database is not able
to fire a query to fetch the marks, then the transaction will fail to
execute.
Aborted
o If any of the checks fail and the transaction has reached a failed
state then the database recovery system will make sure that the
database is in its previous consistent state. If not then it will abort
or roll back the transaction to bring the database into a consistent
state.
o If the transaction fails in the middle of the transaction then before
executing the transaction, all the executed transactions are rolled
back to its consistent state.
o After aborting the transaction, the database recovery module will
select one of the two operations:
1. Re-start the transaction
2. Kill the transaction
Transaction Property
The transaction has the four properties. These are used to maintain
consistency in a database, before and after the transaction.
Property of Transaction
1. Atomicity
2. Consistency
3. Isolation
4. Durability
Atomicity:
Abort: If a transaction aborts then all the changes made are not visible.
T1 T2
Read(A) Read(B)
A:=A-100 Y:=Y+100
Write(A) Write(B)
Consistency
Durability
3.Phantom Read: When the two queries are executed, but they retrieve
the two rows differently, at that time, phantom Read occurs. Let's take
an example. Suppose transaction 1 retrieves a set of rows, and these
rows are satisfied by some conditions. Now transaction 2 generates
some new rows that match the search criteria of transaction 1. If
transaction 1 executes the statement that reads the row, it gets a
different row at different times.
Isolation Levels
Based on the above different phenomena, SQL defines into four
isolation levels.
Example
Consider an example of isolation.
session begins
transaction A
transaction B
SET TRANSACTION
transaction C
SET TRANSACTION
transaction D
transaction E session ends
A- Serializable
B- Repeatable read
C- Read uncommitted
Schedule
A series of operation from one transaction to another transaction is
known as schedule. It is used to preserve the order of the operation in
each of the individual transaction.
1. Serial Schedule
2. Non-Serial Schedule
3. Serializable schedule
Testing of Serializability:
Serialization Graph is used to test the Serializability of a schedule.
Example:
Explanation:
Conflicting Operations
Example:
Swapping is possible only if S1 and S2 are logically equal.
Conflict Equivalent
In the conflict equivalent, one can be transformed to another by
swapping non-conflicting operations. In the given example, S2 is
conflict equivalent to S1 (S1 can be converted to S2 by swapping non-
conflicting operations).
T1 T2
Read(A)
Write(A)
Read(B)
Write(B)
Read(A)
Write(A)
Read(B)
Write(B)
View Equivalent:
Two schedules S1 and S2 are said to be view equivalent if they satisfy
the following conditions:
1. Initial Read:
An initial read of both schedules must be the same. Suppose two
schedule S1 and S2. In schedule S1, if a transaction T1 is reading the
data item A, then in S2, transaction T1 should also read A.
Above two schedules are view equivalent because Initial read operation
in S1 is done by T1 and in S2 it is also done by T1.
2. Updated Read:
In schedule S1, if Ti is reading A which is updated by Tj then in S2
also, Ti should read A which is updated by Tj.
Above two schedules are not view equal because, in S1, T3 is reading
A updated by T2 and in S2, T3 is reading A updated by T1.
3. Final Write
A final write must be the same between both the schedules. In schedule
S1, if a transaction T1 updates A at last then in S2, final writes
operations should also be done by T1.
Schedule S
= 3! = 6
S1 = <T1 T2 T3>
S2 = <T1 T3 T2>
S3 = <T2 T3 T1>
S4 = <T2 T1 T3>
S5 = <T3 T1 T2>
S6 = <T3 T2 T1>
In both schedules S and S1, there is no read except the initial read that's
why we don't need to check that condition.
T1 → T2 → T3
Recoverability of Schedule:
Sometimes a transaction may not execute completely due to a software
issue, system crash or hardware failure. In that case, the failed
transaction has to be rollback. But some other transaction may also
have used value produced by the failed transaction. So we also have to
rollback those transactions.
Failure Classification:
To find that where the problem has occurred, we generalize a failure
into the following categories:
1. Transaction failure
2. System crash
3. Disk failure
1.Transaction failure
The transaction failure occurs when it fails to execute or when it
reaches a point from where it can't go any further. If a few transaction
or process is hurt, then this is called as transaction failure.
2. System Crash:
3. Disk Failure:
Log-Based Recovery:
1.The log is a sequence of records. Log of each transaction is
maintained in some stable storage so that if any failure occurs, then it
can be recovered from there.
3.But the process of storing the logs should be done before the actual
transaction is applied in the database.
<Tn, Start>
o When the transaction modifies the City from 'Noida' to
'Bangalore', then another log is written to the file.
<Tn, Commit>
There are two approaches to modify the database:
1. If the log contains the record <Ti, Start> and <Ti, Commit> or
<Ti, Commit>, then the Transaction Ti needs to be redone.
2. If log contains record<Tn, Start> but does not contain the record
either <Ti, commit> or <Ti, abort>, then the Transaction Ti needs
to be undone.
Checkpoint
o The checkpoint is a type of mechanism where all the previous
logs are removed from the system and permanently stored in the
storage disk.
o The checkpoint is like a bookmark. While the execution of the
transaction, such checkpoints are marked, and the transaction is
executed then using the steps of the transaction, the log files will
be created.
o When it reaches to the checkpoint, then the transaction will be
updated into the database, and till that point, the entire log file
will be removed from the file. Then the log file is updated with
the new step of transaction till next checkpoint and so on.
o The checkpoint is used to declare a point before which the DBMS
was in the consistent state, and all transactions were committed.
Concurrency Control
Concurrency Control is the working concept that is required for
controlling and managing the concurrent execution of database
operations and thus avoiding inconsistencies in the database. Thus, for
maintaining the concurrency of the database, we have the concurrency
control protocols.
Lock-Based Protocol
In this type of protocol, any transaction cannot read or write data until
it acquires an appropriate lock on it. There are two types of lock:
1. Shared lock:
o It is also known as a Read-only lock. In a shared lock, the data
item can only be read by the transaction.
o It can be shared between the transactions because when the
transaction holds a lock, then it can't update the data on the data
item.
2. Exclusive lock:
o In the exclusive lock, the data item can be both read as well as
written by the transaction.
o This lock is exclusive, and in this lock, multiple transactions do
not modify the same data simultaneously.
Growing phase: In the growing phase, a new lock on the data item
may be acquired by the transaction, but none can be released.
Shrinking phase: In the shrinking phase, the existing lock held by the
transaction may be released, but no new locks can be acquired.
The following way shows how unlocking and locking work with 2-PL.
Transaction T1:
Transaction T2:
Where,
Validation (Ti): It contains the time when Ti finishes its read phase
and starts its validation phase.
If we use the Thomas write rule then some serializable schedule can be
permitted that does not conflict serializable as illustrate by the
schedule in a given figure:
Figure: A Serializable Schedule that is not Conflict Serializable
In the above figure, T1's read and precedes T1's write of the same data
item. This schedule does not conflict serializable.
Thomas write rule checks that T2's write is never seen by any
transaction. If we delete the write operation in transaction T2, then
conflict serializable schedule can be obtained which is shown in below
figure.
Multiple Granularity:
o It can be defined as hierarchically breaking up the database into
blocks that can be locked.
o The Multiple Granularity protocol enhances concurrency and
reduces lock overhead.
o It maintains the track of what to lock and how to lock.
o It makes it easy to decide either to lock a data item or to unlock a
data item. This type of hierarchy can be graphically represented
as a tree.
In this example, the highest level shows the entire database. The levels
below are file, record, and fields.
Deadlock in DBMS:
A deadlock is a condition where two or more transactions are waiting
indefinitely for one another to give up locks. Deadlock is said to be one
of the most feared complications in DBMS as no task ever gets finished
and is in a waiting state forever.
Deadlock Detection
In a database, when a transaction waits indefinitely to obtain a lock,
then the DBMS 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.
Wait for Graph
The wait for a graph for the above scenario is shown below:
Deadlock Prevention:
Wait-Die scheme:
In this scheme, if a transaction requests for a resource which is already
held with a conflicting lock by another transaction then the DBMS
simply checks the timestamp of both transactions. It allows the older
transaction to wait until the resource is available for execution.
Let's assume there are two transactions Ti and Tj and let TS(T) is a
timestamp of any transaction T. If T2 holds a lock by some other
transaction and T1 is requesting for resources held by T2 then the
following actions are performed by DBMS:
o In a multi-user system, multiple users can access and use the same
database at one time, which is known as the concurrent execution
of the database. It means that the same database is executed
simultaneously on a multi-user system by different users.
o While working on the database transactions, there occurs the
requirement of using the database by multiple users for
performing different operations, and in that case, concurrent
execution of the database is performed.
o The thing is that the simultaneous execution that is performed
should be done in an interleaved manner, and no operation should
affect the other executing operations, thus maintaining the
consistency of the database. Thus, on making the concurrent
execution of the transaction operations, there occur several
challenging problems that need to be solved.
Consider the below diagram where two transactions TX and TY, are
performed on the same account A where the balance of account A
is $300.
Example:
Consider two transactions TX and TY in the below diagram
performing read/write operations on account A where the
available balance in account A is $300:
o At time t1, transaction TX reads the value of account A, i.e., $300.
o At time t2, transaction TX adds $50 to account A that becomes
$350.
o At time t3, transaction TX writes the updated value in account A,
i.e., $350.
o Then at time t4, transaction TY reads account A that will be read
as $350.
o Then at time t5, transaction TX rollbacks due to server problem,
and the value changes back to $300 (as initially).
o But the value for account A remains $350 for transaction TY as
committed, which is the dirty read and therefore known as the
Dirty Read Problem.