0% found this document useful (0 votes)
8 views50 pages

Unit -4 - part 1

Uploaded by

admin pro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
8 views50 pages

Unit -4 - part 1

Uploaded by

admin pro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 50

Course Code: CS302

Database Management Systems

UNIT – IV

Transactions and Query Processing: Transaction Management - ACID properties,


Concurrency control – Schedules - Serializability, Locking Protocols,
Recoverability, Query Processing and optimization, Database Recovery methods.

Slide 1- 1
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe
What is ATM Transaction?

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 2


What is ATM Transaction?

Steps for ATM Transaction


1. Transaction Start.
2. Insert your ATM card.
3. Select a language for your transaction.
4. Select the Savings Account option.
5. Enter the amount you want to withdraw.
6. Enter your secret pin.
7. Wait for some time for processing.
8. Collect your Cash.
9. Transaction Completed.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 3


Introduction to Transaction Processing

 Transaction processing systems are systems with large databases and hundreds
of concurrent users executing database transactions.
 Examples of such systems include
 Banking, credit card processing, online retail purchasing and stock markets

 These systems require


 High availability

 Fast response time for hundreds of concurrent users.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 4


Single-User versus Multiuser Systems

 One criterion for classifying a database system is according to the number of


users who can use the system concurrently.
 A DBMS is single-user if at most one user at a time can use the system
 Single-user DBMSs are mostly restricted to personal computer systems.

 It is multiuser if many users can use the system— and hence access the
database—concurrently.
 For example,
 an airline reservations system is used by hundreds of users and travel agents
concurrently.
 Database systems used in banks, insurance agencies, stock exchanges,
supermarkets, and many other applications are multiuser systems.
 In these systems,

 Hundreds or thousands of users are typically operating on the database

by submitting transactions concurrently to the system.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 5


Single-User versus Multiuser Systems

 How to achieve this multi user access ?

 Multiple users can access databases—and use computer systems—


simultaneously because of the concept of multiprogramming, which allows
the operating system of the computer to execute multiple programs—or
processes—at the same time.
 A single central processing unit (CPU) can only execute at most one

process at a time (Interleaving A and B)

 If the computer system has multiple hardware processors (CPUs), parallel


processing of multiple processes is possible, as illustrated by processes C and
D in Figure

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 6


Single-User versus Multiuser Systems

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 7


Transaction
 Transaction:
 A transaction is an executing program that forms a logical unit of

database processing.
 A transaction is an action or series of actions by a single user to perform

operations for accessing the contents of the database.


 A transaction usually means that the data in the database has changed.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 8


Transaction
 Example: Suppose an employee of bank transfers Rs 800 from X's account to
Y's account. This small transaction contains several low-level tasks:

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 9


Operations of Transaction:

 Transaction includes one or more database access operations


 Read operations

 (database retrieval, such as SQL SELECT)


 Read(X): Read operation is used to read the value of X from the

database and stores it in a buffer in main memory.


 Write operations
 (modify database, such as SQL INSERT, UPDATE, DELETE)

 Write(X): Write operation is used to write the value back to the

database from the buffer.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 10


Operations of Transaction:

 Example: Let's take an example to debit transaction from an account which


consists of following operations:
1. R(X);
2. X = X - 500;
3. W(X);
 Let's assume the value of X before starting of the transaction is 4000.

Solution:
 The first operation reads X's value from database and stores it in a buffer.
 The second operation will decrease the value of X by 500. So buffer will
contain 3500.
 The third operation will write the buffer's value to the database. So X's final
value will be 3500.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 11


Operations of Transaction:

 But it may be possible that because of the failure of hardware, software or


power, etc. that transaction may fail before finished all the operations in the set.

 For example: If in the above transaction, the debit transaction fails after
executing operation 2 then X's value will remain 4000 in the database which is
not acceptable by the bank.

To solve this problem, we have two important operations:


 Commit: It is used to save the work done permanently.
 Rollback: It is used to undo the work done.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 12


Operations of Transaction:

 A transaction (database operations) may be:


 stand-alone

 specified in a high level language like SQL submitted

interactively
 embedded within a program (most transactions)

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 13


Transaction boundaries:

 Begin transaction and End transaction.


 An application program may contain several transactions separated by Begin
and End transaction boundaries.
 If the database operations in a transaction do not update the database but only
retrieve data,
 the transaction is called a read-only transaction.

 otherwise it is known as a read-write transaction.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 14


READ AND WRITE OPERATIONS:

read_item(X) command includes the following steps:


 Find the address of the disk block that contains item X.
 Copy that disk block into a buffer in main memory (if that disk block is not already
in some main memory buffer).
 Copy item X from the buffer to the program variable named X.

write_item(X) command includes the following steps:


 Find the address of the disk block that contains item X.
 Copy that disk block into a buffer in main memory (if it is not already in some main
memory buffer).
 Copy item X from the program variable named X into its correct location in the
buffer.
 Store the updated block from the buffer back to disk (either immediately or at some
later point in time).

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 15


READ AND WRITE OPERATIONS:

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 16


Transaction property

 The transaction has the four properties.


 These are used to maintain consistency in a database, before and after the
transaction.
Property of Transaction
 Atomicity
 Consistency
 Isolation
 Durability

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 17


Transaction property

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 18


Atomicity

 It states that all operations of the transaction take place at once if not, the
transaction is aborted.
 There is no midway, i.e., the transaction cannot occur partially.
 Each transaction is treated as one unit and either run to completion or is not
executed at all.

Atomicity involves the following two operations:


 Abort: If a transaction aborts then all the changes made are not visible.
 Commit: If a transaction commits then all the changes made are visible.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 19


Atomicity

 Example: Let's assume that following transaction T consisting of T1 and T2. A consists
of Rs 600 and B consists of Rs 300. Transfer Rs 100 from account A to account B.

T1 T2
Read(A) Read(B)
A:= A-100 Y:= Y+100
Write(A) Write(B)

 After completion of the transaction, A consists of Rs 500 and B consists of Rs 400.


 Consider a scenario - If the transaction T fails after the completion of transaction T1 but
before completion of transaction T2.
 then the amount will be deducted from A but not added to B.
 This shows the inconsistent database state
 In order to ensure correctness of database state, the transaction must be executed in
entirety.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 20


Consistency

 The integrity constraints are maintained so that the database is consistent before
and after the transaction.
 The execution of a transaction will leave a database in either its prior stable
state or a new stable state.
 The consistent property of database states that every transaction sees a
consistent database instance.
 The transaction is used to transform the database from one consistent state to
another consistent state.
 For example: The total amount must be maintained before or after the
transaction.

Total before T occurs = 600+300=900


Total after T occurs= 500+400=900

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 21


Isolation

 It shows that the data which is used at the time of execution of a transaction
cannot be used by the second transaction until the first one is completed.
 Example:
 In isolation, if the transaction T1 is being executed and using the data item

X, then that data item can't be accessed by any other transaction T2 until the
transaction T1 ends.
 The concurrency control subsystem of the DBMS enforced the isolation
property.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 22


Durability

 The durability property is used to indicate the performance of the database's


consistent state.
 It states that the transaction made the permanent changes.
 They cannot be lost by the erroneous operation of a faulty transaction or by the
system failure. When a transaction is completed, then the database reaches a
state known as the consistent state. That consistent state cannot be lost, even in
the event of a system's failure.
 The recovery subsystem of the DBMS has the responsibility of Durability
property.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 23


Durability

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 24


Summary of ACID Properties

 Atomicity: A transaction is a single unit of operation. You either execute it


entirely or do not execute it at all. There cannot be partial execution.
 Consistency: Once the transaction is executed, it should move from one
consistent state to another.
 Isolation: Transaction should be executed in isolation from other transactions
(no Locks). During concurrent transaction execution, intermediate transaction
results from simultaneously executed transactions should not be made available
to each other. (Level 0,1,2,3)
 Durability:· After successful completion of a transaction, the changes in the
database should persist. Even in the case of system failures.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 25


States of Transaction

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 26


States of Transaction

State Transaction types


A transaction enters into an active state when the execution
Active State process begins. During this state read or write operations can be
performed.
A transaction goes into the partially committed state after the end
Partially Committed
of a transaction.
When the transaction is committed to state, it has already
Committed State completed its execution successfully. Moreover, all of its changes
are recorded to the database permanently.

A transaction considers failed when any one of the checks fails or


Failed State
if the transaction is aborted while it is in the active state.

State of transaction reaches terminated state when certain


Terminated State
transactions which are leaving the system can’t be restarted.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 27


Active state

 The active state is the first state of every transaction.


 In this state, the transaction is being executed.
 For example: Insertion or deletion or updating a record is done here. But all
the records are still not saved to the database.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 28


Partially committed

 In the partially committed state, a transaction executes its final operation, but
the data is still not saved to the database.
 In the total mark calculation example, a final display of the total marks step is
executed in this state.

Committed
 A transaction is said to be in a committed state if it executes all its operations
successfully.
 In this state, all the effects are now permanently saved on the database system.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 29


Failed state

 If any of the checks made by the database recovery system fails, then the
transaction is said to be in the failed state.
 In the example of total mark calculation, if the database is not able to fire a
query to fetch the marks, then the transaction will fail to execute.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 30


Aborted or Terminated

 If any of the checks fail and the transaction has reached a failed state then the
database recovery system will make sure that the database is in its previous
consistent state. If not then it will abort or roll back the transaction to bring the
database into a consistent state.
 If the transaction fails in the middle of the transaction then before executing the
transaction, all the executed transactions are rolled back to its consistent state.
 After aborting the transaction, the database recovery module will select one of
the two operations:
 Re-start the transaction

 Kill the transaction

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 31


Schedule

 A schedule is a list of transactions.


 Or
 A series of operation from one transaction to another transaction is known as
schedule.
 It is used to preserve the order of the operation in each of the individual
transaction.
 Or
 A Schedule is a process creating a single group of the multiple parallel
transactions and executing them one by one.
 Or
 When there are multiple transactions that are running in a concurrent manner
and the order of operation is needed to be set so that the operations do not
overlap each other, Scheduling is brought into play and the transactions are
timed accordingly.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 32
Schedule

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 33


1. Serial Schedule
 The serial schedule is a type of schedule where one transaction is executed
completely before starting another transaction.
 In the serial schedule, when the first transaction completes its cycle, then the
next transaction is executed.
 For example: Suppose there are two transactions T1 and T2 which have some
operations. If it has no interleaving of operations, then there are the following
two possible outcomes:
1. Execute all the operations of T1 which was followed by all the operations
of T2.
2. Execute all the operations of T2 which was followed by all the operations
of T1.
 In the given (b) figure, Schedule B shows the serial schedule where T2
followed by T1.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 34


1. Serial Schedule
In the given (a) figure, Schedule A shows the serial schedule where T1 followed by
T2.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 35


1. Serial Schedule
In the given (b) figure, Schedule B shows the serial schedule where T2 followed
by T1.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 36


2. Non-serial Schedule
 If interleaving of operations is allowed, then there will be non-serial schedule.
 It contains many possible orders in which the system can execute the individual
operations of the transactions.
 In the given figure (c) and (d), Schedule C and Schedule D are the non-serial
schedules. It has interleaving of operations.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 37


2. Non-serial Schedule

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 38


2. Non-serial Schedule

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 39


2. Non-serial Schedule
 In non-serial schedule - If the schedule is not proper - Then the problems can
arise like
 Multiple update

 Uncommitted dependency

 Incorrect analysis.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 40


3. Serializable schedule
 The serializability of schedules is used to find non-serial schedules that allow
the transaction to execute concurrently without interfering with one another.
 It identifies which schedules are correct when executions of the transaction
have interleaving of their operations.
 A non-serial schedule will be serializable if its result is equal to the result of its
transactions executed serially.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 41


3. Serializable schedule
 The main objective of serializability
 Is to find non-serial schedules

 that allow transactions to execute concurrently without interference and

 produce a database state

 that could be produced by a serial execution.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 42


Schedule Example

Consider the following two transactions for a bank:


 The first is meant to deduct $100 from an account
 The second adds 0.5% interest to every account in the bank.

 The transactions are:


Transaction 1
UPDATE accounts SET balance = balance – 100 WHERE acct_id = 3456;

Transaction 2
UPDATE accounts SET balance = balance * 0.005;

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 43


Schedule Example

 We'll summarize their interactions with the DBMS in the following form:

Transaction 1: r1(A), w1(A)


Transaction 2: r2(A), w2(A), r2(B), w2(B)

 One Possible Schedule - (Non – serial)


 A schedule is some interleaving of the operations from the two transactions
(without violating the order of operations within any individual transaction).

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 44


Schedule Example

Schedule S:
r1(A), r2(A), w1(A), w2(A), r2(B), w2(B)
 If account A starts with $200
 If account B starts with $100
 We can trace what would happen with Schedule S.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 45


Schedule Example

 Schedule S is very bad! (At least, it's bad if you're the bank!)
 We withdrew $100 from account A,
 But somehow the database has recorded that our account now holds $201.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 46


Schedule Example

Serial schedule
 Our idea is a serial schedule, in which all operations by a transaction are
grouped together.

 For our two transactions, • There are only two ways to arrange their operations
to get a serial schedule:

 Serial schedule 1: r1(A), w1(A), r2(A), w2(A), r2(B), w2(B)


 Serial schedule 2: r2(A), w2(A), r2(B), w2(B), r1(A), w1(A)

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 47


Schedule Example

Serial schedule
 Problem :
 In practice, a serial schedule isn't realistic, because it means we must wait for
one transaction to complete before starting another.

 Solution:
 We would really prefer to interleave them — but we need to interleave the
transactions so that they work the same as some serial schedule.

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 48


Schedule Example

serializable schedule
 As an example, consider Schedule T, which has swapped the third and
fourth operations from S:
 •
 Schedule T: r1(A), r2(A), w2(A), w1(A), r2(B), w2(B)

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 49


5.Lossless Decomposition - Example

 Now, when these two relations are joined on the common column "EMP_ID",
then the resultant relation will look like:
 Employee ⋈ Department

EMP_ID EMP_NAME EMP_AGE EMP_CITY DEPT_ID DEPT_NAM


E

22 Denim 28 Mumbai 827 Sales


33 Alina 25 Delhi 438 Marketing
46 Stephan 30 Bangalore 869 Finance

52 Katherine 36 Mumbai 575 Production

60 Jack 40 Noida 678 Testing

Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe Slide 1- 50

You might also like