RDBMS Unit2
RDBMS Unit2
Introduction to
Transaction concepts
Unit 2
Transaction
A transaction is a logical unit of work that contains one or more
SQL statements
A transaction can be defined as a group of tasks. A single task is
the minimum processing unit which cannot be divided further.
1
9/18/2019
A’s Account
Let’s take an example of a
simple transaction.
Open_Account(A)
Suppose a bank employee Old_Balance = A.balance
transfers Rs 500 from A's New_Balance = Old_Balance - 500
account to B's account. This A.balance = New_Balance
very simple and small Close_Account(A)
transaction involves several
low-level tasks. B’s Account
Open_Account(B)
Old_Balance = B.balance
New_Balance = Old_Balance + 500
Close_Account(B)
ACID Properties
2
9/18/2019
Atomicity
This property states that a transaction must be treated as an
atomic unit, that is, either all of its operations are executed or
none.
There must be no state in a database where a transaction is left
partially completed.
States should be defined either before the execution of the
transaction or after the execution/abortion/failure of the
transaction.
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.
3
9/18/2019
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.
For example, in an application that transfers funds from one
account to another, the isolation property ensures that another
transaction sees the transferred funds in one account or the other,
but not in both, nor in neither.
Durability
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 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.
4
9/18/2019
5
9/18/2019
Lock-based Protocols
Database systems equipped with lock-based protocols use a
mechanism by which any transaction cannot read or write data
until it acquires an appropriate lock on it.
Locks are of two kinds −
Binary Locks − A lock on a data item can be in two states; it is
either locked or unlocked.
Shared/exclusive − This type of locking mechanism
differentiates the locks based on their uses.
If a lock is acquired on a data item to perform a write operation, it
is an exclusive lock.
Allowing more than one transaction to write on the same data
item would lead the database into an inconsistent state.
Read locks are shared because no data value is being changed.
Prepared by Shimi Biju 12
6
9/18/2019
7
9/18/2019
Lock protocols
There are four types of lock protocols available −
1. Simplistic Lock Protocol
2. Pre-claiming Lock Protocol
3. Two-Phase Locking 2PL
4. Strict Two-Phase Locking
8
9/18/2019
9
9/18/2019
Timestamp-based Protocols
10
9/18/2019
Deadlock
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.
Deadlock Prevention
11
9/18/2019
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 −
This scheme allows the older transaction to wait but kills the younger
one.
Wound-Wait Scheme
In this scheme, if a transaction requests to lock a resource (data item), which is
already held with conflicting lock by some 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.
12
9/18/2019
Deadlock Avoidance
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.
13
9/18/2019
14
9/18/2019
Serializability
When multiple transactions are running concurrently then there is
a possibility that the database may be left in an inconsistent state.
Serializability is a concept that helps us to check
which schedules are serializable.
A serializable schedule is the one that always leaves the database
in consistent state.
A serial schedule doesn’t allow concurrency, only one transaction
executes at a time and the other starts when the already running
transaction finished.
Types of Serializability
There are two types of Serializability.
1. Conflict Serializability
2. View Serializability
15
9/18/2019
Conflict Serializability
IT is one of the type of Serializability, which can be used to check
whether a non-serial schedule is conflict serializable or not.
What is Conflict Serializability?
A schedule is called conflict serializable if we can convert it into
a serial schedule after swapping its non-conflicting operations.
Conflicting operations
Two operations are said to be in conflict, if they satisfy all the
following three conditions:
1. Both the operations should belong to different transactions.
2. Both the operations are working on same data item.
3. At least one of the operation is a write operation.
16
9/18/2019
If we can prove that the given schedule is View Equivalent to its serial
schedule then the given schedule is called view Serializable.
Prepared by Shimi Biju 34
17
9/18/2019
View Equivalent
Two schedules T1 and T2 are said to be view equivalent, if they satisfy
all the following conditions:
1. Initial Read: Initial read of each data item in transactions must match
in both schedules. For example, if transaction T1 reads a data item X
before transaction T2 in schedule S1 then in schedule S2, T1 should
read X before T2.
Read vs Initial Read: Here initial read means the first read operation
on a data item, for example, a data item X can be read multiple times
in a schedule but the first read operation on X is called the initial read.
1. Final Write: Final write operations on each data item must match in
both the schedules. For example, a data item X is last written by
Transaction T1 in schedule S1 then in S2, the last write operation on X
should be performed by the transaction T1.
2. Update Read: If in schedule S1, the transaction T1 is reading a data
item updated by T2 then in schedule S2, T1 should read the value after
the write operation of T2 on same data item. For example, In schedule
S1, T1 performs a read operation on X after the write operation on X
by T2 then in S2, T1 should read the X after T2 performs write on X.
Prepared by Shimi Biju 36
18
9/18/2019
View Serializable
19
9/18/2019
Final Write
In schedule S1, the final write operation on X is done by
transaction T2. In S2 also transaction T2 performs the final write
on X.
Lets check for Y. In schedule S1, the final write operation on Y is
done by transaction T2. In schedule S2, final write on Y is done
by T2.
We checked for both data items X & Y and the final
write condition is satisfied in S1 & S2.
20
9/18/2019
Update Read
In S1, transaction T2 reads the value of X, written by T1. In S2,
the same transaction T2 reads the X after it is written by T1.
In S1, transaction T2 reads the value of Y, written by T1. In S2,
the same transaction T2 reads the value of Y after it is updated by
T1.
The update read condition is also satisfied for both the schedules.
Result: Since all the three conditions that checks whether the two
schedules are view equivalent are satisfied in this example, which
means S1 and S2 are view equivalent.
Also, as we know that the schedule S2 is the serial schedule of
S1, thus we can say that the schedule S1 is view serializable
schedule.
21
9/18/2019
Transaction Control
22
9/18/2019
Example
The syntax for the COMMIT command is as follows.
COMMIT;
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Prepared by Shimi Biju 45
Following is an example which would delete those records from the table which
have age = 25 and then COMMIT the changes in the database.
SQL> DELETE FROM CUSTOMERS WHERE AGE = 25;
SQL> COMMIT;
Thus, two rows from the table would be deleted and the SELECT statement would
produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
23
9/18/2019
Example
Consider the CUSTOMERS table having the following records −
| ID | NAME | AGE | ADDRESS | SALARY |
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Prepared by Shimi Biju 47
24
9/18/2019
Thus, the delete operation would not impact the table and the SELECT statement
would produce the following result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
25
9/18/2019
example
Delete the three different records from the CUSTOMERS table. You want to
create a SAVEPOINT before each delete, so that you can ROLLBACK to any
SAVEPOINT at any time to return the appropriate data to its original state.
Example
Consider the CUSTOMERS table having the following records.
26
9/18/2019
Now that the three deletions have taken place, let us assume that you have decided to
ROLLBACK to the SAVEPOINT that you identified as SP2. Because SP2 was created after
the first deletion, the last two deletions are undone −
SQL> ROLLBACK TO SP2;
Rollback complete.
Notice that only the first deletion took place since you rolled back to SP2.
27
9/18/2019
28