0% found this document useful (0 votes)
9 views

Transaction

How Transaction is done in DBMS, how can we avoid deadlock

Uploaded by

paki.kakawzd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Transaction

How Transaction is done in DBMS, how can we avoid deadlock

Uploaded by

paki.kakawzd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Data Base Management System

Lecture 16

Mudassar Mahmood Mirza


Transaction
 A transaction is defined as a logical unit of work. It consists
of one or more operations on a database that must be
completed together so that the database remains in consistent
state. A transaction must either complete successfully or
completely fails.
 Begin
Insert into A (Col1, Col2, Col3) values (m, n, p)
Select ColX, ColY, ColZ from B
Delete From B
End

 Begin
Insert into A (Col1, Col2, Col3)
Select ColX, ColY, ColZ from B
Delete From B
End

 Begin
Delete From B
Insert into A (Col1, Col2, Col3)
Select ColX, ColY, ColZ from B
End
Let Have look on this
 Suppose that information of stock is stores in database. The database
stores two things i.e. sold list and quantity.
 When an item is sold to a customer, it is added to the sold list. This is
one operation on the database.
 However, the quantity of the sold item must also be subtracted from the
items currently in stocks.
 If both steps are not completed together, the database will be in an
inconsistent state.
 The quantity of items in stock will be wrong if items are sold but not
subtracted from items in stock.
 The quantity will be wrong if the items are subtracted from items in
stock but not sold.
 Both of these operations together make up a single transaction and both
must complete successfully or both must fail.
 Sale Table (SaleID, ProductID, Quantity, ClientID)
 Stock Table (StockID,ProductID,Quantity)

 Begin
 Insert into Sale (SaleID, ProductID, Quantity, ClientID) values (100, 3, 7, 1)
 Update Stock Set Quantity = Quantity –7 Where ProductID=3
 End
Commit and Rollback
 A transaction is committed if it completes successfully and
changes the data. If it fails and leaves the data unchanged, it is
said that the transaction has been rolled back.
 It means that the rollback is use to undo the work done in the
current transaction.
Transaction Properties
(ACID Properties)
 A transaction must have four Properties called ACID
Properties. These are as follows:

 Atomicity
 Consistency
 Isolation
 Durability
Atomicity
 A transaction must be an atomic unit of work. A transaction
must completely succeed or completely fail. If any statement
in transaction fails, the entire transaction fails completely.
 When a transaction is completed successfully it is said to be
committed. In case of failure of a statement, all previous
successful statements in transaction are rollback or reverted
to the previous state of consistency. An incomplete
transaction doesn’t take place.
Consistency
 A transaction must leave the data in a consistent state after
completion. For example, in a bank database, money should
never be “created” or “deleted” without an appropriate
deposit or withdrawal.
Isolation
 All transactions that modify the data are isolated from each
other. They do not access the same data at the same time.
Transactions must have no dependence or effect on other
transactions. (A transaction is an independent unit of
logically related work)
Durability
 The durability means that the modifications made by a
transaction are permanent and persistent. If the system is
crashed or rebooted, data should be guaranteed to be
completed when the computer restarts
Concurrency
 Concurrency is a situation in which two are more users
access the same place of data at the same time. In a multi-
user environment, concurrency occurs very commonly. In
some situations, the concurrent access may arise to some
serious problems.

Concurrency Problems
 Different problems that may occur due to concurrency are as
follows:

 Update Problem
 Uncommitted Dependency/Problem
 Inconsistent Analysis Problem
Update Problem
 The problem arises when two or more transactions update
the same data concurrently.
 It occurs when two or more transactions select the same row
and then update the row based on the value originally
selected.
 Each transaction is unaware of other transactions.

 The last update overwrites updates made by the previous


transactions. It results in loss of data.
Uncommitted Dependency/Problem
 This problem arises when two or more transactions work on
the same table.
 One transaction updates certain part of data which is not yet
committed.
 The uncommitted dependency/problem occurs when a
second transaction selects a row that is being updated by
another transaction.
 The second transaction is reading data that has not been
committed yet and may be changed by the transaction that is
updating the row.
Inconsistent Analysis Problem
 Inconsistent analysis problem occurs when a transaction
reads several values from the database, but another
transaction updates some of them during the executing of the
first transaction.
Resource Locking
 One way to prevent concurrency problems is to lock the
shared data. Locking ensures that the shared data can be used
by one user at one time.
 When a user accesses the data, the second user has to wait
until the first user finishes his work.
 The locking mechanism ensures that if one transaction is in
progress, the second transaction will be waits for the
completion of first.
Deadlock
 Deadlock is a situation in which two transactions wait for
completion of each other. The first transaction needs a
resource that is locked by second transaction and the second
transaction needs a resource that is locked by first
transaction. Deadlock is caused by locking.
 Suppose there are two items A and B in the database. Two
users need these items to complete their work. The following
sequence of lock will result in a situation where user 1 has
lock the item A and user 2 has needs the item A to complete
his work and user 2 has lock the item B and user 1 has needs
the item B to complete his work. Both are waiting each
other. This situation is known as deadlock.
Solution of Deadlock
 The Deadlock problem can be solved in two ways:
 Deadlock Prevention
 Deadlock Detection and Recovery
 Deadlock Prevention
 There are different ways for preventing a deadlock to occur.
One way is to allow users to issue only one lock request at one
time. The user must lock all required resources at once. In the
above example, if user 1 locks both item A and B at once in the
first line, deadlock will not occur.
Deadlock Detection and Recovery
 If deadlock occurs, it needs to be detected and then
recovered. In order to recover from a deadlock situation, one
transaction needs to be killed. In above example, if the
transaction of user 2 is aborted, the item B will release from
the lock and the user 1 will be able to complete the work.
When the transaction is killed, all changes made by that
transaction should be undone (rollback) to ensure database
consistency.
Types of Backups
 Backup is used to protect database from media failures or other
errors. One or more of database files may be physically damaged
and unusable after media failure. The most recent backup can be
used to replace the damaged files and reconstruct the database.
 There are two main types of database backups:
 Full backup
 Incremental backup
 In addition database may be in two different states while
performing a backup:
 Online
 Offline
Types of Backup
 Full backup
 A full backup is used to create a copy of all data of entire database. It
requires a large amount of storage space. However, a database can
restore relatively quickly using a full backup as it requires to simply
copying the backup files.
 Incremental backup
 An incremental backup creates a copy of only the data that have
changed since the last backup. Since an incremental backup only
contains changes made to the database, the user must perform a full
backup before the incremental database to restore the database at a
later time. Incremental backup requires small amount of storage
space. However, it may take more time to restore a database.
Offline backup
 An offline backup is a backup that is performed after a
database has been shut down. The database administrator
must schedule a time to shutdown the database and notify all
users so they can disconnect for the database.
 Offline backup can be inconvenient for users as they must
remember to complete all active transactions and disconnect
from the database before shutting down. Offline backup is
performed by using the commands of operating system.
DBMS does not provide this facility as offline back is
performed when DBMS has been shutdown.
Online backup
 An online backup is a backup that can be performed while
the database is running. The database administrator does not
have to shut down the database. The users do not need to
disconnect. Online backup are more convenient foe users as
they perform no action.

 Online backup is performed by using the commands of


DBMS. DBMS should provide the facility for online backup
as DBMS is running when online backup is performed.

You might also like