Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

Unit – IV SWC College

UNIT – IV

The transaction log:-

 A DBMS uses a transaction log to keep track of all transactions that update the
database.
 The transaction log is a buffer and it stores the following information:
a) A record for the beginning of the transaction.
b) For each transaction component (SQL statement) the information is stored like
i. The type of operation being performed (update,delete,insert)
ii. The names of the objects affected by the transaction (The name of the
table)
iii. The before and after values for the fields being updated.
c) The ending (COMMIT) of the transaction.

 The information stored in this log is used by DBMS when a system failure occurred or
abnormal termination of a program or network failure or disk crash etc.
 When recovery process is completed, the DBMS will write in the log all committed
transactions.
 Committed transactions are not rolled back.
 Although using a transaction log increase the processing overhead of a DBMS but it
restores a corrupted database.
****************************************************************************
SRIDHARA JAYA SUDHA
Unit – IV (chapter – I)

Concurrency Control:
 In multi user database system many transactions are executing simultaneously. The
coordination among those transactions is called concurrency control.
 The objective of concurrency control is to ensure the serializability of transaction in a
multi-user database environment.

Need for Concurrency control:


 Concurrency control is important because the simultaneous execution of transactions
can create several data integrity and consistency problems.
 The three main problems are of concurrent transactions are
a) Lost updates
b) Uncommitted data(dirty read)
c) Inconsistent data
a) Lost updates:-
 The Lost Updates problem occurs when two concurrent transactions T1 and T2 are
updating the same data element.
 For example let us examine PRODUCT table.
One of the attributes is QOH (Product’s quantity on hand).
Assume that we have a product and the value for QOH attribute is 35.
 Now two concurrent transactions T1 and T2 are occurring on QOH attribute for
update like as shown below.
Transaction Computation

T1:Purcahse 100 units QOH=QOH+100

T2:Sell 30 units QOH=QOH-30

 In the normal circumstances the serial execution of two transactions given below:
2
Unit – IV (chapter – I)

Transaction Step Stored value

T1 Read QOH 35

T1 QOH=35+100

T1 Write QOH 135

T2 Read QOH 135

T2 QOH=135-30

T2 Write QOH 105

 But when T1 and T2 concurrent transaction updating the same data element then lost

update problem can arise as shown below.

Transaction Step Stored value

T1 Read QOH 35

T2 Read QOH 35

T1 QOH=35+100

T2 QOH=35-30

T1 Write QOH 135

(Lost update)

T2 Write QOH 5

 The first transaction T1 has not yet bean committed when the second transaction (T2)

is executed.

b) Uncommitted data:-

3
Unit – IV (chapter – I)

 Uncommitted data occurs when two transactions T1 and T2 are executed


concurrently and the first transaction (T1) is rolled back after the second transaction
(T2) has already accessed the uncommitted data. Thus it is violating Isolation
property of transactions.
 For example T1 transaction is forced to rollback due to an error during the update
operation. So the transactions are as follows.
Transaction Computation
T1: Purchase 100 units --- QOH=QOH+100
(Rolled Back)
T2: Sell 30 units --- QOH=QOH-30

 In the normal circumstances the serial execution of those transactions is as follows:

Transaction Step Stored value

T1 Read QOH 35

T1 QOH=35+100

T1 Write QOH 135

Roll Back 35

T2 Read QOH 35

T2 QOH=135-30

T2 Write QOH 5

 But uncommitted data problem arises when T2 has begun before the ROLLBACK of T1.

 An uncommitted data problem is as follows

4
Unit – IV (chapter – I)

Transaction Step Stored value

T1 Read QOH 35

T1 QOH=35+100

T1 Write QOH 135

T2 Read QOH 135

(Read uncommitted data)

T2 QOH=135-30

T1 Roll Back 35

T2 QOH 105

c) Inconsistent retrievals:-
 Inconsistent retrievals occur when a transaction is accessing the data before another
transaction starts and accessing the data after finishing the another transaction.
 For example
1. T1 calculates the total quantity on hand of the products stored in the
PRODUCT table.
2. At the same time T2 updates the quantity on hand (QOH) for two products in
PRODUCT table
 The 2 transactions are as shown in below
Transaction (T1) Transaction (T2)
Select sum(QOH) from product; update product set QOH=QOH+10 where Pid=’P10’;
Update product set QOH=QOH-10
where Pid=’P14’;
commit;
 So in general circumstances results are as follows.
Pid Before After

QOH QOH

5
Unit – IV (chapter – I)

P1 8 8

P2 32 32

P10 15 15+10=25

P14 23 23-10=13

P18 8 8

P20 6 6

Total 92 92

 But an inconsistent retrieval can occur if transaction T1 is reading some data before
they are changed and other data after they are changed.
 The inconsistent retrieval is shown below:
Transaction Action Value Total

T1 Read QOH for Pid=’P1’ 8 8

T1 Read QOH for Pid=’P2’ 32 40

T2 Read QOH for Pid=’P10’

T2 QOH=15+10

T2 Write QOH for Pid=’P10’ 25

T1 Read QOH for Pid=’P10’ 25 (After) 65

T1 Read QOH for Pid=’P14’ 23 (Before) 88

T2 Read QOH for Pid=’P14’ 23

T2 QOH=23-10

T2 Write QOH for Pid=’P14’ 13

(Commit)

T1 Read QOH for Pid=’P18’ 96

6
Unit – IV (chapter – I)

T1 Read QOH for Pid=’P20’ 102

The Scheduler:-
 The Scheduler is a special DBMS process which is used to maintain serializability
and isolation of transactions.
Serializability:-
 When multiple transactions are executed, then the DBMS must schedule the
concurrent execution of the transaction’s operations. This property is reffred as
serializability.
 This property is important in multi-user and distributed databases.
 The scheduler’s main job is to create a serializable schedule of a transaction’s
operations.
 When transactions are non serializable then they are executed on first come, first
served basis by the DBMS. The problem with this approach is processing time of
CPU is wasted.
 The scheduler also facilitates data isolation to insure that two transactions do not
update the same data element at the same time.
 Suppose T1 and T2 are the two transactions executing concurrently on the same
data item, then in the following situations conflictions will occur.

Operation of T1 Operation of T2 Result


Read Read No conflict
Read Write Conflict
Write Read Conflict
Write Write conflict
****************************************************************************

7
Unit – IV (chapter – I)

View serializability:

 A schedule is view serializable if it is view equivalent to a serial schedule.

 For example,Two schedules S1 and S2 consisting of the same operations from n


transactions T1, T2, . . . , Tn are view equivalent if the following 3 conditions hold:

 • For each data item x, if transaction Ti reads the initial value of x in schedule S1,
then transaction Ti must also read the initial value of x in schedule S2.

 • For each read operation on data item x by transaction Ti in schedule S1, if the
value read by Ti has been written by transaction Tj , then transaction Ti must also
read the value of x produced by transaction Tj in schedule S2.

 • For each data item x, if the last write operation on x was performed by
transaction Ti in schedule S1, the same transaction must perform the final write on
data item x in schedule S2.

Recoverability:

 Serializability identifies schedules that maintain the consistency of the database,


assuming that none of the transactions in the schedule fails.

 An alternative perspective examines the recoverability of transactions within a


schedule. If a transaction fails, the atomicity property requires that we undo the
effects of the transaction.

 In addition, the durability property states that once a transaction commits, its
changes cannot be undone.

8
Unit – IV (chapter – I)

Concurrency Control with Locking Methods:-

 To avoid the problems of concurrent transactions the DBMS provides the following
methods:
1) Locking methods
2) Time stamping methods
3) Optimistic methods
 A transaction acquires a lock before accessing the data, when the transaction is
completed then the lock is released (unlock), so that another transaction can lock
the data item.
Ex: Suppose T1 and T2 are two transactions. T1 is accessing a data item, then
Transaction T2 cannot access that data item.
 All lock information is managed by a lock manager, which is responsible for
assigning and policing the locks used by the transactions.
Lock Types:-
 The DBMS use different lock types like
a) Binary Locks
b) Shared/Exclusive Locks.

a) Binary Locks:-

 A binary lock has two states:


a) Locked
b) Unlocked
 If a transaction is locking an object, then other transaction cannot use that object.
 If an object is unlocked, any other transaction can lock the object for its use.
 The object may be a database, table, page or row.
 Every transaction must lock the object, and then it performs operation.
 A transaction must unlock the object after its termination. Therefore every
transaction requires a lock and unlocks operation for each data item.
 The LOCK TABLE and other SQL commands are available to implement locking
mechanism.
9
Unit – IV (chapter – I)

 Using binary locks the lost update problem is eliminated in concurrency control
because the lock released until the WRITE statement is completed.
 But binary locks are considered as too restrictive.
For example the DBMS will not allow two transactions to read the same database
object even though neither transaction updates the database.

b) Shared/Exclusive Locks:-

 A shared lock exists when concurrent transactions are granted for read operation.
A common lock is used for concurrent transaction. A shared lock produces no
conflict as long as all the concurrent transactions are read only.
 An exclusive lock exists when transaction is granted for write operation. The
exclusive lock must be used when conflicts exists. (One transaction is READ and
other is WRITE).
 So a shared lock is issued when a transaction wants to read data from the database
and an exclusive lock is issued when a transaction wants to write a data item.
 Using Shared/Exclusive locking concept a lock can have 3 states
a) Unlocked
b) Shared (read)
c) Exclusive (write)
Examples for shared/Exclusive locks:
1) If transaction T1 has shared lock on data item X and transaction T2 wants to read
data item X. So T2 may also obtain a shared lock on data item X.
Read Read
Data Item X
T1 T2
Shared lock
2) If transaction T1 has Shared or Exclusive lock on data item X and transaction T2
wants an exclusive lock to update data item X. But an exclusive lock cannot be granted
to transaction T2 and it must wait till T1 is saved. So “The exclusive lock is granted if
and only if no other locks are held on the data item”.
Write unable to access
Data Item X
T1 T2

10
Unit – IV (chapter – I)

Exclusive lock
 Shared/Exclusive locks are more efficient but these increase the lock manager’s
overhead because of the following reason.
a) The type of the lock must be held before a lock is granted.
 Shared/Exclusive locks can lead to two major problems:
a) The resulting transaction schedule might not be serializable.
b) The schedule might create deadlock.
****************************************************************************

Two Phase Locking to ensure Serializability:-

 Two phase locking defines how transactions acquire locks.


 Two phase locking guarantees Serializability but it does not prevent dead locks.
 The two phases are
a) A growing phase in which a transaction acquires all required locks with out
unlocking any data. Once all locks have been acquired the transaction is in its locked
point.
b) A shrinking phase, in which a transaction releases all locks and can not obtain
any new lock.
 The two phase locking protocol is depicted as follows:

 In two phase locking protocol the transaction acquires all the locks until it reaches
its locked point. When the locked point is reached, the data are modified to
conform to the transaction requirements. Finally the transaction is completed, and
then it releases all the locks which are acquired.
 The two phase locking increase the transaction processing cost but the drawback is
it might create deadlocks.
*************************************************************************

11
Unit – IV (chapter – I)

Deadlocks in DBMS
 In a database, a deadlock is an unwanted situation in which 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 it brings the
whole system to a Halt.
 Example – let us understand the concept of Deadlock with an example :
Suppose, Transaction T1 holds a lock on some rows in the Students table and needs to
update some rows in the Grades table. Simultaneously, Transaction T2 holds locks on
those very rows (Which T1 needs to update) in the Grades table but needs to update the
rows in the Student table held by Transaction T1.

 Now, the main problem arises. Transaction T1 will wait for transaction T2 to give up lock,
and similarly transaction T2 will wait for transaction T1 to give up lock.
 As a consequence, all activity comes to a halt and remains at a standstill forever unless the
DBMS detects the deadlock and aborts one of the transactions.

Figure – Deadlock in DBMS


 So in real world DBMS, many transactions can be executed simultaneously, there
by increasing the probability of generating deadlocks.
 The 3 basic techniques to control deadlocks are
a) DeadLock Prevention:-
A transaction requesting a new lock is aborted when there is the possibility
that a dead lock can occur. This method is used when there is existing high
probability of deadlock.
b) DeadLock Detection:-

12
Unit – IV (chapter – I)

 The DBMS tests the database for deadlocks. If a deadlock is found, one of the
transactions is aborted and the other transaction continues.
 This method is used when there is less probability of dead locks.
c) DeadLock Avoidance:-
 The transaction must obtain all of the locks if needs before it can be executed. This
technique avoids the rollback of conflicting transactions.
*************************************************************************

Concurrency Control with Time Stamping Methods:-

 Time stamping methods are used to manage concurrency transaction execution.


 In time stamping approach, for each transaction unique time stamp assigned.
 Time stamps must have two properties.
a) Uniqueness: It specifies unique time stamp value exist, that means no equal
time stamp values can exist.
b) Monotonicity: It specifies that time stamp values always increase.
 The disadvantage of time stamping approach is that each values stored in the
database requires two additional time stamp fields. They are
a) One field is for the last time the field was read
b) Another field is for the last update.
 So time stamping increases memory needs and the database’s processing overhead.

Wait/die and Wound/wait Schemes:-

 The wait/die and wound/wait scheme are used in time stamping method.
Example:
 Assume that we have two conflicting transactions:
T1 and T2, each with a unique time stamp.
 Suppose T1 has time stamp of 1154 and T2 has a time stamp of 1956. So T1 is the
older transaction and T2 is newer (younger) transaction.

(i) Using the wait/die scheme:-

13
Unit – IV (chapter – I)

a) If the transaction requesting is the older of the two transactions, it will wait until
the other transactions is completed.
b) If the transaction requesting is the younger of the two transactions, it will die
(rollback) and is rescheduled using the same time stamp.

(ii) Using the wound/wait scheme:-

a) If the transaction requesting is the older of the two transactions, it will preempt
(wound) the younger transaction (by rolling it back). The younger transaction is
rescheduled using the same time stamp.
b) If the transaction requesting is the younger of the two transactions, it will wait until
the other transaction is completed.
 Wait/die scheme and wound/wait scheme are shown in the following table.
Transaction owing Transaction Wait/die scheme Wound/wait scheme
requesting
T2 (1956) T1 (1154) T1 waits until T2 is T1 preempts
completed (rollback) T2 and T2
is rescheduled using
the same time stamp
T1 (1154) T2 (1956) T2 dies (rollback) T2 waits until T1 is
and T2 is rescheduled completed.
using the same time
stamp

****************************************************************************

Concurrency Control with Optimistic Techniques/methods:-


14
Unit – IV (chapter – I)

 The optimistic approach is based on the assumption that the majority of the
database operations do not conflict.
The optimistic approach requires neither locking nor time stamping techniques.
 Using an optimistic approach, each transaction moves through three phases.
They are
a) Read Phase
b) Validation Phase
c) Write Phase.
 During the read phase, the transaction reads the database, executes the needed
computations, and makes the updates to a private copy of the database values. All
the update operations of the transaction are recorded in a temporary update file,
which is not accessed by the remaining transactions.
 During the validation phase the transaction is validated to ensure that the changes
made will not affect the integrity and consistency of the database.
 During the write phase, If the validation test is positive, the transaction goes to the
write operations. If the validation test is negative, the transaction is restarted and
the changes are discarded.
Multi version Timestamp Ordering:
 Versioning of data can also be used to increase concurrency, as different users may
work concurrently on different versions of the same object instead of having to
wait for each others’ transactions to complete.
 The basic timestamp ordering protocol discussed in the previous section assumes
that only one version of a data item exists, and so only one transaction can access a
data item at a time.
 This restriction can be relaxed if we allow multiple transactions to read and write
different versions of the same data item, and ensure that each transaction sees a
consistent set of versions for all the data items it accesses.
 In multi version concurrency control, each write operation creates a new version of
a data item while retaining the old version. When a transaction attempts to read a
data item, the system selects the correct version of the data item according to the
timestamp of the requesting transaction.

15
Unit – IV (chapter – I)

Granularity of Data items/Lock Granularity/Locking Levels:

 Lock granularity indicates the level of lock use. Locking can take place at the
following level.
a) Database level
b) Table level
c) Page level
d) Row level
e) Field (attribute level)
a) Database level:-
 In database level lock the entire database is locked. So if transaction T1 is accessing
that database, then transaction T2 can not access it.
Database
Table A

Table B

 This level of locking is good for batch processes but it is not suitable for multi user
DBMS. Because thousands of transactions had to wait for the previous transaction
is to be completed. So the data access would be slow.
Table level:-
 In table level lock the entire table is locked that means if transaction T1 is accessing
a table then transaction T2 can not access the same table.
Database
Table A

16
Unit – IV (chapter – I)

Table B

 If a transaction requires access to several tables, each table may be locked.


 Table level locks are less restrictive than database level locks.
 Table level locks are not suitable for multi-user DBMS.
 The draw back of table level lock is suppose transaction T1 and T2 can not access
the same table even when they try to use different rows; T2 must wait until T1
unlocks the table.
Page level:-
 In a page level lock, the DBMS will lock on entire disk page.
 A disk page is also referred as a disk block.
 A page has a fixed size such as 4k, 8k or 16k.
 A table can span several pages, and a page can contain several rows of one or more
tables.
 Page level locks are currently frequently used multi-user DBMS locking method.
 Page level lock is shown in the following fig.

Table A

Page 1
T1 is accessing
Page 2

T2 is accessing

 In the above fig. T1 and T2 access the same table while locking different disk pages.
 If T1 is locking page1 then T2 cannot access the rows in the page1 till T1 unlocks.
Row level:-
 A row level lock is much less restrictive than the other locks. The DBMS allows
concurrent transactions to access different rows of the same table.
 The row level locking approach improves the availability of data.

17
Unit – IV (chapter – I)

 But row level locking requires high overhead because a lock exist for each row in a
table of the database.
Field level:-
 The field level lock allows concurrent transactions to access the same row but
different fields (attributes) within that row.
 Although field level locking is the most flexible in multi user data access, but it is
rarely implemented in a DBMS because it requires an extreme High Level
computer overhead.

*************************************************************************
***

18

You might also like