Top Banner
Overview of Transaction Management
127
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dbms sixth chapter_part-1_2011

Overview

of Transaction Management

Page 2: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane2

Chapter8 (part-1): Overview of Transaction Management (T2:Page 519-540, 550-555)

The ACID PropertiesConsistency and IsolationAtomicity and Durability

Transactions and Schedules Concurrent Execution of Transactions

Motivation for Concurrent ExecutionSerializabilityAnomalies due to Interleaved ExecutionSchedules Involving Aborted Transactions

Page 3: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane3

Chapter8 (part-1): Overview of Transaction Management (T2:Page 519-540, 550-555)

Lock-Based Concurrency Control

Strict Two-Phased Locking

Deadlocks

Performance of Locking,

Transaction Support in SQL

Creating and Terminating Transactions

What Should We Lock?

Transaction Characteristics in SQL

Page 4: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane4

Chapter8 (part-1): Overview of Transaction Management (T2:Page 519-540, 550-555)

2PL, Serializability and Recoverability

View serializability

Introduction to Lock Management

Implementing lock and unlock requests

Page 5: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane5

Overview

Transaction processing systems are systems with large databases and hundreds of concurrent users that are executing database transactions

Examples:

Reservation systems

Banking systems

They require high availability and fast response time for hundreds of concurrent users.

Page 6: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane6

Overview

A transaction is an execution of a user program, seen by the DBMS as a series or list of actions i.e. read and write operations.

For performance reasons, a DBMS has to interleave the actions of several transactions.

The interleaving is done carefully to ensure that the result of a concurrent execution of transactions should be equivalent (in its effect upon the database) to some serial, or one-at-a-time, execution of the same set of transactions.

Page 7: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane7

Overview

Transactions submitted by the various users may execute concurrently and may access and update the same database items.

If this concurrent execution is uncontrolled , it may lead to problem, such as inconsistent database.

DBMS handles concurrent executions and it is an important aspect of transaction management and is the subject of concurrency control.

Page 8: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane8

Overview

DBMS handles partial transactions, or transactions that are interrupted before they run to normal completion.

The DBMS ensures that the changes made by such partial transactions are not seen by other transactions and it is the subject of crash recovery.

Page 9: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane9

Overview

Thus, two main issues to deal with:

Failures of various kinds, such as hardware failures and system crashes: Crash Recovery

Concurrent execution of multiple transactions: concurrency control

Page 10: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane10

The ACID Properties

There are four important properties of transactions that a DBMS must ensure to maintain data in the face of concurrent access and system failures and these properties are known as ACID properties:

1. Atomicity

2. Consistency

3. Isolation

4. Durability

Page 11: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane11

Atomicity

Users should be able to regard the execution of each transaction as atomic: either all actions are carried out or none are.

A transaction is an atomic unit of processing; it is either performed in its entirety or not performed at all.

Users should not have to worry about the effect of incomplete transactions (when a system crash occurs).

The system should ensure that updates of a partially executed transaction are not reflected in the database

Page 12: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane12

Atomicity

Transactions can be incomplete for three kinds of reasons:The system may crash A transaction can be aborted, or terminated by

DBMSA transaction may encounter an unexpected

situation and decide to abort Thus a DBMS must find a way to remove the effects

of partial transactions from the database, that is, it must ensure transaction atomicity: either all of a transaction's actions are carried out, or none are.

Page 13: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane13

Consistency

Each transaction must preserve the consistency of the database. (when no concurrent execution is present)

This property is called consistency, and the DBMS assumes that it holds for each transaction.

Ensuring this property of a transaction is the responsibility of the user.

Page 14: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane14

Consistency

In general, consistency requirements include

• Explicitly specified integrity constraints such as primary keys and foreign keys

• Implicit integrity constraints

A transaction must see a consistent database i.e. a transaction is consistency preserving if its complete execution takes the database from one consistent state to another.

Page 15: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane15

Consistency

During transaction execution, the database may be temporarily inconsistent.

When the transaction completes successfully the database must be consistent

Erroneous transaction logic can lead to inconsistency

Page 16: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane16

Consistency (Example)

Transaction to transfer Rs. 50 from account A to account B:

1. read(A)

2. A := A – 50

3. write(A)

4. read(B)

5. B := B + 50

6. write(B) Consistency requirement:

The sum of A and B is unchanged by the execution of the transaction

Page 17: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane17

Isolation

A transaction should appear as though it is being executed in isolation from other transactions i.e. the execution of a transaction should not be interfered by any other transactions executing concurrently.

This property is referred to as isolation: Transactions are isolated, or protected, from the effects of concurrently scheduling other transactions.

Page 18: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane18

Isolation

The isolation property is ensured by guaranteeing that even though actions of several transactions might be interleaved, the net effect is identical to executing all transactions one after the other in some serial order.

For example, if two transactions T1 and T2 are executed concurrently, the net effect is guaranteed to be equivalent to executing T1 followed by executing T2 or executing T2 followed by executing T1.

Page 19: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane19

Durability

Once the DBMS informs the user that a transaction has been successfully completed, its effects should persist even if the system crashes before all its changes are reflected on disk.

This property is called durability.

The DBMS component that ensures atomicity and durability is called the recovery manager

Page 20: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane20

Durability

DBMS maintains a record, called the log, of all writes to the database.

The log is used to ensure durability: If the system crashes before the changes made by a completed transaction are written to disk, the log is used to remember and restore these changes when the system restarts.

Page 21: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane21

Transactions and Schedules

A transaction is seen by the DBMS as a series, or list, of actions.

The actions that can be executed by a transaction include reads and writes of database objects.

Page 22: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane22

Transactions and Schedules

Two important assumptions can be made: Transactions interact with each other only via

database read and write operations, and they are not allowed to exchange messages.

A database is a fixed collection of independent objects. When objects are added to or deleted from a database or there are relationships between database objects, some additional issues arise.

Page 23: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane23

Transactions and Schedules

For recovery purposes, the systems need to keep track of when the transaction starts, terminate, and commits or aborts.Begin_transactionRead or writeEnd_transactionCommit_transaction or Abort_transaction

Page 24: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane24

Transactions and Schedules

The action of a transaction T reading an object O is denoted as RT (O); and writing as WT (O).

In addition to reading and writing, each transaction must specify as its final action either commit (i.e., complete successfully) or abort (i.e., terminate and undo all the actions carried out so far).

AbortT denotes the action of T aborting, and CommitT denotes T committing.

Page 25: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane25

Transactions and Schedules

A schedule is a list of actions (reading, writing, aborting, or committing) from a set of transactions.

The order in which two actions of a transaction T appear in a schedule must be the same as the order in which they appear in T.

A schedule represents an actual or potential execution sequence.

Page 26: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane26

Transactions and Schedules

A schedule that contains either an abort or a commit for each transaction whose actions are listed in it is called a complete schedule.

A complete schedule must contain all the actions of every transaction that appears in it.

Page 27: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane27

Transactions and Schedules

Serial schedule If the actions of different

transactions are not interleaved i.e. transactions are executed from start to finish, one by one , schedule is serial.

Basic Assumption is each transaction preserves database consistency.

Thus serial execution of a set of transactions preserves database consistency.

Page 28: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane28

Transactions and Schedules

Serial Transaction as T1; T2 and T2; T1

Page 29: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane29

Concurrent Execution of Transactions(Motivation for concurrent execution)

The DBMS interleaves the actions of different transactions to improve performance, in terms of increased throughput or improved response times for short transactions, but not all interleaving should be allowed.

Ensuring transaction isolation while permitting such concurrent execution is difficult, but is necessary for performance reasons.

Page 30: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane30

Concurrent Execution of Transactions (Motivation for concurrent execution)

While one transaction is waiting for a page to be read in from disk, the CPU can process another transaction.

Because I/O activity can be done in parallel with CPU activity in a computer.

Overlapping I/O and CPU activity reduces the amount of time disks and processors are idle, and increases system throughput (the average number of transactions completed in a given time).

Page 31: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane31

Concurrent Execution of Transactions (Motivation for concurrent execution)

Interleaved execution of a short transaction with a long transaction usually allows the short transaction to complete quickly.

In serial execution, a short transaction could get stuck behind a long transaction leading to unpredictable delays in response time, or average time taken to complete a transaction.

Page 32: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane32

Serializability

A serializable schedule over a set S of committed transactions is a schedule whose effect on any consistent database instance is guaranteed to be identical to that of some complete serial schedule over S.

or The database instance that results from executing

the given schedule is identical to the database instance that results from executing the transactions in some serial order.

Thus a (possibly concurrent) schedule is serializable if it is equivalent to a serial schedule.

Page 33: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane33

Serializability

Even though the actions of T1 and T2 are interleaved, the result of this schedule is equivalent to running T1 and running T2.

T1’s read and write of B is not influenced by T2’s actions on A, and the net effect is the same if these actions are ‘swapped’ to obtain the serial schedule T1:T2.

Page 34: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane34

Serializability

Executing the transactions serially in different orders may produce different results, but all are presumed to be acceptable.

The DBMS makes no guarantees about which of them will be the outcome of an interleaved execution.

If T1 and T2 are submitted concurrently to a DBMS, either of these schedule could be chosen.

Page 35: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane35

Serializability (Example)

This schedule is not a serial schedule, but it is equivalent to Schedule T1;T2.

Page 36: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane36

Serializability (Example)

This concurrent schedule does not preserve the value of (A + B ).

Page 37: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane37

Serializability

DBMS might sometimes execute transactions in a way that is not equivalent to any serial execution; i.e, using a schedule that is not serializable.

This could happen for two reasons: First, the DBMS might use a concurrency control

method that ensures the executed schedule, though not itself serializable, is equivalent to some serializable schedule.

Second, SQL gives application programmers the ability to instruct the DBMS to choose non-serializable schedulers.

Page 38: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane38

Anomalies Due to Interleaved Execution

There can be three main ways in which a schedule involving two consistency preserving, committed transactions could run against a consistent database and leave it in an inconsistent state.

Two actions on the same data object conflict if at least one of them is a write.

Page 39: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane39

Anomalies Due to Interleaved Execution

The three anomalous situations can be described in terms of when the actions of two transactions T1 and T2 conflict with each other:

Write-read (WR) conflict Read-write (RW) conflictWrite-write (WW) conflict

Page 40: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane40

Reading Uncommitted Data (WR Conflicts)

A transaction T2 could read a database object A that has been modified by another transaction T1, which has not yet committed.

Such a read is called a dirty read or Temporary Update Problem.

Page 41: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane41

Reading Uncommitted Data (WR Conflicts)

Consider two transactions T1 and T2, each of which, run alone, preserves database consistency: T1 transfers Rs.100 from A to B, and T2 increments both A and B by 10 percent (e.g. annual interest is deposited into these two accounts).

Suppose that their actions are interleaved so that (1) the account transfer program T1 deducts Rs.50 from account A, then (2) the interest deposit program T2 reads the current values of accounts A and B and adds 10 percent interest to each, and then (3) the account transfer program credits Rs.50 to account B.

Page 42: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane42

WR conflict (Example)

Page 43: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane43

Reading Uncommitted Data (WR Conflicts)

The result of this schedule is different from any result that we would get by running one of the two transactions first and then the other.

The problem can be traced to the fact that the value of A written by T1 is read by T2 before T1 has completed all its changes.

Page 44: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane44

Unrepeatable Reads (RW Conflicts)

A transaction T2 could change the value of an object A that has been read by a transaction T1, while T1 is still in progress.

If T1 tries to read the value of A again, it will get a different result, even though it has not modified A in the meantime.

This situation could not arise in a serial execution of two transactions; it is called an unrepeatable read.

Page 45: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane45

Unrepeatable Reads (RW Conflicts)

Suppose A is available number of copies of a book in a library.

A transaction that places an order first reads the A, checks that it is greater than 0 and decrements it.Transaction T1 reads A and finds 1, Transaction T2 also reads A, finds 1 and

decrements A to 0Transaction T1 then tries to decrements A and

gets error since integrity constraint says A can not be Zero.

Page 46: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane46

Overwriting Uncommitted Data (WW Conflicts)

A transaction T2 could overwrite the value of an object A, which has already been modified by a transaction T1, while T1 is still in progress.

This is also known as Lost Update Problem.

Page 47: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane47

Overwriting Uncommitted Data (WW Conflicts)

Suppose that Harry and Larry are two employees, and their salaries must be kept equal.

Transaction T1 sets their salaries to Rs.10000 and transaction T2 sets their salaries to Rs. 20000.

If we execute these in the serial order T1 followed by T2, both receive the salary Rs.20000; the serial order T2 followed by T1 gives each the salary Rs. 10000.

Either of these is acceptable from a consistency standpoint (although Harry and Larry may prefer a higher salary!).

Page 48: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane48

Overwriting Uncommitted Data (WW Conflicts)

If we interleave the actions of T1 and T2: T1 sets Harry's salary to Rs.10000, T2 sets Larry's

salary to Rs.20000, T2 sets Harry's salary to Rs.20000 and finally T1 sets Larry's salary to Rs.10000.

The result is not identical to the result of either of the two possible serial executions, and the interleaved schedule is therefore not serializable.

It violates the desired consistency criterion that the two salaries must be equal.

Page 49: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane49

Schedules Involving Aborted Transactions

Definition of serializability to include aborted transactions includes all actions of aborted transactions are to be undone.

A serializable schedule over a set S of transactions is a schedule whose effect on any consistent database instance is guaranteed to be identical to that of some complete serial schedule over the set of committed transactions in S.

Page 50: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane50

Schedules Involving Aborted Transactions

This definition of serializability relies on the actions of aborted transactions being undone completely, which may be impossible in some situations.

Page 51: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane51

Schedules Involving Aborted Transactions

Now, T2 has read a value for A that should never have been there!

The aborted transactions' effects are not supposed to be visible to other transactions.

If T2 had not yet committed, cascading the abort of T1 and also aborting T2 can be done.

This process would recursively abort any transaction that read data written by T2, and so on.

But T2 has already committed, and its actions cannot be undone such a schedule is unrecoverable.

Page 52: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane52

Schedules Involving Aborted Transactions

A recoverable schedule is one in which transactions commit only after all transactions whose changes they read, also commit.

If transactions read only the changes of committed transactions, not only is the schedule recoverable, but also aborting a transaction can be accomplished without cascading the abort to other transactions.

Such a schedule is said to avoid cascading aborts.

Page 53: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane53

Schedules Involving Aborted Transactions

Suppose that a transaction T2 overwrites the value of an object A that has been modified by a transaction T1, while T1 is still in progress, and T1 subsequently aborts.

All of T1's changes to database objects are undone by restoring the value of any object that it modified to the value of the object before T1's changes.

Page 54: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane54

Schedules Involving Aborted Transactions

When T1 is aborted, and its changes are undone in this manner, T2's changes are lost as well, even if T2 decides to commit.

For example, if A originally had the value 5, then was changed by T1 to 6, and by T2 to 7, if T1 now aborts, the value of A becomes 5 again.

Even if T2 commits, its change to A is inadvertently lost.

A concurrency control technique called Strict 2PL can prevent this problem.

Page 55: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane55

Lock-based Concurrency Control

A concurrency control technique called Strict 2PL can prevent problem due to aborting the transaction.

A DBMS must be able to ensure that only serializable, recoverable schedules are allowed, and than no actions of committed transactions are lost while undoing aborted transactions.

A DBMS typically uses a locking protocol to achieve this.

Page 56: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane56

Lock-based Concurrency Control

A locking protocol is a set of rules to be followed by each transaction (and enforced by the DBMS), in order to ensure that even though actions of several transactions might be interleaved, the net effect is identical to executing all transactions in some serial order.

Different locking protocols use different types of locks, such as shared locks or exclusive locks.

Page 57: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane57

Strict Two-Phase Locking (Strict 2PL)

The most widely used locking protocol, called Strict Two-Phase Locking, or Strict 2PL, has two rules:

1. If a transaction T wants to read an object, it first requests a shared lock on the object.

(or If a transaction T wants to modify an object, it first requests a exclusive lock on the object.)

2. All locks held by a transaction are released when the transaction is completed.

Page 58: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane58

Strict Two-Phase Locking (Strict 2PL)

A transaction that has an exclusive lock can also read the object; an additional shared lock is not required.

A transaction that requests a lock is suspended until the DBMS is able to grant it the requested lock.

Page 59: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane59

Strict Two-Phase Locking (Strict 2PL)

The DBMS keeps track of the locks it has granted and ensures that if a transaction holds an exclusive lock on an object, no other transaction holds a shared or exclusive lock on the same object.

Requests to acquire and release locks can be automatically inserted into transactions by the DBMS; users need not worry about these details.

Page 60: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane60

Strict Two-Phase Locking (Strict 2PL)

The locking protocol allows only ‘safe’ interleaving of transactions.

If two transactions access completely independent parts of the database, they will be able to concurrently obtain the locks that they need and proceed on their ways.

If two transactions access the same object, and one of them wants to modify it, their actions are effectively ordered serially

Page 61: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane61

Strict Two-Phase Locking (Strict 2PL)

All actions of one of these transactions (the one that gets the lock on the common object first) are completed before (this lock is released and) the other transaction can proceed.

Action of a transaction T requesting a shared lock on object O can be denoted as ST (O)

Action of a transaction T requesting a exclusive lock on object O can be denoted as XT (O)

Page 62: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane62

Strict Two-Phase Locking (Strict 2PL)

Page 63: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane63

Strict Two-Phase Locking (Strict 2PL)

First, T1would obtain an exclusive lock on A and then read and write A

Then, T2 requests a lock on A. But this request cannot be granted until T1 releases

its exclusive lock on A and the DBMS therefore suspends T2.

T1 now proceeds to obtain an exclusive lock on B, reads and writes B, then finally commits, at which time its locks are released.

T2's lock request is now granted, and it proceeds.

Page 64: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane64

Strict Two-Phase Locking (Strict 2PL)

The actions of different transactions can be interleaved if lock is sharable.

Page 65: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane65

Deadlocks

Example: Transaction T1 sets an exclusive lock on object A,

T2 sets an exclusive lock on B, T1 requests an exclusive lock on B and is queued, and T2 requests an exclusive lock on A and is queued.

T1 waiting for T2 to release its lock and T2 is waiting for T1 to release its locks.

Such a cycle of transactions waiting for locks to be released is called as Deadlock.

These two transactions will make no further progress.

Page 66: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane66

Deadlocks

They hold locks that may be required by other transactions.

The DBMS must either prevent or detect and resolve deadlocks.

Timeout mechanism can be used to identify deadlock.

If a transaction has been waiting too long for a lock, it is assumed that it is in deadlock and can be aborted.

Page 67: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane67

Performance of Locking

Lock based schemes are designed to resolve conflicts between transactions and use two basic mechanisms: blocking and aborting.

Both mechanism involve a performance penalty: blocked transactions may hold locks that force other

transactions to waitaborting and restarting a transaction wastes the

work done thus far by that transaction. A deadlock represents an extreme instance of blocking

in which a set of transactions is forever blocked unless one of the deadlocked transactions is aborted by the DBMS.

Page 68: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane68

Performance of Locking

Fewer than 1% of transactions are involved in a deadlock, and there are relatively few aborts.

The overhead of locking comes primarily from delays due to blocking.

How blocking delays affect throughput. The first few transactions are unlikely to conflict and

throughput rises in proportion to the number of active transactions.

As more and more transactions execute concurrently on the same number of database objects, the likelihood of their blocking each other goes up.

Page 69: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane69

Performance of Locking

Thus, delays due to blocking increase with the number of active transactions and throughput increases more slowly than the number of active transactions.

There comes a point when adding another active transaction actually reduces throughput: the new transaction is blocked and effectively competes with existing transactions.

We say that the system thrashes at this point.

Page 70: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane70

Performance of Locking

Page 71: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane71

Performance of Locking

If a database system begins to thrash, the database administrator should reduce the number of transactions allowed to run concurrently.

Thrashing is to be seen to occur when 30% of active transactions are blocked, and a DBA should monitor the fraction of blocked transactions to see if the system is at risk of thrashing.

Page 72: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane72

Performance of Locking

Throughput can be increased in three ways: By locking the smallest sized objects possible

(reducing the likelihood that two transactions need the same lock).

By reducing the time that transaction hold locks (so that other transactions are blocked for a shorter time).

By reducing hot spots: A hot spot is a database object that is frequently accessed and modified and cause a lot of blocking delays. Hot spots can significantly affect performance.

Page 73: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane73

Transaction support in SQL

Creating and Terminating Transactions A transaction is automatically started when a user

executes a statement that accesses either the database or the catalogs, such as a SELECT query, an UPDATE command, or CREATE table statement.

Once a transaction is started other statements can be executed as part of this transaction until the transaction is terminated by either a COMMIT command or a ROLLBACK command.

Two features for support

Page 74: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane74

Transaction support in SQL

In SQL: 1999, two new features are provided to support applications that involve long running transactions, or that must run several transactions one after the other.

Because all the actions of a given transactions are executed in order, regardless of how the actions of different transactions are interleaved, each transaction can be thought as a sequence of steps.

Page 75: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane75

Transaction support in SQL

1. savepoint: savepoint allows us to identify a point in a

transaction and selectively roll back operations carried out after this point.

This is especially useful if the transaction carries out what-if kinds of operations and wishes to undo or keep the changes based on the results.

In a long running transaction, a series of savepoints can be defined.

Page 76: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane76

Transaction support in SQL

The savepoint command allows us to give each savepoint a name:

SAVEPOINT< savepoint name >

A subsequent rollback command can specify the save point to roll back to

ROLLBACK TO SAVEPOINT < savepoint name >

Page 77: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane77

Transaction support in SQL (Example)

If we define three savepoints A, B and C in that order, and then rollback to A, all operations since A are undone, including the creation of savepoints B and C.

The savepoint A is itself undone when we rollback to it, and we must re-establish it if we wish to able to rollback to it again.

From a locking standpoint, locks obtained after savepoint, A can be released when we rollback to A.

Page 78: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane78

Transaction support in SQL

Operations between two consecutive savepoints can be treated as a new transaction.

The save point mechanism offers advantages:We can rollback over several save points. We can rollback only the most recent

transaction, which is equivalent to rolling back to the most recent savepoint.

The overhead of initiating several transaction is avoided.

Page 79: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane79

Transaction support in SQL

2. Chained transaction: Even with the use of savepoints, certain

applications might require us to run several transactions one after the other.

To minimize, the overhead in such situations, SQL: 1999 introduces another feature, called chained transactions.

We can commit or rollback a transaction and immediately initiate another transaction. This is done by using the optional key words AND CHAIN in the COMMIT and ROLLBACK statements.

Page 80: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane80

What Should We Lock?

Example: If a transaction T1 has query

SELECT S.rating, MIN(S.age)

FROM Sailors S

WHERE S.rating = 8 Transaction T2 has SQL statement that modifies

the age of a given sailor ‘joe’ with rating = 8 The DBMS could set a shared lock on the entire

sailors table for T1 and set an exclusive lock on sailors for T2, which would ensure that the two transactions are executed in a serializable manner.

Page 81: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane81

What Should We Lock?

This approach yields low concurrency and we can do better by locking smaller objects, reflecting what each transaction actually accesses.

DBMS could set a shared lock on every row with rating = 8 for transaction T1 and set an exclusive lock on just the row for the modified tuple for transaction T2.

Other read-only transactions that do not involve rating = 8 rows can proceed without waiting for T1 or T2.

Page 82: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane82

What Should We Lock?

The DBMS can lock objects at different granularities. We can lock entire tables or set row-level locks. The row-level locks approach is taken in current

systems because it offers much better performance. While row-level locking is generally better, the choice of

locking granularity is complicated. Thus, a transaction that examines several rows and

modifies those that satisfy some condition might be best served by setting shared locks on entire table and setting exclusive locks on those rows it want to modify.

Page 83: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane83

What Should We Lock?

Because SQL statements conceptually access a collection of rows described by a selection predicate, having shared lock on few rows may create another problem i.e. phantom problem

Example:

Transaction T1 accesses all rows with rating = 8.

If this could be dealt with by setting shared locks on all rows in sailors that had rating =8.

And a SQL statements that inserts a new sailors with rating = 8 and runs as transaction T3.

Page 84: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane84

What Should We Lock?

Suppose that the DBMS sets shared locks on every existing sailors row with rating = 8 for T1.

This does not prevent transaction T3 from creating a brand new row with rating = 8 and setting an exclusive lock on this row.

If this new row has a smaller age value than existing row, this returns an answer that depends on when it executed relative to T2.

Locking scheme imposes no relative order on these two transactions.

Page 85: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane85

What Should We Lock?

Following phenomenon is called the phantom problem:

A transaction retrieves a collection of objects twice and sees different results, even though it does not modify any of these tuples, itself.

To prevent phantoms, the DBMS must conceptually lock all possible rows with rating = 8 on behalf of T1.

One way to do this is to lock the entire table, at the cost of low concurrency.

It is possible to take advantage of indexes to do better.

Page 86: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane86

What Should We Lock?

It may be that the application invoking T1 can accept the potential inaccuracy due to phantoms.

The approach of setting shared locks on existing tuples for T1 is adequate, and offers better performance.

SQL allows a programmer to make this choice- and other similar choices explicitly .

Page 87: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane87

Transaction Characteristics in SQL

In order to give programmers control over the locking overhead incurred by their transactions, SQL allows them to specify three characteristics of a transaction: Access modeDiagnostic sizeIsolation level.

The diagnostics size determines the number of error conditions that can be recorded.

DIAGNOSTIC SIZE n

Page 88: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane88

Transaction Characteristics in SQL

If the access mode is READ ONLY the transaction is not allowed to modify the database.

To execute one of INSERT, DELETE, UPDATE, CREATE commands, the access mode should be set to READ WRITE.

For transactions with read only access mode, only shared locks need to be obtained, thus increase concurrency.

Page 89: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane89

Transaction Characteristics in SQL

The isolation level controls the extent to which a given transaction is exposed to the actions of other transactions executing concurrently.

By choosing one of four possible isolation level settings, a user can obtain greater concurrency at the cost of increasing the transaction’s exposure to other transactions uncommitted changes.

Page 90: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane90

Transaction Characteristics in SQL

Isolation level choices are

READ UNCOMMITTED

READ COMMITTD

REPEATABLE READ

SERIALIZABLE.

Page 91: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane91

Transaction Characteristics in SQL (SERIALIZABLE)

The highest degree of isolation from the effect of other transactions is achieved by setting the isolation level for a transaction T to SERIALIZABLE.

This isolation level ensures that T reads only the changes made by committed transactions.

And that if T reads a set of values based on some search condition, this set is not changed by other transactions until T is complete.

Page 92: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane92

Transaction Characteristics in SQL (SERIALIZABLE)

In terms of a lock based implementation a SERIALIZABLE transaction obtains locks before reading or writing objects.

Including locks on sets of objects that it requires to be unchanged and holds them until the end, according to strict 2PL.

Page 93: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane93

Transaction Characteristics in SQL (REPEATABLE READ )

REPEATABLE READ ensures that T reads only the changes made by committed transactions and no value read or written by T is changed by any other transaction until T is complete.

However, T could experience the phantom phenomenon.

Eg. While T examines all sailors records with rating = 1 another transaction might add a new such sailors record, which is missed by T.

Page 94: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane94

Transaction Characteristics in SQL

A REPEATABLE READ transaction sets the same locks as a SERIALIZABLE transaction except that it does not do index locking, i.e, it locks only individual objects, not sets of objects.

Page 95: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane95

Transaction Characteristics in SQL

READ COMMITTED ensures that T reads only the changes made by committed transactions, and that no value written by T is changed by any other transaction until T is complete.

However, a value read by T may well be modified by another transaction while T is still in progress, and T is exposed to the phantom problem.

Page 96: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane96

Transaction Characteristics in SQL

A READ COMMITTED transaction obtains exclusive locks before writing objects and holds these locks until the end.

It also obtains shared locks before reading objects, but these locks are released immediately, their only effect is to guarantee that the transaction that last modified the object is complete.

Page 97: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane97

Transaction Characteristics in SQL

A READ UNCOMMITTED transaction T can read changes made to an object by an ongoing transaction, obviously the object can be changed further while T is in progress and T is also vulnerable to the phantom problem.

Page 98: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane98

Transaction Characteristics in SQL

A READ UNCOMMITTED transaction does not obtain shared locks before reading objects.

This mode represents the greatest exposure to uncommitted changes of other transactions: so much so that SQL prohibits such a transaction from making any changes itself.

A READ UNCOMMITTED transaction is required to have an access mode of READ ONLY.

Since such a transaction obtains no locks for reading objects and it is not allowed to write objects, it never makes any lock requests.

Page 99: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane99

Transaction Characteristics in SQL

The serializable isolation level is generally the safest and is recommended for most transactions.

Some transactions, however can run with a lower isolation level and the smaller number of locks requested can contribute to improved system performance.

Page 100: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane100

Transaction Characteristics in SQL

Eg. A statistical query that finds the average sailor age can be run at the READ COMMITED level or even the READ UNCOMMITED level, because a few incorrect or missing values do not significantly affect the result if the number of sailors is large.

The isolation level and access mode can be set using the SET TRANSACTION command.

Page 101: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane101

Transaction Characteristics in SQL

Eg. The following command declares the current transaction to be SERIALIZALE and READ ONLY>

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY

When a transaction is started , the default is SERIALIZABLE and READ WRITE.

Page 102: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane102

2PL, Serializability, and Recoverability

Locking protocols guarantee some important properties of schedules: serializability and recoverability.

Two actions from same schedule, from different transactions conflict if they operate on the same data object and at least one of them is a write.

Two schedules are said to be conflict equivalent if they involve the (same set of) actions of the same transactions and they order every pair of conflicting actions of two committed transactions in the same way.

Page 103: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane103

2PL, Serializability, and Recoverability

The outcome of a schedule depends only on the order of conflicting operations

We can interchange any pair of nonconflicting operations without altering the effect of the schedule on the database.

A schedule is conflict serializable if it is conflict equivalent to some serial schedule.

Every conflict serializable schedule is serializable

Page 104: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane104

2PL, Serializability, and Recoverability

However, some serializable schedules are not conflict serializable.

This schedule is equivalent to executing the transactions T1 T2 T3 serially in the order T1, T2, T3 (as it looks), but it is not conflict equivalent to this serial schedule because the writes of T1 and T2 are ordered differently.

Page 105: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane105

2PL, Serializability, and Recoverability

It is useful to capture all potential conflicts between the transactions in a schedule in a precedence graph, also called a serializability graph.

The precedence graph for a schedule S contains:A node for each committed transaction in S.An arc from Ti to Tj if an action of Ti precedes

and conflicts with one of Tj's actions.

Page 106: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane106

2PL, Serializability, and Recoverability

Page 107: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane107

2PL, Serializability, and Recoverability

The Strict 2PL protocol allows only serializable schedules, as is seen from the following two results:

1. A schedule S is conflict serializable if and only if its precedence graph is acyclic.

2. Strict 2PL ensures that the precedence graph for any schedule that it allows, is acyclic

Page 108: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane108

2PL, Serializability, and Recoverability

A widely studied variant of Strict 2PL, called Two-Phase Locking (2PL), relaxes the second rule of Strict 2PL to allow transactions to release locks before the end, that is, before the commit or abort action.

For 2PL, the second rule is replaced by the following rule:

(2PL) (2) A transaction cannot request additional locks once it releases any lock.

Thus, every transaction has a `growing' phase in which it acquires locks, followed by a `shrinking' phase in which it releases locks.

Page 109: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane109

2PL, Serializability, and Recoverability

Even (nonstrict) 2PL ensures acyclicity of the precedence graph and therefore allows only serializable schedules.

A schedule is said to be strict if a value written by a transaction T is not read or overwritten by other transactions until T either aborts or commits.

Strict schedules are recoverable, do not require cascading aborts, and actions of aborted transactions can be undone by restoring the original values of modified objects.

Page 110: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane110

2PL, Serializability, and Recoverability

Strict 2PL improves upon 2PL by guaranteeing that every allowed schedule is strict, in addition to being conflict serializable.

The reason is that when a transaction T writes an object under Strict 2PL, it holds the (exclusive) lock until it commits or aborts.

Thus, no other transaction can see or modify this object until T is complete.

Page 111: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane111

2PL, Serializability, and Recoverability

View Serializability Conflict serializability is sufficient but not necessary

for serializability.

A more general sufficient condition is view serializability.

Two schedules S1 and S2 over the same set of transactions - any transaction that appears in either S1 or S2 must also appear in the other -- are view equivalent under following conditions:

Page 112: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane112

2PL, Serializability, and Recoverability

1. If Ti reads the initial value of object A in S1, it must also read the initial value of A in S2.

2. If Ti reads a value of A written by Tj in S1, it must also read the value of A written by Tj in S2.

3. For each data object A, the transaction (if any) that performs the final write on A in S1 must also perform the final write on A in S2.

A schedule is view serializable if it is view equivalent to some serial schedule.

Every conflict serializable schedule is view serializable, although the converse is not true.

Page 113: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane113

Lock Management

The part of the DBMS that keeps track of the locks issued to transactions is called the lock manager.

The lock manager maintains a lock table, which is a hash table with the data object identifier as the key.

The DBMS also maintains a descriptive entry for each transaction in a transaction table, and among other things, the entry contains a pointer to a list of locks held by the transaction.

Page 114: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane114

Lock Management

A lock table entry for an object - which can be a page, a record, and so on, depending on the DBMS.

It contains the following information:

the nature of the lock (shared or exclusive), and a pointer to a queue of lock requests

the number of transactions currently holding a lock on the object (this can be more than one if the object is locked in shared mode)

Page 115: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane115

Implementing Lock and Unlock Requests

According to the Strict 2PL protocol, before a transaction T reads or writes a database object O, it must obtain a shared or exclusive lock on O and must hold on to the lock until it commits or aborts.

Page 116: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane116

Implementing Lock and Unlock Requests

When a transaction needs a lock on an object, it issues a lock request to the lock manager:

1. If a shared lock is requested, the queue of requests is empty, and the object is not currently locked in exclusive mode, the lock manager grants the lock and updates the lock table entry for the object (indicating that the object is locked in shared mode, and incrementing the number of transactions holding a lock by one).

Page 117: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane117

Implementing Lock and Unlock Requests

2. If an exclusive lock is requested, and no transaction currently holds a lock on the object (which also implies the queue of requests is empty), the lock manager grants the lock and updates the lock table entry.

3. Otherwise, the requested lock cannot be immediately granted, and the lock request is added to the queue of lock requests for this object. The transaction requesting the lock is suspended.

Page 118: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane118

Implementing Lock and Unlock Requests

When a transaction aborts or commits, it releases all its locks.

When a lock on an object is released, the lock manager updates the lock table entry for the object and examines the lock request at the head of the queue for this object.

If this request can now be granted, the transaction that made the request is woken up and given the lock.

Indeed, if there are several requests for a shared lock on the object at the front of the queue, all of these requests can now be granted together.

Page 119: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane119

Implementing Lock and Unlock Requests

If T1 has a shared lock on O, and T2 requests an exclusive lock, T2's request is queued.

Now, if T3 requests a shared lock, its request enters the queue behind that of T2, even though the requested lock is compatible with the lock held by T1.

This rule ensures that T2 does not starve, that is, wait indefinitely while a stream of other transactions acquire shared locks and thereby prevent T2 from getting the exclusive lock that it is waiting for.

Page 120: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane120

Atomicity of Locking and Unlocking

The implementation of lock and unlock commands must ensure that these are atomic operations.

To ensure atomicity of these operations when several instances of the lock manager code can execute concurrently, access to the lock table has to be guarded by an operating system synchronization mechanism such as a semaphore.

Page 121: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane121

Atomicity of Locking and Unlocking

Suppose that a transaction requests an exclusive lock.

The lock manager checks and finds that no other transaction holds a lock on the object and therefore decides to grant the request.

But in the meantime, another transaction might have requested and received a conflicting lock.

To prevent this, the entire sequence of actions in a lock request call (checking to see if the request can be granted, updating the lock table, etc.) must be implemented as an atomic operation.

Page 122: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane122

Additional Issues: Lock Upgrades, Convoys, Latches

The DBMS maintains a transaction table, which contains (among other things) a list of the locks currently held by a transaction.

This list can be checked before requesting a lock, to ensure that the same transaction does not request the same lock twice.

However, a transaction may need to acquire an exclusive lock on an object for which it already holds a shared lock.

Page 123: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane123

Additional Issues: Lock Upgrades, Convoys, Latches

Such a lock upgrade request is handled specially by granting the write lock immediately if no other transaction holds a shared lock on the object and inserting the request at the front of the queue otherwise.

The rationale for favoring the transaction thus is that it already holds a shared lock on the object and queuing it behind another transaction that wants an exclusive lock on the same object causes both transactions to wait for each other and therefore be blocked forever

Page 124: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane124

Additional Issues: Lock Upgrades, Convoys, Latches

This interleaving interacts with the operating system's scheduling of processes' access to the CPU and can lead to a situation called a convoy, where most of the CPU cycles are spent on process switching.

The problem is that a transaction T holding a heavily used lock may be suspended by the operating system.

Page 125: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane125

Additional Issues: Lock Upgrades, Convoys, Latches

Until T is resumed, every other transaction that needs this lock is queued.

Such queues, called convoys, can quickly become very long; a convoy, once formed, tends to be stable.

Convoys are one of the drawbacks of building a DBMS on top of a general-purpose operating system with preemptive scheduling.

Page 126: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane126

Additional Issues: Lock Upgrades, Convoys, Latches

In addition to locks, which are held over a long duration, a DBMS also supports short duration latches.

Setting a latch before reading or writing a page ensures that the physical read or write operation is atomic; otherwise, two read/write operations might conflict

Latches are unset immediately after the physical read or write operation is completed.

Page 127: Dbms sixth chapter_part-1_2011

04/10/23Lecture presentation by Neelam Bawane127

End of Chapter 6(part-1)