You are on page 1of 28

DBMS UNIT-V

DBMS UNIT-V

UNIT-V

Transaction Management
Transaction: A Transaction is an execution of user program and is seen by DBMS as a series
(or) list of actions. Transaction starts and ends between the statements ‘begin Transaction’and
‘end Transaction’.

In a transaction,access to the database is performed by two

operations. 1.read(x)

2.write(x)

Read(x) performs the reading operation of data item x from the data base, write(x) perform
the writing operation of data item x to the data base.consider a Transaction ‘Ti’ which
transfer 100$ from account ‘A’ to account’ B’. this Transaction will be as follows.

Ti:=read(A);

A:=A-100;

Write(A);

Read(B);

B:=B+100;

Write(B);

ACID properties:

These are properties that a Transaction should poses in order to avoid failures during
concurrent access to a database.ACID stands for Atomicity,Consistency.Isolation and
Durability

Atomicity:

Atomicity ensures that the Transaction should either be executed completly (or) not at all.this
is because incomplete execution of the Transactions results in negative consequences.

Consistency:
DBMS UNIT-V

The data in the data base must always be in consistent state. A Transaction that is carried on a
consistent data should bring the data to another consistent state after execution.however,
transaction need not maintain consistency at intermediate stages.

Isolation: All Transaction must run in isolation from one another. That is every Transaction
should be kept unaware of other Transaction and execute independently.

Durability: This property ensures that data remains in a consistent state even after the failure.
This task is handled by recovery-management component of DBMS.

Transaction log:

Transaction log is a database component which is typically a table that stores all the
modifications done on the database. The information carried by this table(log) is used to
recover from critical situations caused by abnormalities such as system failure,network
failure, termination of a program etc... in case of such situations the DBMS uses the log to
roll back the Transaction that were failed to commit.

Transaction log is updated automatically for each modification and carries the following
fileds of information.

1. Captures the start of each Transaction

2. Captures the operation performed which may include SQL queries such as
update,delete,insert etc..

3. Captures the tables that are effected by these operations

4. Captures the value before and after the modifications.

5. captures the end of Transaction.

Transaction management with sql using commit,roll back and save point:

SQL follows certain predefined standard as suggested by ANSI (American National Standard
Institute) and therefore,each SQL query (or) Transaction must follow this standard.
Transactions are managed using commit and roll back statements with which one among the
following events is executed..
DBMS UNIT-V

1. in an SQL Transaction, if the sequence of steps reaches ‘commit’ statement ,it indicates that
the Transaction has been completed successfully. At this point, the modifications done are
saved permanently .

2. in case that if ‘roll back’ statement is reached instead of a ‘commit’ the Transaction is
considered as failed (or) aborted and all the modifications done are rolled back.

Example:

Update custemers set Name=”rob” where name=’ron’;

Update customers set bal=bal+1000 where cid=530;

Commit;

In this series of statements,,if the ‘commit’ is reached the modifications are


saved.however,there can be a situation whre the application might end successfully without
using a ‘commit’ statement.

For better understanding of complete Transaction, it is suggested to include ‘begin’ and


‘commit’ statements at the beginning and at the end respectively.

Apart from ‘commit’ and’ roll back’ SQL also includes a feature called ‘savepoint’ which
helps in rolling back only the failed operation. it is nothing but a check point at which the
modifications are saved whenever the Transaction reached it.when error occured after this
point,roll back is done with the operation performed after this point insted of rolling back
overall Transaction.

Syntax of a ‘savepoint’ statement is,

Savepoint<names of save point>

If a Transaction carry number of operatons,multiple save points can be used with different
names.in case of rollback operation,the following command is used with respect to the save
points,

Roll back to save point<name of the save point>


DBMS UNIT-V

Concurrency control:

Concurrency control is a DBMS mechanism that does not allows multiple Transactions to be
executed concurrently (I.e) at the same time.it provides an assurance about the Transactions
that ther are executed serially in the database. But, the concurrent execution of transaction
may lead to data consistency and integrity problems. The following are the different
problems associated with the concurrency control.

1. Lost updates

2. uncommited data

3.inconsistant data retrieval.

Lost update problem:

If two Transactions are executed concurrently and updating same attribute then some data
may be lost resulting in an incorrect data, such type of problem is known as ‘lost
update’problem.consider an employee table where in an attribute ‘emp_sal’exists,which is
updated by two Transactions t1 and t2.

T1 T2
Read emp_sal(7000)
Emp_sal=7000+500
Write emp_sal(7500);
Read emp_sal(7500)
Emp_sal=7500-100
Write emp_sal(7400);
Table(i):execution of Transactions T1 and T2 serially

In the above table. correct execution of Transactions t1 and t2 takes place,which is shown
below

1. initially ,Transaction t1 is executed,this Transaction performs a read operation as,

Read emp_sal(7000)

2. T1 updates emp_sal as,


DBMS UNIT-V

Emp_sal=7000+500

=7500

3. T1 performs a write operation and commits the

result, write emp_sal(7500)

4. Now the Transaction t2 performed. this Transaction performs a read operation with the
committed value as,

Read emp_sal(7500)

5. T2 updates the emp_sal

as, Emp_sal=7500-

100

=7400

6. T2 performs a write operation and then commits the final results as,

Write emp_sal=7400

Thus, the above serial execution of the Transaction produces the correct result.But, if a
Transaction is performed before committing a previous Transaction then loss of data may
occur.

T1 T2
Read emp_sal(7000)
Read emp_sal(7000);
Emp_sal=700+500
7500
Emp_sal=700-100
Write emp_sal(7500)
(Lost update)

Write emp_sal
DBMS UNIT-V

(7000-100=6900);
DBMS UNIT-V

Table(ii): lost update due to concurrent execution of Transactions

In the above table, the execution of Transactions T1 and T2 is done in the following way,

1. Transaction T1 performs a read operation


as, Read emp_sal(7000)

2. T2 performs a read operation as,

Read emp_sal(7000)

3.T1 updates the attributes value

as, Emp_sal=7000+500=7500

4.T2 updates the attributes value before the results of T1 is commited

Emp_sal=7000-100

5.T1 performs the write operation due to which lost of data

occurs Write emp_sal(7500)

6.T2 performs the write operation as

Write emp_sal(7000-100=6900)

Consequently, lost update problem of data occurs since,the result of table(i) (i,e 7400)
mismatches with the results of table(II)(i,e 6900).

Uncommitted data problem:

If two Transactions are executing concurrently and a rollback is performed by a Transaction


T1 after the Transaction T2 had accessed the uncommitted data then the result of the
Transactions may be incorrect. such type of accessing the uncommitted data results in a
problem called ‘uncommitted data problem’. this problem may violate the isolation property
of Transactions.
DBMS UNIT-V

T1 T2

Read emp_sal(7000);
Emp_sal=7000+500;
Write emp_sal(7500);
Roll back(7000);
Read emp_sal(7000);
Emp_sal=7000-100
Write emp_sal(6900)

Table(iii) serial execution of T1 and T2

In the above table, the execution of Transactions is done in the following the way

1.initially, the Transaction t1 is executed .this Transaction performs a read operation

as:

Read Emp_sal(7000)

2.T1 updates the attribute value as

Emp_sal =7000+500

=7500

3. T1 perform write operation

as Write

Emp_sal(7500)

4. T1 performs the rollback operation

as Rollback(7000)

5. Now,Transaction t2 is executed.this transaction performs a read operation as,

Read emp_sal(7000)
DBMS UNIT-V

6. T2 updates the emp_sal attributes as

Emp_sal(7000-100=6900)
DBMS UNIT-V

7. T2 performs a write operation as

Write emp_sal(7000-100=6900)

Thus,the serial execution of transactions T1 and T2 produce the correct result.But,if a


rollback operation is performed by the transaction T1 after the transaction T2 starts its
execution then incorrect data may be generated..

T1 T2
Read emp_sal(7000)
Emp_sal=7000+500;
Write emp_sal(7500);
Read emp_sal(7500);
(Un comited data is read)
Emp_sal=7500-100
Roll back(7000);

Write emp_sal(7500-100)=7400

Table(4) uncommited data due to concurrent execution of Transactions

In the above table, the execution of the Transaction t1 and t2 is done in the following way

1. Transaction T1 performs a read operation


as, Read emp_sal(7000)

2. T1 updates the attribute value as,

Emp_sal=7000+500=7500

3.T1 performs a write operation as

Write emp_sal(7500)

4.T2 perform the read operation(i.e) it reads the uncommitted data as


DBMS UNIT-V

Read emp_sal(7500)

5.T2 updates the emp_sal attributes

as Read emp_sal=7500-100

6.T1 perform roll back operation as

Roll back(7000)

7.T2 performs the write operatons as

Write emp_sal(7500-100=7400). Consequently,uncommited data problem occurs


since, the results of table (iii) (i..e 6900) mismatches with the result of table(iv)(i.e 7400).

Inconsistent data retrievals: if two Transactions are executing concurrently and a


Transaction is accessing the data prior to (or) after another Transaction completes the
execution of such a data,then inconsistent retrievals may arise.for ex,.if a Transaction t1
performs an aggregate function over a set of data and other Transaction t2 is performing
updations on the same set of data then Transaction t1 may read some data before updation
and the remaining data after updation.such type of execution may results in an inconsistent
data retrieval problem.

Before updating After updating


Emp_id Emp_sal Emp_sal

E1 1000 1000
E2 2000 2000
E3 3000 3000+500=3500
E4 1500 1500+1000=2500
E5 2500 2500
E6 3500 3500
Total=15000

Table(v) sum and update function


DBMS UNIT-V

In the above table, the updations that are performed on the emp_sal attribute are done by the
Transaction T2,where as aggregate (or) the total of all the attribute values is computed by the
Transaction T1. Now if T1 reads a value before T2 performs the updation on emp_sal then
incorrect results may occur.this is shown in the following table.

T1 T2
Read emp_sal for e1=1000;
Read emp_sal for ee=1000;
Total= e1+e2=1000+2000=3000;
Read emp_sal for e3:=3000;
Update emp_sal=3000+500
=3500
write emp_sal for e3=3500;
Read emp_sal for e3:=3500;
Total e1+e2+e3
1000+2000+3500
=6500(after updation)
Read emp_sal for e4=1500;
Total= e1+e2+e3+e4
1000+2000+3500+1500
=8000(before updating)

Read emp_sal for e4=1500;


Update emp_sal=1500+1000
=2500
write emp_sal for e4=2500;
commit

Read emp_sal for e5=2500;


Total =e1+e2+e3+e4+e5
1000+2000+3500+1500+2500
DBMS UNIT-V

=10500;
Read emp_sal for e6=3500;
Total= e1+e2+e3+e4+e5+e6
1000+2000+3500+1500+2500+3500
= 14000

Table(vi):inconsistent data retrievals due to concurrent execution of transactions.

In the above table an inconsistent dta retrieval problem occurs since,the result of table(v) (i,e
1500) mismatches with the result of table(vi)(i,,e 14000).

Scheduler:

A scheduler refers to a database process which provides an order to the transactions that are
concurrently executing the same data.this order can be determined by the concurrency
control algorithms.But,in a database, every transaction is not serializable,(i.e)some
transactions are serially executed while the other are non-serially executed.

The DBMS finds which transactions are serializable and which are non-serializable and the
non-serializable transactions are executed in a first come first serve(fcfs) manner.

However, the execution of non-serializable transactions in an fcfs manner consume a lot of


cpu time because, the cpu has to wait until a read (or) write operation is entirely completed.as
a result,several cpu cycles are lost and the fcfs scheduling of the transaction results in an un-
acceptable response times in a database system.Thus, to avoid such situation other scheduling
algorithms are required so as to increase the overall efficiency of the database system.

A schedule in which the interleaved execution of the database transaction produce the same
result as that of the transactions that are executed serially,such type of schedule is known as a
serializable schedule.Thus,the basic responsibility of a scheduler is to establish a serializable
schedule for the database transactions. It ensures that the cpu and the storage system are
working efficiently and effectively. It also help in ensuring the data isolation property by
maintaining the fact that no two transactions update/modify the same set of data
DBMS UNIT-V

T1 T2 output
Read Read No conflict
Write Write Conflict
Write Read Conflict
Read Write conflict

Table(i) read/write operations performing by t1 and t2

In the above table ,the following outputs occur during the concurrent execution of
transactions:

1. when t1 and t2 perform a read operation, then no conflict occurs

2. When t1 and t2 perform a write operation, then no conflict occurs

3. when t1 performs a write operation t2 concurrently performs a read operation, then a


conflict occurs

4. When t1 performs a read operation t2 concurrently performs a write operation, then a


conflict occurs.

The conflicting operations can be scheduled for concurrent executions adopting several
methods like time stamping, locking and optimistic methods.

concurrency control with locking methods:-

Lock granularity:

Lock granularity refers to a process that specifies the level to the lock that is being used.The
following are the different levels of locks in the data bases,

1. Database level

2. Tabel level

3. Page level

4. Row level

5.Field level
DBMS UNIT-V

1. data base level:

data base level lock refers to a lock that is provided on the entire data base so as to restrict the
transacton ‘t2’ to acess a table in the data base when onother transaction ‘t1’ using the same
database .such type of locks are suitable for the transactions that are executed in
batches,however they are not applicable for the multiple users that are accessing the same
database .the database level locks also make the access time of data slow because several
transactions have to wait until the previous transactions are executed completely.Throgh
,two transactions t1 and t2 use different tables for execution ,the database level lock does not
facilitate those transactions to access the same data base.

2. Table levels:

The table level lock refers to a lock that is provided on the complete table so as to restrict the
transaction t2 to access a row in the table when another transaction t1 is accessing any other
row in the same table. Thus different transactions can use the same database only when they
need different tables for accessing.

3. Page level:

A page level lock refers to a lock that is provided on an entire disk page so as to restrict a
transaction t2 to access a page that is locked when another transaction t1 is accessing the
same page.

4)row level:

A row level locks refers to a lock that is provided on a row so as to restrict a transaction T2
access a page when another transaction T1 is accessing the same row.

.Lock types:

Lock:

A lock is a variable associated with a data base object.locking is amechanism of manipulating


the data value of a lock.locks are used when multiple users want to access a data base object
concurrently.

The main purpose of using want to block other transaction from accessing the data items
which are locked currently.

The locks are granted to the transaction by a component of data base management system
called lock manger
DBMS UNIT-V

Types of locks:

The following the two different types of locks

1.binary locks.

2. shared/exclusive locks.

1. binary locks:

Binary locks are the locks that consists of two states,locked and uncloked.binary values 1 and
0 are used for representing locked state and uncloked state respectively..

If a dta item is in lock state.then it implies that it can not be accessd by any requested data
base operation..if the data item is is in unclock state,the any data base operation that have sent
a request for accessing the adat item can access it..

When ever a transaction sends a request for accessing data item(say x) then data base system
must ensure whether x is in unlocked state .if it is in locked state,then DBMS grants the lock
to the requested transaction,otherwise ,the transaction have to wait until the required data
item is unlocked.

2.Shared/exlcusive locks:

1.shared locks(s):

A transaction that acquired shared lock on data item p,can instruction but not the write
instruction(i,.e) these locks support only read integrity.

Using these locks multiple users can the data concurrently.but can not change/write the data
value.the intention of this lock is to ensure that data are not modified while they are locked
using shared locking mode.

Exclusive lock(X):

A transaction that acquired an exclusive lock on data item p, can execute both read as well
write instruction(i,e) these lock,support read and write integrity.only a single transaction can
update the data item to one transaction (i,e) if a transaction ta holds an exclusive lock on data
p1 then no other transaction can read/write that data item until ta releases the lock.

Whenever data item is locked using shared lock,then it can not be locked using exclusive
lock.on the other hand,if it is locked using exclusive lock,then it can not either shared lock or
another exclusive lock until the first exclusive lock is released.
DBMS UNIT-V

Two phase locking for ensuring serialiazability:

Two phase locking (2pl) protocol:

Two phase locking (2pl) protocol is a concurrency control protocol that serialiazability
between the schedules.a transaction obeys Two phase locking (2pl) protocol when read as
well as the write operation are executed before the executon of first unlock instruction.in 2
pl,the transactions can be devided into two phases

1. Growing(lock aqusition)phase)

2.Shrinking lock aqusition)phase)

1.Growing(lock aqusition)phase):

In this phase transaction releases the aquuires new locks,but can not relese them.

2. Shrinking lock aqusition)phase):

In this phase transaction releases the existing locks,but can not acquire any new locks.

Whenever a transacton enters the system,it is said to be in growing phase,where it acquires


the locks as per the requirement.but when the locks are released ,transaction enters the
second phase(i,e) shrinking phase where no more lock requests are processed.if lock
conversion is applied in 2pl,then lock upgrading and downlgrading can be done during and
shrinking phase respectively.
DBMS UNIT-V

T1 T2 Lock manager

Lock-s(p) Grant-s(p,T1)
Read(p)
Unlock(p)
Lock-x(q)

Read(q)
Q:=q+P
Write(q) Grant-x(q,T1)
Unlock(q) Lock-s(q)
Read(q)
Unlock(q)
Lock-x(p)

Read(q)
Unlock(q)
Grant-s(q,T2)
Lock-x(p)
Read(p)
P:=p+q
Write(q)
Unlock(p) Grant-x(p,T2)

Table(i):schedule s1

A transaction in schedule s1(T1 and T2) does not obey 2pl protocol since the lock-x(q) is executed
after the execution of unlock operation in T1.similarity,the lock-x(p0 operation is executed after the
execution of unlock(Q) operationT2.
DBMS UNIT-V

T3 T4 Lock manager

Lock-s(p) Grant-s(p,t3)
Read(p)
Lock-x(q)

Unlock(p)

Read(q)
Q:=q+P Lock-s(Q) Grant-x(q,t3)
Write(q)
Unlock(q) Read(q)
Lock-x(p)

Read(q)
Unlock(q)

Unlock(q) Grant-s(q,t4)
Read(p)
P:=p+q
Write(q)
Unlock(p)
Grant-x(p,t4)

Table(ii) scheduler s2

The transaction inn schedule s2,follows 2pl protocol since lock-x(q) operation is executed before
the execution of unlock(p) instruction and lock-x(p) is executed before execution of unlock(q).

Advantages:

In the transaction follows 2pl ,then the schedule is guarented to be seriliazable.

Disadvantages:

1. this protocol does not ensure freedom from deadlocks.

2.there is a possibility of cascading rollbacks to occurs.

3.limited amount of concurrency is provided.

Dead locks:

Dead lock is a situation where one transaction is waiting for another transaction to relese a
lock,which itself is waiting for another transaction to relese a lock before it can process with
its normal execution.
DBMS UNIT-V

Example:

Suppose, atransaction T1 holds an exclusive lock on some data item x and transaction T2
holds an exclusive lock an exclusive lock on x. Both these transactions are waiting for one
another for releasing their locks(i,e T1 is waiting for T2 to relese the the lock and T2 is
waiting their locks(i,e T1 is waiting for T2 to relese the lock and T2 is waiting for T1 to
relese lock),thus,a state is arrived,where none of these transaction can proced with their
normal execution.such a state is called a dead lock state.

If a dead lock is detected,it can be handled,by the system by rolling any one of the
transaction.if a transaction is rolled back,then all the locks acquired by that transaction can be
released.now other transactions can acess the unlocked data item and can proced their
execution.

DBMS is responsible for for detecting and preventing the deadlock.

Techniques for controlling dead locks:

The following the 3 diffrent techniques used for controlling the occurrence of dead lock.

1.dead lock prevention

2. dead lock detection

3.dead lock avoidance.

1.dead lock prevention:

This techniques prevents the occurrence of dead lock by avoiding the execution of the
conditions that may generate dead lock.this is done by examing whether to grant a lock
permission to a transaction results in a dead lock or not.if it results in dead lock,then the
requested transaction is aborted and all the changes performed by the transaction rolled
back.in addition to this,all the locks acquired by the aborted transaction are released and then
the transaction is reschedule for execution.

This technique is generally performed when the probability for the occurrence of dead lock
is very high.

Dead lock detection:


DBMS UNIT-V

This technique detects the transaction whose execution may lead to the occurrence of dead
lock.this is done examing the data base for dead lock regularly.while examing if a dead lock
is found.,then the transaction and therefore can continue with their execution, the changes of
aborted is rolled back and the transaction is rescheduled for execution.

Dead lock avoidance:

This technique avoids the occurrence of dead llock by allowing the transaction to initially
acquire all the locksthat are required for performing the execution.

Using this technique, a transaction acquires the lock in succession,due to which the roll back
of confliction transactions can be avoided.however,such way of acquiring lock in sequential
increases the response times of execution.

This technique is performed in the situation where in the response time is less for
transactions on the system.

Concurrency control with time stamp ordering:wait/die and wound/wait schemes:

Time stamping:

Time stamping ordering technique is amethod that determines the seriliazability order of
different transactions in a schedule.this can be determine by having prior knowledge about
the order in which transactions.are executed.this technique assign a global,distinct time stamp
for each transaction.basically time stamp is an identifier that not only specifies the the time of
transaction but also the order in which the transaction is to be processed by DBMS.

Properties of time stamp:

The following are two important properties of time stamp.

1.uniqueness

2.monotonicity

1.uniqueness:
DBMS UNIT-V

This property ensures that the time stamp value of one transaction is different from the other
transaction.in other words,it meames that no two transactions can have the same time
stamp value.

2.momotonity:

This property ensures that the time stamp value of one transaction is always in increasing
order (i,e) the time stamp of an existing transaction is less than the time stamp of newly
entered transaction.

Time stamping methods:

The following are two methods implemented for ensuring concurrency control with the
transactions.

1. wait/die method

2.wound/wait method

1.wait/die method:

In wait/die scheme,if older transaction requests for an item which is locked by younger
transaction then the older transaction have to wait until the execution of the younger
transaction is completed.upon execution ,the younger transaction releases the lock so that the
waiting transaction can acquire the lock and perform its execution.

On the otherhand, if a younger transaction requests for a lock on the system item locked by
older transaction, then the younger transaction is aborted and restarted with the same time
stamp. this scheme is non-premptive scheme since the older transaction never pre-empts the
younger transaction.

Algorithms:

Let T1 requests lock on x which is already locked by tj.

Idf ts(T1)<tj) then

T1 waits
DBMS UNIT-V

Else

If

ts(ti)>tj)then

Ti aborts

In the above algorithm, the condition ts(T1)<tj) specifies that (T1) is older transaction and tj
is younger transaction.on the other hand, the condition ts(ti)>ts(tj) specifies that ti is younger
transaction and t3 is older transaction.

If transaction T1 request a for a lock on item which is already locked by T2 then T1 has wait
until T2 completes its execution. this is because the time stamp of T1 is less than time stamp
of T2(i,e 25<30).

Now, if transaction T2 requests for a lock on item which is already locked by T1. Then T2 is
aborted.this is because the time stamp of T2 is greter than timestamp of T1(30>25)

2. wound/wait method:

In wound/wait method,if an order transaction requests for a lock on item, which is locked by
younger transaction,then the older transaction has to using the same time stamp value.
however, if a young transaction request for an item locked by older transaction,then the
younger transaction has to wait until the older transaction has completed its executon and has
released the acquired lock.this schemes is preemptive since the order transaction pre-empts
the younger transaction.

Algorithm:

Let T1 request a lock on data item x,which is locked tj if ts(ti)<ts(tj)

Tj is wounded(rolled back(

Else

If ts(ti)>(tj)

Ti waits.
DBMS UNIT-V
DBMS UNIT-V
DBMS UNIT-V
DBMS UNIT-V

You might also like