[2].Transaction From Sixth Edition Korth

Embed Size (px)

Citation preview

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    1/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    2/53

    Silberschatz, Korth and Sudarshan14.2Database System Concepts - 6thEdition

    Chapter 14: Transactions

    Transaction Concept

    Transaction State

    Concurrent Executions

    Serializability

    Recoverability

    Implementation of Isolation Transaction Definition in SQL

    Testing for Serializability.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    3/53

    Silberschatz, Korth and Sudarshan14.3Database System Concepts - 6thEdition

    Transaction Concept

    A transactionis a unit of program execution that accesses and

    possibly updates various data items. E.g. transaction to transfer $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)

    Two main issues to deal with:

    Failures of various kinds, such as hardware failures and systemcrashes

    Concurrent execution of multiple transactions

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    4/53

    Silberschatz, Korth and Sudarshan14.4Database System Concepts - 6thEdition

    Example of Fund Transfer

    Transaction to transfer $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)

    Atomicity requirement

    if the transaction fails after step 3 and before step 6, money will be lostleading to an inconsistent database state

    Failure could be due to software or hardware

    the system should ensure that updates of a partially executed transaction

    are not reflected in the database Durabili ty requirement once the user has been notified that the transaction

    has completed (i.e., the transfer of the $50 has taken place), the updates to thedatabase by the transaction must persist even if there are software orhardware failures.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    5/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    6/53

    Silberschatz, Korth and Sudarshan14.6Database System Concepts - 6thEdition

    Example of Fund Transfer (Cont.)

    Isolation requirement if between steps 3 and 6, another

    transaction T2 is allowed to access the partially updated database, itwill see an inconsistent database (the sum A + Bwill be less than itshould be).

    T1 T2

    1. read(A)

    2. A:=A 50

    3. write(A) read(A), read(B), print(A+B)

    4. read(B)

    5. B:= B + 50

    6. write(B

    Isolation can be ensured trivially by running transactions serially

    that is, one after the other.

    However, executing multiple transactions concurrently has significantbenefits, as we will see later.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    7/53Silberschatz, Korth and Sudarshan14.7Database System Concepts - 6thEdition

    ACID Properties

    Atomicity. Either all operations of the transaction are properly reflectedin the database or none are.

    Consistency. Execution of a transaction in isolation preserves the

    consistency of the database. Isolation. Although multiple transactions may execute concurrently,

    each transaction must be unaware of other concurrently executingtransactions. Intermediate transaction results must be hidden from otherconcurrently executed transactions.

    That is, for every pair of transactions Tiand Tj, it appears to Tithateither Tj, finished execution before Tistarted, or Tjstarted executionafter Tifinished.

    Durability. After a transaction completes successfully, the changes ithas made to the database persist, even if there are system failures.

    A transaction is a unit of program execution that accesses and possibly

    updates various data items.To preserve the integrity of data the databasesystem must ensure:

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    8/53Silberschatz, Korth and Sudarshan14.8Database System Concepts - 6thEdition

    Transaction State

    Activethe initial state; the transaction stays in this state while it is

    executing Partially committedafter the final statement has been executed.

    Failed-- after the discovery that normal execution can no longerproceed.

    Aborted after the transaction has been rolled back and the

    database restored to its state prior to the start of the transaction.Two options after it has been aborted:

    restart the transaction

    can be done only if no internal logical error

    kill the transaction

    Committed after successful completion.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    9/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    10/53Silberschatz, Korth and Sudarshan14.10Database System Concepts - 6thEdition

    Concurrent Executions

    Multiple transactions are allowed to run concurrently in the system.

    Advantages are: increased processor and disk util ization , leading to better

    transaction throughput

    E.g. one transaction can be using the CPU while another isreading from or writing to the disk

    reduced average response timefor transactions: shorttransactions need not wait behind long ones.

    Concurrency contro l schemes mechanisms to achieve isolation

    that is, to control the interaction among the concurrenttransactions in order to prevent them from destroying the

    consistency of the databaseWill study in Chapter 16, after studying notion of correctness

    of concurrent executions.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    11/53Silberschatz, Korth and Sudarshan14.11Database System Concepts - 6thEdition

    Schedules

    Schedule a sequences of instructions that specify the chronological

    order in which instructions of concurrent transactions are executed a schedule for a set of transactions must consist of all instructions

    of those transactions

    must preserve the order in which the instructions appear in eachindividual transaction.

    A transaction that successfully completes its execution will have acommit instructions as the last statement

    by default transaction assumed to execute commit instruction as itslast step

    A transaction that fails to successfully complete its execution will have

    an abort instruction as the last statement

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    12/53Silberschatz, Korth and Sudarshan14.12Database System Concepts - 6thEdition

    Schedule 1

    Let T1transfer $50 fromA to B, and T2transfer 10% of thebalance fromA to B.

    A serial schedule in which T1is followed by T2:

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    13/53Silberschatz, Korth and Sudarshan14.13Database System Concepts - 6thEdition

    Schedule 2

    A serial schedule where T2is followed by T1

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    14/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    15/53Silberschatz, Korth and Sudarshan14.15Database System Concepts - 6thEdition

    Schedule 4

    The following concurrent schedule does not preserve the

    value of (A + B).

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    16/53Silberschatz, Korth and Sudarshan14.16Database System Concepts - 6thEdition

    Serializability

    Basic Assumption Each transaction preserves database

    consistency. Thus serial execution of a set of transactions preserves

    database consistency.

    A (possibly concurrent) schedule is serializable if it isequivalent to a serial schedule. Different forms of schedule

    equivalence give rise to the notions of:1. conflict serializability

    2. view serializability

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    17/53Silberschatz, Korth and Sudarshan14.17Database System Concepts - 6thEdition

    Simplified view of transactions

    We ignore operations other than readand writeinstructions

    We assume that transactions may perform arbitrarycomputations on data in local buffers in between readsand writes.

    Our simplified schedules consist of only readand writeinstructions.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    18/53Silberschatz, Korth and Sudarshan14.18Database System Concepts - 6thEdition

    Conflicting Instructions

    Instructions liand ljof transactions Tiand Tjrespectively, conflict

    if and only if there exists some item Qaccessed by both liand lj,and at least one of these instructions wrote Q.

    1. li= read(Q), lj= read(Q). liand ljdont conflict.2. li= read(Q), lj= write(Q). They conflict.3. li= write(Q), lj= read(Q). They conflict4. li= write(Q), lj= write(Q). They conflict

    Intuitively, a conflict between liand ljforces a (logical) temporalorder between them.

    If liand ljare consecutive in a schedule and they do notconflict, their results would remain the same even if they hadbeen interchanged in the schedule.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    19/53Silberschatz, Korth and Sudarshan14.19Database System Concepts - 6thEdition

    Conflict Serializabil ity

    If a schedule Scan be transformed into a schedule Sby a series of

    swaps of non-conflicting instructions, we say that Sand Sareconf lict equivalent.

    We say that a schedule Sis confl ict serializableif it is conflictequivalent to a serial schedule

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    20/53Silberschatz, Korth and Sudarshan14.20Database System Concepts - 6thEdition

    Conflict Serializability (Cont.)

    Schedule 3 can be transformed into Schedule 6, a serial

    schedule where T2follows T1, by series of swaps of non-conflicting instructions. Therefore Schedule 3 is conflictserializable.

    Schedule 3 Schedule 6

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    21/53Silberschatz, Korth and Sudarshan14.21Database System Concepts - 6thEdition

    Conflict Serializability (Cont.)

    Example of a schedule that is not conflict serializable:

    We are unable to swap instructions in the above schedule toobtain either the serial schedule < T3, T4>, or the serialschedule < T4, T3>.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    22/53Silberschatz, Korth and Sudarshan14.22Database System Concepts - 6thEdition

    View Serializability

    Let Sand Sbe two schedules with the same set of transactions. S

    and Sare view equivalentif the following three conditions are met,for each data item Q,

    1. If in schedule S, transaction Tireads the initial value of Q, then inschedule Salso transaction Ti must read the initial value of Q.

    2. If in schedule S transaction Tiexecutes read(Q), and that value

    was produced by transaction Tj (if any), then in schedule Salsotransaction Timust read the value of Qthat was produced by thesame write(Q) operation of transaction Tj.

    3. The transaction (if any) that performs the final write(Q) operationin schedule S must also perform the finalwrite(Q) operation inschedule S.

    As can be seen, view equivalence is also based purely on reads andwritesalone.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    23/53

    Silberschatz, Korth and Sudarshan14.23Database System Concepts - 6thEdition

    View Serializability (Cont.)

    A schedule Sis view serializableif it is view equivalent to a serial

    schedule. Every conflict serializable schedule is also view serializable.

    Below is a schedule which is view-serializable but not conflictserializable.

    What serial schedule is above equivalent to?

    Every view serializable schedule that is not conflict serializable hasblind writes.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    24/53

    Silberschatz, Korth and Sudarshan14.24Database System Concepts - 6thEdition

    Other Notions of Serializabil ity

    The schedule below produces same outcome as the serial

    schedule < T1,T5>, yet is not conflict equivalent or viewequivalent to it.

    Determining such equivalence requires analysis of operationsother than read and write.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    25/53

    Silberschatz, Korth and Sudarshan14.25Database System Concepts - 6thEdition

    Testing for Serializabil ity

    Consider some schedule of a set of transactions T1, T2, ..., Tn

    Precedence graph a directed graph where the verticesare the transactions (names).

    We draw an arc from Tito Tjif the two transaction conflict,and Tiaccessed the data item on which the conflict aroseearlier.

    We may label the arc by the item that was accessed. Example 1

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    26/53

    Silberschatz, Korth and Sudarshan14.26Database System Concepts - 6thEdition

    Test for Conflict Serializabil ity

    A schedule is conflict serializable if and only

    if its precedence graph is acyclic. Cycle-detection algorithms exist which take

    order n2time, where n is the number ofvertices in the graph.

    (Better algorithms take order n+ e

    where eis the number of edges.) If precedence graph is acyclic, the

    serializability order can be obtained by atopological sortingof the graph.

    This is a linear order consistent with thepartial order of the graph.

    For example, a serializability order forSchedule A would beT5T1T3T2T4

    Are there others?

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    27/53

    Silberschatz, Korth and Sudarshan14.27Database System Concepts - 6thEdition

    Test for View Serializability

    The precedence graph test for conflict serializability cannot be used

    directly to test for view serializability. Extension to test for view serializability has cost exponential in the

    size of the precedence graph.

    The problem of checking if a schedule is view serializable falls in theclass of NP-complete problems.

    Thus existence of an efficient algorithm is extremelyunlikely. However practical algorithms that just check some sufficient

    conditionsfor view serializability can still be used.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    28/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    29/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    30/53

    Silberschatz, Korth and Sudarshan14.30Database System Concepts - 6thEdition

    Cascadeless Schedules

    Cascadelessschedules cascading rollbacks cannot occur; for

    each pair of transactions Tiand Tjsuch that Tj reads a data itempreviously written by Ti, the commit operation of Ti appears before theread operation of Tj.

    Every cascadeless schedule is also recoverable

    It is desirable to restrict the schedules to those that are cascadeless

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    31/53

    Silberschatz, Korth and Sudarshan14.31Database System Concepts - 6thEdition

    Concurrency Control

    A database must provide a mechanism that will ensure that all possible

    schedules are either conflict or view serializable, and

    are recoverable and preferably cascadeless

    A policy in which only one transaction can execute at a time generatesserial schedules, but provides a poor degree of concurrency

    Are serial schedules recoverable/cascadeless?

    Testing a schedule for serializability afterit has executed is a little toolate!

    Goal to develop concurrency control protocols that will assureserializability.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    32/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    33/53

    Silberschatz, Korth and Sudarshan14.33Database System Concepts - 6thEdition

    Concurrency Control vs. Serializabil ity Tests

    Concurrency-control protocols allow concurrent schedules, but ensurethat the schedules are conflict/view serializable, and are recoverableand cascadeless .

    Concurrency control protocols generally do not examine theprecedence graph as it is being created

    Instead a protocol imposes a discipline that avoids nonseralizableschedules.

    We study such protocols in Chapter 16.

    Different concurrency control protocols provide different tradeoffsbetween the amount of concurrency they allow and the amount of

    overhead that they incur. Tests for serializability help us understand why a concurrency control

    protocol is correct.

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    34/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    35/53

    Silberschatz, Korth and Sudarshan14.35Database System Concepts - 6thEdition

    Levels of Consistency in SQL-92

    Serializable default

    Repeatable readonly committed records to be read, repeatedreads of same record must return same value. However, atransaction may not be serializable it may find some recordsinserted by a transaction but not find others.

    Read committedonly committed records can be read, butsuccessive reads of record may return different (but committed)values.

    Read uncommittedeven uncommitted records may be read.

    Lower degrees of consistency useful for gathering approximate

    information about the database Warning: some database systems do not ensure serializable

    schedules by default

    E.g. Oracle and PostgreSQL by default support a level ofconsistency called snapshot isolation (not part of the SQL

    standard)

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    36/53

    Silberschatz, Korth and Sudarshan14.36Database System Concepts - 6thEdition

    Transaction Definit ion in SQL

    Data manipulation language must include a construct for

    specifying the set of actions that comprise a transaction. In SQL, a transaction begins implicitly.

    A transaction in SQL ends by:

    Commit workcommits current transaction and begins a newone.

    Rollback workcauses current transaction to abort.

    In almost all database systems, by default, every SQL statementalso commits implicitly if it executes successfully

    Implicit commit can be turned off by a database directive

    E.g. in JDBC, connection.setAutoCommit(false);

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    37/53

    Fi 14 01

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    38/53

    Silberschatz, Korth and Sudarshan14.38Database System Concepts - 6thEdition

    Figure 14.01

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    39/53

    Fi 14 03

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    40/53

    Silberschatz, Korth and Sudarshan14.40Database System Concepts - 6thEdition

    Figure 14.03

    Fi 14 04

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    41/53

    Silberschatz, Korth and Sudarshan14.41Database System Concepts - 6thEdition

    Figure 14.04

    Fi 14 05

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    42/53

    Silberschatz, Korth and Sudarshan14.42Database System Concepts - 6thEdition

    Figure 14.05

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    43/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    44/53

    Fi 14 08

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    45/53

    Silberschatz, Korth and Sudarshan14.45Database System Concepts - 6thEdition

    Figure 14.08

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    46/53

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    47/53

    Figure 14 11

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    48/53

    Silberschatz, Korth and Sudarshan14.48Database System Concepts - 6thEdition

    Figure 14.11

    Figure 14 12

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    49/53

    Silberschatz, Korth and Sudarshan14.49Database System Concepts - 6thEdition

    Figure 14.12

    Figure 14 13

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    50/53

    Silberschatz, Korth and Sudarshan14.50Database System Concepts - 6thEdition

    Figure 14.13

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    51/53

    Figure 14 15

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    52/53

    Silberschatz, Korth and Sudarshan14.52Database System Concepts - 6thEdition

    Figure 14.15

    Figure 14 16

  • 8/13/2019 [2].Transaction From Sixth Edition Korth

    53/53

    Figure 14.16