transactions signed

Upload: kyalo-martin

Post on 04-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Transactions Signed

    1/53

    Database System Concepts, 6

    th

    Ed.Silberschatz, Korth and Sudarshan

    See www.db-book.com for conditions on re-use

    Chapter 14: TransactionsChapter 14: Transactions

    kinuthia

  • 7/30/2019 Transactions Signed

    2/53

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

    Chapter 14: TransactionsChapter 14: Transactions

    Transaction Concept

    Transaction State

    Concurrent Executions

    Serializability Recoverability

    Implementation of Isolation

    Transaction Definition in SQL Testing for Serializability.

  • 7/30/2019 Transactions Signed

    3/53

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

    Transaction ConceptTransaction Concept

    A transaction is a unitof program execution that accesses andpossibly 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 +506. write(B)

    Two main issues to deal with:

    z Failures of various kinds, such as hardware failures and systemcrashes

    z Concurrent execution of multiple transactions

  • 7/30/2019 Transactions Signed

    4/53Silberschatz, Korth and Sudarshan14.4Database System Concepts - 6th Edition

    Example of Fund TransferExample 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

    z 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

    z the system should ensure that updates of a partially executed transaction

    are not reflected in the database Durability 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.

  • 7/30/2019 Transactions Signed

    5/53Silberschatz, Korth and Sudarshan14.5Database System Concepts - 6th Edition

    Example of Fund Transfer (Cont.)Example of Fund Transfer (Cont.)

    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 +506. write(B)

    Consistency requirement in above example:

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

    In general, consistency requirements include Explicitly specified integrity constraints such as primary keys and foreign

    keys

    Implicit integrity constraints

    e.g. sum of balances of all accounts, minus sum of loan amounts

    must equal value of cash-in-handz A transaction must see a consistent database.

    z During transaction execution the database may be temporarily inconsistent.

    z When the transaction completes successfully the database must beconsistent

    Erroneous transaction logic can lead to inconsistency

  • 7/30/2019 Transactions Signed

    6/53Silberschatz, Korth and Sudarshan14.6Database System Concepts - 6th Edition

    Example of Fund Transfer (Cont.)Example of Fund Transfer (Cont.)

    Isolation requirement if between steps 3 and 6, anothertransaction 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

    z that is, one after the other.

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

  • 7/30/2019 Transactions Signed

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

    ACID PropertiesACID Properties

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

    Consistency. Execution of a transaction in isolation preserves theconsistency 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.

    z

    That is, for every pair of transactions Tiand Tj, it appears to Ti thateitherTj, finished execution before Tistarted, orTjstarted executionafterTi finished.

    Durability. After a transaction completes successfully, the changes it

    has made to the database persist, even if there are system failures.

    A transaction is a unit of program execution that accesses and possiblyupdates various data items.To preserve the integrity of data the databasesystem must ensure:

  • 7/30/2019 Transactions Signed

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

    Transaction StateTransaction State

    Active the initial state; the transaction stays in this state while it isexecuting

    Partially committed after 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 thedatabase restored to its state prior to the start of the transaction.

    Two options after it has been aborted:z restart the transaction

    can be done only if no internal logical error

    z kill the transaction

    Committed after successful completion.

  • 7/30/2019 Transactions Signed

    9/53Silberschatz, Korth and Sudarshan14.9Database System Concepts - 6th Edition

    Transaction State (Cont.)Transaction State (Cont.)

  • 7/30/2019 Transactions Signed

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

    Concurrent ExecutionsConcurrent Executions

    Multiple transactions are allowed to run concurrently in the system.Advantages are:

    z increased processor and disk utilization, leading to bettertransaction throughput

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

    z reduced average response time for transactions: shorttransactions need not wait behind long ones.

    Concurrency control schemes mechanisms to achieve isolation

    z 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.

  • 7/30/2019 Transactions Signed

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

    SchedulesSchedules

    Schedule a sequences of instructions that specify the chronologicalorder in which instructions of concurrent transactions are executed

    z a schedule for a set of transactions must consist of all instructionsof those transactions

    z 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

    z 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

  • 7/30/2019 Transactions Signed

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

    Schedule 1Schedule 1

    Let T1 transfer $50 from A to B, and T2 transfer 10% of thebalance from A to B.

    A serial schedule in which T1 is followed by T2 :

  • 7/30/2019 Transactions Signed

    13/53

    Silberschatz, Korth and Sudarshan14.13Database System Concepts - 6th Edition

    Schedule 2Schedule 2

    A serial schedule where T2 is followed by T1

  • 7/30/2019 Transactions Signed

    14/53

  • 7/30/2019 Transactions Signed

    15/53

    Silberschatz, Korth and Sudarshan14.15Database System Concepts - 6th Edition

    Schedule 4Schedule 4

    The following concurrent schedule does not preserve thevalue of (A + B ).

  • 7/30/2019 Transactions Signed

    16/53

    Silberschatz, Korth and Sudarshan14.16Database System Concepts - 6th Edition

    SerializabilitySerializability

    Basic Assumption Each transaction preserves databaseconsistency.

    Thus serial execution of a set of transactions preservesdatabase consistency.

    A (possibly concurrent) schedule is serializable if it isequivalent to a serial schedule. Different forms of scheduleequivalence give rise to the notions of:

    1. conflict serializability

    2. view serializability

  • 7/30/2019 Transactions Signed

    17/53

    Silberschatz, Korth and Sudarshan14.17Database System Concepts - 6th Edition

    Simplified view of transactionsSimplified view of transactions

    z We ignore operations other than read and writeinstructions

    z

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

    z Our simplified schedules consist of only read and write

    instructions.

    C

  • 7/30/2019 Transactions Signed

    18/53

    Silberschatz, Korth and Sudarshan14.18Database System Concepts - 6th Edition

    Conflicting InstructionsConflicting Instructions

    Instructions liand ljof transactions Tiand Tjrespectively, conflictif 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 lj forces a (logical) temporal

    order between them.

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

    C SC fli S i li bili

  • 7/30/2019 Transactions Signed

    19/53

    Silberschatz, Korth and Sudarshan14.19Database System Concepts - 6th Edition

    Conflict SerializabilityConflict Serializability

    If a schedule Scan be transformed into a schedule S by a series ofswaps of non-conflicting instructions, we say that Sand S areconflict equivalent.

    We say that a schedule Sis conflict serializable if it is conflict

    equivalent to a serial schedule

    C fli S i li bili (C )C fli t S i li bilit (C t )

  • 7/30/2019 Transactions Signed

    20/53

    Silberschatz, Korth and Sudarshan14.20Database System Concepts - 6th Edition

    Conflict Serializability (Cont.)Conflict Serializability (Cont.)

    Schedule 3 can be transformed into Schedule 6, a serialschedule where T2 follows T1, by series of swaps of non-conflicting instructions. Therefore Schedule 3 is conflictserializable.

    Schedule 3 Schedule 6

    C fli t S i li bilit (C t )C fli t S i li bilit (C t )

  • 7/30/2019 Transactions Signed

    21/53

    Silberschatz, Korth and Sudarshan14.21Database System Concepts - 6th Edition

    Conflict Serializability (Cont.)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 >.

    Vi S i li bilitVi S i li bilit

  • 7/30/2019 Transactions Signed

    22/53

    Silberschatz, Korth and Sudarshan14.22Database System Concepts - 6th Edition

    View SerializabilityView Serializability

    Let Sand S be two schedules with the same set of transactions. Sand S are view equivalent if the following three conditions are met,for each data item Q,

    1. If in schedule S, transaction Tireads the initial value ofQ, then in

    schedule Salso transaction Ti must read the initial value ofQ.

    2. If in schedule S transaction Tiexecutes read(Q), and that valuewas produced by transaction Tj (if any), then in schedule Salsotransaction Timust read the value ofQthat was produced by the

    same write(Q) operation of transaction Tj .

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

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

    Vi S i li bilit (C t )View Serializability (Cont )

  • 7/30/2019 Transactions Signed

    23/53

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

    View Serializability (Cont.)View Serializability (Cont.)

    A schedule Sis view serializable if it is view equivalent to a serialschedule.

    Every conflict serializable schedule is also view serializable.

    Below is a schedule which is view-serializable but notconflictserializable.

    What serial schedule is above equivalent to?

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

    Other Notions of SerializabilityOther Notions of Serializability

  • 7/30/2019 Transactions Signed

    24/53

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

    Other Notions of SerializabilityOther Notions of Serializability

    The schedule below produces same outcome as the serialschedule < T1, T5 >, yet is not conflict equivalent or viewequivalent to it.

    Determining such equivalence requires analysis of operations

    other than read and write.

    Testing for SerializabilityTesting for Serializability

  • 7/30/2019 Transactions Signed

    25/53

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

    Testing for SerializabilityTesting for Serializability

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

    Precedence graph a direct graph where the vertices arethe transactions (names).

    We draw an arc from Ti to Tj if 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

    Test for Conflict SerializabilityTest for Conflict Serializability

  • 7/30/2019 Transactions Signed

    26/53

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

    Test for Conflict SerializabilityTest for Conflict Serializability

    A schedule is conflict serializable if and onlyif its precedence graph is acyclic.

    Cycle-detection algorithms exist which takeordern2 time, where nis the number of

    vertices in the graph.z (Better algorithms take ordern+ e

    where eis the number of edges.)

    If precedence graph is acyclic, theserializability order can be obtained by atopological sortingof the graph.

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

    z For example, a serializability order forSchedule A would beT5 T1 T3 T2 T4

    Are there others?

    Test for View SerializabilityTest for View Serializability

  • 7/30/2019 Transactions Signed

    27/53

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

    Test for View SerializabilityTest for View Serializability

    The precedence graph test for conflict serializability cannot be useddirectly to test for view serializability.

    z Extension to test for view serializability has cost exponential in thesize of the precedence graph.

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

    z Thus existence of an efficient algorithm is extremelyunlikely.

    However practical algorithms that just check some sufficientconditions for view serializability can still be used.

    Recoverable SchedulesRecoverable Schedules

  • 7/30/2019 Transactions Signed

    28/53

    Silberschatz, Korth and Sudarshan14.28Database System Concepts - 6th Edition

    Recoverable SchedulesRecoverable Schedules

    Recoverable schedule if a transaction Tjreads a data itempreviously written by a transaction Ti, then the commit operation ofTi

    appears before the commit operation ofTj. The following schedule (Schedule 11) is not recoverable ifT9commits

    immediately after the read

    IfT8 should abort, T9 would have read (and possibly shown to the user)an inconsistent database state. Hence, database must ensure thatschedules are recoverable.

    Need to address the effect of transaction failures on concurrentlyrunning transactions.

    Cascading RollbacksCascading Rollbacks

  • 7/30/2019 Transactions Signed

    29/53

    Silberschatz, Korth and Sudarshan14.29Database System Concepts - 6th Edition

    Cascading RollbacksCascading Rollbacks

    Cascading rollback a single transaction failure leads to aseries of transaction rollbacks. Consider the following schedulewhere none of the transactions has yet committed (so theschedule is recoverable)

    IfT10 fails, T11 and T12 must also be rolled back. Can lead to the undoing of a significant amount of work

    CascadelessCascadeless SchedulesSchedules

  • 7/30/2019 Transactions Signed

    30/53

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

    CascadelessCascadeless SchedulesSchedules

    Cascadeless schedules cascading rollbacks cannot occur; foreach pair of transactions Tiand Tjsuch that Tj reads a data itempreviously written by Ti, the commit operation ofTi appears before theread operation ofTj.

    Every cascadeless schedule is also recoverable It is desirable to restrict the schedules to those that are cascadeless

    Concurrency ControlConcurrency Control

  • 7/30/2019 Transactions Signed

    31/53

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

    Concurrency ControlConcurrency Control

    A database must provide a mechanism that will ensure that all possibleschedules are

    z either conflict or view serializable, and

    z 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

    z 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.

    Concurrency Control (Cont )Concurrency Control (Cont.)

  • 7/30/2019 Transactions Signed

    32/53

    Silberschatz, Korth and Sudarshan14.32Database System Concepts - 6th Edition

    Concurrency Control (Cont.)Concurrency Control (Cont.)

    Schedules must be conflict or view serializable, and recoverable,for the sake of database consistency, and preferably cascadeless.

    A policy in which only one transaction can execute at a timegenerates serial schedules, but provides a poor degree of

    concurrency. Concurrency-control schemes tradeoff between the amount of

    concurrency they allow and the amount of overhead that theyincur.

    Some schemes allow only conflict-serializable schedules to begenerated, while others allow view-serializable schedules that arenot conflict-serializable.

    Concurrency Control vs. Serializability TestsConcurrency Control vs. Serializability Tests

  • 7/30/2019 Transactions Signed

    33/53

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

    Co cu e cy Co t o s Se a ab ty estsy y

    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

    z Instead a protocol imposes a discipline that avoids nonseralizableschedules.

    z 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.

    Weak Levels of ConsistencyWeak Levels of Consistency

  • 7/30/2019 Transactions Signed

    34/53

    Silberschatz, Korth and Sudarshan14.34Database System Concepts - 6th Edition

    Weak Levels of ConsistencyWeak Levels of Consistency

    Some applications are willing to live with weak levels of consistency,allowing schedules that are not serializable

    z E.g. a read-only transaction that wants to get an approximate totalbalance of all accounts

    z E.g. database statistics computed for query optimization can beapproximate (why?)

    z Such transactions need not be serializable with respect to othertransactions

    Tradeoff accuracy for performance

    Levels of Consistency in SQLLevels of Consistency in SQL--9292

  • 7/30/2019 Transactions Signed

    35/53

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

    Levels of Consistency in SQLLevels of Consistency in SQL 9292

    Serializable default Repeatable read only committed records to be read, repeated

    reads of same record must return same value. However, atransaction may not be serializable it may find some records

    inserted by a transaction but not find others. Read committed only committed records can be read, but

    successive reads of record may return different (but committed)values.

    Read uncommitted even 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

    z E.g. Oracle and PostgreSQL by default support a level of

    consistency called snapshot isolation (not part of the SQLstandard)

    Transaction Definition in SQLTransaction Definition in SQL

  • 7/30/2019 Transactions Signed

    36/53

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

    Transaction Definition in SQLQ

    Data manipulation language must include a construct forspecifying the set of actions that comprise a transaction.

    In SQL, a transaction begins implicitly.

    A transaction in SQL ends by:

    z Commit work commits current transaction and begins a newone.

    z Rollback work causes current transaction to abort.

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

    z Implicit commit can be turned off by a database directive

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

  • 7/30/2019 Transactions Signed

    37/53

    Database System Concepts, 6th Ed.

    Silberschatz, Korth and SudarshanSee www.db-book.com for conditions on re-use

    End of Chapter 14End of Chapter 14

    Figure 14.01Figure 14.01

  • 7/30/2019 Transactions Signed

    38/53

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

    gg

    Figure 14.02Figure 14.02

  • 7/30/2019 Transactions Signed

    39/53

    Silberschatz, Korth and Sudarshan14.39Database System Concepts - 6th Edition

    gg

    Figure 14.03Figure 14.03

  • 7/30/2019 Transactions Signed

    40/53

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

    gg

    Figure 14.04Figure 14.04

  • 7/30/2019 Transactions Signed

    41/53

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

    g

    Figure 14.05Figure 14.05

  • 7/30/2019 Transactions Signed

    42/53

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

    Figure 14.06Figure 14.06

  • 7/30/2019 Transactions Signed

    43/53

    Silberschatz, Korth and Sudarshan14.43Database System Concepts - 6th Edition

    Figure 14.07Figure 14.07

  • 7/30/2019 Transactions Signed

    44/53

    Silberschatz, Korth and Sudarshan14.44Database System Concepts - 6th Edition

    Figure 14.08Figure 14.08

  • 7/30/2019 Transactions Signed

    45/53

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

    Figure 14.09Figure 14.09

  • 7/30/2019 Transactions Signed

    46/53

    Silberschatz, Korth and Sudarshan14.46Database System Concepts - 6th Edition

    Figure 14.10Figure 14.10

  • 7/30/2019 Transactions Signed

    47/53

    Silberschatz, Korth and Sudarshan14.47Database System Concepts - 6th Edition

    Figure 14.11Figure 14.11

  • 7/30/2019 Transactions Signed

    48/53

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

    Figure 14.12Figure 14.12

  • 7/30/2019 Transactions Signed

    49/53

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

    Figure 14.13Figure 14.13

  • 7/30/2019 Transactions Signed

    50/53

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

    Figure 14.14Figure 14.14

  • 7/30/2019 Transactions Signed

    51/53

    Silberschatz, Korth and Sudarshan14.51Database System Concepts - 6th Edition

    Figure 14.15Figure 14.15

  • 7/30/2019 Transactions Signed

    52/53

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

    Figure 14.16Figure 14.16

  • 7/30/2019 Transactions Signed

    53/53

    Silberschatz, Korth and Sudarshan14.53Database System Concepts - 6th Edition