33
Elmasri/Navathe, Fundamen tals of Database Systems, 4th Edition

Chapter17

Embed Size (px)

DESCRIPTION

Navate Database Management system

Citation preview

Page 1: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Page 2: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Introduction to TransactionProcessing Concepts

and Theory

Chapter 17

Page 3: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Introduction to Transaction Processing

Transaction and System Concepts

Desirable Properties of Transactions

Characterizing Schedules Based on Recoverability

Characterizing Schedules based on Serializability

Transaction Support in SQL

Chapter Outline

Page 4: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Database systems are classified as:Single-user: at most one user at a time can use the systemMulti-user: many users –simultaneously-- can use the system

Multi-user systems work in one of the following modes:

Interleaved concurrency, Parallel processing

Transaction processing systems are systems with large number of users that are executing database transactions on large databases (e.g., reservation systems, banking, credit card processing, supermarket checkout, and etc.).

Introduction to Transaction Processing

Page 5: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.1 Interleaved processing versus parallel processing of concurrent transactions.

Introduction to Transaction Processing

Page 6: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A transaction is a logical unit of database processing that includes one or more database access operations (e.g., insertion, deletion, modification, or retrieval operations).

A read-only transaction only retrieves data and thus do not change the state of the database.

The basic database access operations are:Read-item(X): reads a database item X into a program variable XWrite-item(X): writes the value of program variable X into the database item named X

Introduction to Transaction Processing

Page 7: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.2 Two sample transactions. (a) Transaction T1.

(b) Transaction T2.

Introduction to Transaction Processing

Page 8: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Problems occur when concurrent transactions execute in an uncontrolled manner:

The Lost Update Problem

The Temporary Update (or Dirty Read) Problem

The Incorrect Summary Problem

Why Concurrency Control is Needed

Page 9: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.3 (a) The lost update problem.

Why Concurrency Control is Needed

Page 10: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.3 (continued)(b) The temporary update problem.

Why Concurrency Control is Needed

Page 11: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.3 (continued)(c) The incorrect summary problem.

Why Concurrency Control is Needed

Page 12: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A DBMS must make sure that either:1) All operations in the transaction are completed, or 2) The transaction has no effect whatsoever on the

database or on any other transactions.

Transaction failure in the middle of execution:

A computer failure –e.g., main memory failureA transaction or system error –e.g., division by zeroLocal errors or exception conditions detected by the transaction –e.g., data may not be foundConcurrency control enforcement –e.g., abort the transaction, and start it laterDisk failure –e.g., read/write head crashPhysical problems and catastrophes –e.g., fire, sabotage

Why Recovery is Needed

Page 13: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A transaction is an atomic unit of work.

For recovery purpose, the system needs to keep track of when the transaction starts, terminates, and commits/aborts.

The recovery manager keeps track of the following operations:

BEGIN_TRANSACTIONREAD OR WRITEEND_TRANSACTIONCOMMIT_TRANSACTION –ended successfully ROLLBACK (OR ABORT) --ended unsuccessfully

Transactions and System Concepts

Page 14: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A transaction goes into an active state immediately after it starts execution.

When the transaction ends, it moves to the partially committed state.

If the transaction passes recovery protocol checks, then it enters the committed state, otherwise it goes to the failed state.

The terminated state corresponds to the transaction leaving the system.

Transactions and System Concepts

Page 15: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.4 State transition diagram illustrating the states for transaction

execution.

Transactions and System Concepts

Page 16: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

For recovery purpose, the system maintains a log to keep track of all transaction operations that affect the value of database items.

Log records contains of the following information

START_TRANSACTION, TWRITE_ITEM, T, X, OLD_VALUE, NEW_VALUEREAD_ITEM, T, XCOMMIT, TABORT, T

Where T refers to a unique transaction-id that is generated automatically by the system.

Log records enables undo/redoing

Transactions and System Concepts

Page 17: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Transactions should posses ACID properties:

Atomicity: it is either performed in its entirety or not performed at all.

Consistency preservation: its complete execution take(s) the database from one consistent state to another.

Isolation: its execution should not be interfered with by any other transaction executing concurrently.

Durability or Permanency: the changes applied to the database by a committed transaction must persist in the database.

Desirable Properties of Transactions

Page 18: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A schedule (or history) S of n transactions T1, T2, …, Tn is an ordering of the operations of the transactions, such that operations of Ti in S must appear in the same order in which they occur in Ti. For example, the schedule of Figure 17.3(a) is

Sa: r1(X); r2(X); w1(X); r1(Y); w2(X); w1(Y);

and the schedule for Figure 17.3(b) is Sb: r1(X); w1(X); r2(X); w2(X); r1(Y); a1;

Two operations in a schedule are conflict if:1) They belong to different transactions,2) They access the same item X, and3) At least one them is a WRITE_ITEM(X) operation.

Characterizing Schedules Based on Recoverability

Page 19: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A schedule S of n transactions T1, T2, …, Tn is said to be a complete schedule if:

1) The operations in S are exactly those operations in T1, T2, …, Tn including a commit or abort operation as the last operation for each transaction.

2) The order of operations in S is the same as their occurrence in Ti.

3) For any two conflict operations, one of the two must occur before the other in the schedule.

Characterizing Schedules Based on Recoverability

Page 20: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

For some schedule it is easy to recover from transaction failures.

We would like that, once a transaction T is committed, it should never be necessary to roll back T. The schedules that meet this criterion are called recoverable schedules. Therefore:

A schedule S is recoverable if no transaction T in S commits until all transactions T’ that have written an item that T reads have committed. For exampleS’a: r1(X); r2(X); w1(X); r1(Y); w2(X); c2; w1(Y); c1;

is recoverable, even though it suffers from the lost update problem.

Characterizing Schedules Based on Recoverability

Page 21: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Example -- Consider the following schedules:Sc: r1(X); w1(X); r2(X); r1(Y); w2(X); c2; a1;

Sd: r1(X); w1(X); r2(X); r1(Y); w2(X); w1(Y); c1; c2;

Se: r1(X); w1(X); r2(X); r1(Y); w2(X); w1(Y); a1; a2;

Sc is not recoverable, because T2 reads item X written by T1, and then T2 commits before T1 commits.

Sd is recoverable, because T2 reads item X written by T1, and then T2 commits after T1 commits and no other conflict operations exists in the schedule.

Se is similar to Sd, but T1 is aborted (instead of commit).

In this situation T2 should also abort, because the value of X it read is no longer valid (this phenomenon known as cascading rollback or cascading abort).

Characterizing Schedules Based on Recoverability

Page 22: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Because cascading rollback can be quite time-consuming, we perform cascadeless schedules.

A schedule is said to be cascadeless or to avoid cascading rollback if every transaction in the schedule reads only items that were written by committed transactions.

A more restrictive type of schedule is called strict schedule, in which transactions can neither read nor write an item X until the last transaction that wrote X has committed (or aborted)

Characterizing Schedules Based on Recoverability

Page 23: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

If no interleaving of operations is permitted, there are only two possible arrangements for executing transactions T1 and T2:

1) Execute (in sequence) all the operations of transaction T1, followed by all the operations of transaction T2.

2) Execute (in sequence) all the operations of transaction T2, followed by all the operations of transaction T1.

If interleaving of operations is allowed there will be many possible schedules.

The concept of serializability of schedules is used to identify which schedules are correct.

Characterizing Schedules Based on Serializability

Page 24: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.5 Examples of serial and nonserial schedules involving transactions T1 and T2.

(a) Serial schedule A: T1 followed by T2.

(b) Serial schedules B: T2 followed by T1.

Characterizing Schedules Based on Serializability

Page 25: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.5 (continued) Examples of serial and nonserial schedules involving

transactions T1 and T2.

(c) Two nonserial schedules C and D with interleaving of operations.

Characterizing Schedules Based on Serializability

Page 26: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A schedule S is serial, if for every transaction T participating in the schedule, all the operations of T are executed consecutively in the schedule; otherwise the schedule is called nonserial.

The drawback of serial schedules is that they limit concurrency of interleaving of operations.

Two schedules are called result equivalent if they produce the same final state of the database.

Characterizing Schedules Based on Serializability

Page 27: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.6 Two schedules that are result equivalent for the initial value of X = 100, but are not result equivalent in general.

Characterizing Schedules Based on Serializability

Page 28: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Two schedules are called conflict equivalent if the order of any two conflicting operations is the same in both schedules.

A schedule S of n transactions is serializable if it is equivalent to some serial schedule of the same n transactions.

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

Characterizing Schedules Based on Serializability

Page 29: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Algorithm for testing conflict serializability of S:

1) For each transaction Ti in schedule S, create a node,

2) If Tj executes a READ_ITEM(X) after Ti executes a WRITE_ITEM(X), create an edge Ti Tj

3) If Tj executes a WRITE_ITEM(X) after Ti executes a READ_ITEM(X), create an edge Ti Tj

4) If Tj executes a WRITE_ITEM(X) after Ti executes a WRITE_ITEM(X), create an edge Ti Tj

5) The schedule S is serializable if and only if the precedence graph has no cycle.

Characterizing Schedules Based on Serializability

Page 30: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

FIGURE 17.7 Constructing the precedence graphs for schedules A, B, C, and D from Figure 17.5 to test for conflict serializability

(a) Precedence graph for serial schedule A. (b) Precedence graph for serial schedule B. (c) Precedence graph for schedule C (not serializable). (d) Precedence graph for schedule D (serializable, equivalent to

schedule A)

Characterizing Schedules Based on Serializability

Page 31: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

Every transaction has certain characteristics attributed to it, which is specified by a SET TRANSACTION statement in SQL. The characteristics are:

Access mode: can be READ ONLY or READ WRITE

Diagnostic area size: specifies an integer value n, indicating the number of conditions that can be held simultaneously (supplies feedback information).

Isolation level: is specified using the statement ISOLATION LEVEL <isolation>Where the value for <isolation> can be READ

UNCOMMITTED, READ COMMITTED, REPEATABLE READ, or SERIALIZABLE

Transaction Support in SQL

Page 32: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

The default isolation level is SERIALIZABLE (not allowing violations that cause dirty read, unrepeatable read, and phantoms).

If a transaction executes at a lower level than SERIALIZABLE, then one or more of the following violations may occur:

Dirty read: a transaction T1 may read the update of a transaction T2, which is not committed yet,

Nonrepeatable read: T1 may read a value, and after T2 updates it, T1 reads and will see a different value,Phantoms: T1 reads a set of rows from a table, a transaction inserts a row; if T1 is repeated it will see a phantom (a row that previously did not exist).

Transaction Support in SQL

Page 33: Chapter17

Elmasri/Navathe, Fundamentals of Database Systems, 4th Edition

A sample SQL transaction:EXEC SQL WHENEVER SQLERROR GOTO UNDO;EXEC SQL SET TRANSACRTION

READ WRITEDIAGNOSTIC SIZE 5ISOLATION LEVEL SERIALIZABLE;

EXEC SQL INSERT INTO EMPLOYEE (FNAME, LNAME, SSN, DNO, SALARY) VALUES (‘Robert’, ‘Smith’, ‘991004321’, 2, 35000);

EXEC SQL UPDATE EMPLOYEE SET SALARY = SALARY * 1.1 WHERE DNO = 2;

EXEC SQL COMMIT;GOTO THE_END;

UNDO: EXEC SQL ROLLBACK;

THE_END: …;

Transaction Support in SQL