DBMS ch-5
DBMS ch-5
Unit 5
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:
Execution of transaction
Ex:
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
A.balance = New_Balance
Close_Account(A)
B’s Account
Open_Account(B)
Old_Balance = B.balance
B.balance = New_Balance
Close_Account(B)
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.
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.
Single user:
A DBMS is single-user means one user can use the system at a time.
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.
Presentation application
computer
Services services
DBMS
Services services
Computing systems
Multiprogramming
Inter-leaved Execution
Pseudo-parallel processing
Parallel processing
Steps
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:
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).
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.
Ex:
T1 T3
Sum:=0;
Read_item(A);
Sum:=sum+A;
Read_item (X);
Write_item (X);
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
2.System Failure
• Physical problems
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.
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.
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.
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:
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;
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.
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.
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.
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.