0% found this document useful (0 votes)
35 views12 pages

DBMS ch-5

Uploaded by

bsvaishnavi260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views12 pages

DBMS ch-5

Uploaded by

bsvaishnavi260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Seshadripuram College Database Management Systems (DSC-4)

Unit 5

Query Processing Transaction Management

Transaction:
 A transaction is the basic logical unit of execution in an information system. It is a
sequence of operations executed as a whole by taking a consistent database into
another consistent database state.
 A transaction is a unit of a program execution that accesses and modifies various
data objects.(tuples, relations)

Ex:

Begin transaction End transaction

Execution of transaction

Database may be temporarily in an

In consistent state during execution

Ex:

Let’s take an example of a simple transaction.

Suppose a bank employee transfers Rs 500 from A's account to B's account.

This very simple and small transaction involves several low-level tasks.

A’s Account

Open_Account(A)

Old_Balance = A.balance

New_Balance = Old_Balance - 500

A.balance = New_Balance

Close_Account(A)

B’s Account

Open_Account(B)

Department of CS (BCA) Page 1


Seshadripuram College Database Management Systems (DSC-4)

Old_Balance = B.balance

New_Balance = Old_Balance + 500

B.balance = New_Balance

Close_Account(B)

Transaction Properties: (or) Rules


DBMS has to maintain the following properties of transactions (ACID Properties)

ACID

A- Atomicity

C-Consistency

I - Isolation

D- Durability

A:Atomicity
States that every Transaction should be atomic in nature. Thus by Atomic Transaction it
means “all or none”. Either all SQL statements/steps execute successfully in a transaction, or fail
as a single unit and none of them should be treated as executed.

For example:

If account-A & account-B both having $2000 balance, you have to transfer $1000 from
account-A to account-B, this will involves 2 steps. First withdrawal from account-A, and Second
deposit in account-B. Thus, both the steps should be treated as single or atomic unit and at the
end account-A should have $1000 & account-B should have $3000 balance. If in case after First
step the system fails or any error occurs then first step should also be rolled-back and $1000
withdrawn from account-A should be re-deposited to it, maintaining $2000 back in both the
accounts.

Department of CS (BCA) Page 2


Seshadripuram College Database Management Systems (DSC-4)

C: Consistency
States that any Transaction happened in a database will take it from one consistent state
to another consistent state. The data finally recorded in the database must be valid according to
the defined Rules, Constraints, Cascades, Triggers, etc. If in case of any failure to these rules the
changes made by any transaction should be rolled-back, this will put the system in earlier
consistent state.

For example: If the money deposit process has any Trigger built on top of it. And at the time of
money transfer any of the Trigger fails or any database node, the system should automatically
Rollback the complete transaction and switches back the system to its previous consistent state
before the transaction was started. Or if everything executes successfully then the system is
committed to a new consistent state.

I: Isolation
Means Transactions performing same functions should run in Isolation and not in parallel
to provide more concurrency to the data and avoiding dirty reads & writes. One needs to use
proper Transaction Isolation levels and locking in order to prevent this.

For example:

If two people accessing a joint-account with $5000 balance from 2 terminals to withdraw
money. Let’s say at same time John & Marry apply to withdraw $4000 from two different
ATMs. If both the Transactions do not run in Isolation and run in parallel then both John &
Marry will be able to withdraw $4000 each i.e. $8000 total from their account. To make sure this
won’t happen Transactions should be not allowed to run in parallel, by setting Transaction
Isolations and/or locking methods on the database objects.

D: Durability
A transaction should be durable by storing the data permanently and making it available
in case of power failure, recovery from system failure, crash, any error, etc.

Department of CS (BCA) Page 3


Seshadripuram College Database Management Systems (DSC-4)

Transaction processing Systems(TPS)

It is an information system designed to process routine business transactions, these


are the systems with large databases and hundreds of concurrent users are
executing database transations.eg: Airline reservation, E-commerce , Bank’s
account processing transactions etc

Single User and Multi User Systems

Single user:
A DBMS is single-user means one user can use the system at a time.

computer Presentation application DBMS

Services services

Presentation services display forms, handles flow of information to/ from screen.
Application services implements user request, interacts with DBMS.

Multi- user:
A DBMS is multi-user means many users can use the system at a time or concurrently.
 Dumb terminals connected to main frame means application and presentation services on
main frame.
 ACID properties required
It means isolation: In DBMS sees an interleaved schedule.
Atomicity and durability implemented by DBMS, Provides ACID properties.

Department of CS (BCA) Page 4


Seshadripuram College Database Management Systems (DSC-4)

Presentation application
computer
Services services

DBMS

computer Presentation application

Services services

Dumb terminals user module

Concurrent Transactions: It is the concept of multiprogramming where a System can


execute multiple programs/processes at a time.

Computing systems

Single-processor computer system

 Multiprogramming
 Inter-leaved Execution
 Pseudo-parallel processing

Multi-processor computer system

 Parallel processing

Interleaving: in concurrent execution of


processes the CPU will be kept busy and
if process requires an input or output
instead of keeping the process idle
another process is executed at I/O
runtime.

Basic operations in Transaction:

Department of CS (BCA) Page 5


Seshadripuram College Database Management Systems (DSC-4)

Read_item(X): reads a database item named X into a program variable, here we


assume the program variable name also as X.

Steps

1. Find the address of the disk block that contains item X

2. Copy the disk block into a buffer in main memory

3. Copy the item X from the buffer to the program variable named X

Write_item(X): writes the value of program variable X into the database item named X

Steps:

1. Find the address of the disk block that contains item X.

2. Copy that disk block into a buffer in main memory

3. Copy item X from the program variable named X into its correct
location in the buffer.

4. Store the updated block from the buffer back to disk (either
immediately or at some later point in time).

Why Concurrency Control is needed

Department of CS (BCA) Page 6


Seshadripuram College Database Management Systems (DSC-4)

Several problems can occur when concurrent transactions execute in an uncontrolled


manner.ginal value. Ex:

T1 T2

Read_item (X);

X:=X-N;

Write_item (X);

Read_item(X);

X:=X+M;

Write_item(X);

Read_item (Y);

In this transaction T1 fails and must change the value of X back to its old value.

Transaction T2 has read the temporary incorrect value of X. the value of item X is read by T2 is
called “Dirty data”, because it has been created by a transaction that has not completed and
committed yet.

The Incorrect Summary Problem:


If one transaction is calculating an aggregate summary function on a number of records
while other transaction are updating some of these records, the aggregate function may calculate
some values before they are updated and others after they are updated.

Ex:

T1 T3

Sum:=0;

Read_item(A);

Sum:=sum+A;

Read_item (X);

X:=X-N; T3 reads X after N is subtracted and


reads Y before N is added

Write_item (X);

Department of CS (BCA) Page 7


Seshadripuram College Database Management Systems (DSC-4)

Read_item(X);

Sum:=sum+x;

Read_item(Y);

Sum:=sum+Y;

Read_item (Y);

Y:=Y+N;

Write_item(Y);

A transaction reads items twice with 2 different values because it was changed by another
transaction between 2 reads.

Database Failures
Database failures are generally classified into 3 types. They are

1.Transaction failures

• Transaction or system error

• Local errors or exception conditions

• Concurrency control enforcement

2.System Failure

• Computer failure or System crash

• Physical problems

3.Media failures: loss of data by read or write malfunction

DBMS has a Recovery Subsystem to protect database against system failures

Why recovery is needed?


 Transaction submitted for execution.
 DBMS is responsible for making sure that either
 All operations in transaction are completed successfully and the changes are recorded
permanently in the database.

Department of CS (BCA) Page 8


Seshadripuram College Database Management Systems (DSC-4)

 The DBMS must not permit some operations of a transaction T to be applied to the
Data base while others of T are not.
 It will cause Inconsistency.
 A system may crash, disk failures, transaction or local errors and physical problems.

Transaction and System concepts

 BEGIN_TRANSACTION: marks start of transaction


 READ or WRITE: two possible operations on the data
 END_TRANSACTION: marks the end of the read or write operations; start
checking whether everything went according to plan
 COMIT_TRANSACTION: signals successful end of nsaction
Kill the transaction

Committed –

If a transaction executes all its operations successfully, it is said to be committed. All its
effects are now permanently established on the database system.

System Log:
Log or Journal keeps track of all transaction operations that affect the values of database
items. The log is kept on disk because it should not affect with any system failures and it is
periodically backed up.

Types of log records: Transaction id is T


1. [start_transaction, T]
2. [write_item, T,X,old_value,new_value]:
3. [read_item, T,X]
4. [commit,T]
5. [abort,T]
6. [checkpoint]

Transaction support in SQL


• A single SQL statement is always considered to be atomic
• There is no explicit Begin_Transaction statement

Department of CS (BCA) Page 9


Seshadripuram College Database Management Systems (DSC-4)

• SET TRANSACTION statement in SQL2 sets the characteristics of a


transaction
• Access mode
• READ only or READ-WRITE
• Diagnostic area size
• Indicates the number of conditions that can be held
simultaneously in the diagnostic area.
• Isolation level
• READ UNCOMMITTED, READ COMMITTED,
REPEATABLE READ, SERIALIZABLE

Concurrency Control Techniques


Concurrency:
It refers to when multiple programs executing or running at the same time. It is also
referred as multiprogramming or multitasking.

Schedule: is a list of actions /operations(read, write, abort, commit) in a set of transactions.


Serializability: Serializability of a schedule means equivalence to a serial schedule with the
same transactions.in this we have serial, non serial, conflict serializable etc

Time stamp:
Time stamp is a unique identifier created by the DBMS to identify the transaction. The
time stamp values are assigned in the order in which the transactions are submitted to the system.

Time stamp has 2 values. They are


1. Read_TS(X)
2. Write_TS(X)

Lock:
A lock is a variable associated with a data item that describes the status of the item with
respect to possible operations.

Types of Locks:
 Binary Lock
 Shared Lock or Exclusive Lock

Binary Lock:

Department of CS (BCA) Page 10


Seshadripuram College Database Management Systems (DSC-4)

A binary Lock can have 2 states or values those are locked and unlocked (1 and 0).

In binary locking system, almost one transaction can hold a lock on a given item.

Suppose we should allow several transactions to access the same item X, if they all access X for
reading purposes only. However, if a transaction is to write an item X, it must have exclusive
access to X. it cannot be possible by binary lock instead of this we are using Exclusive lock.

Lock operation:

1. Lock_item(X)
B: if LOCK(x)=0 (*item is ulocked*)
The LOCK(x)  1 (*lock the item*)
else begin
wait (until lock(X)=0 and the lock manager wakes up the transaction;
Goto B
End;
2. Unlock_item(X)
Lock  0;

Shared or exclusive lock:

In this we are using 3 locking operations they are

 Read _lock(X)
 Write lock(X)
 Unlock(X)

In the above a read locked item is known as shared locked because outer transactions are allowed
to read the item.

Write locked item is called exclusive locked because a single transaction exclusively holds the
lock on the item.

Two Phase Locking (2PL)


This locking protocol divides the execution phase of a transaction into three parts.

 In the first part, when the transaction starts executing, it seeks permission for the locks it
requires.
 The second part is where the transaction acquires all the locks. As soon as the transaction
releases its first lock,
 The third phase starts. In this phase, the transaction cannot demand any new locks; it only
releases the acquired locks.

Department of CS (BCA) Page 11


Seshadripuram College Database Management Systems (DSC-4)

Two-phase locking has two phases, one is growing, where all the locks are being acquired by the
transaction; and the second phase is shrinking, where the locks held by the transaction are being
released.

Strict Two-Phase Locking


The first phase of Strict-2PL is same as 2PL. After acquiring all the locks in the first phase, the
transaction continues to execute normally. But in contrast to 2PL, Strict-2PL does not release a
lock after using it. Strict-2PL holds all the locks until the commit point and releases all the locks
at a time.

Problems caused by use of Locks

1. Deadlock: A deadlock is a situation in which two or more processes sharing the same
recourse were effectively preventing each other from accessing the resource resulting in
both process ending the transaction
2. Starvation: This occurs when a particular transaction consistently waits or restarted and
never gets a chance to proceed further.

Department of CS (BCA) Page 12

You might also like