Ch 15 Transaction

download Ch 15 Transaction

of 26

Transcript of Ch 15 Transaction

  • 8/7/2019 Ch 15 Transaction

    1/26

    Chapter 15:

    TransactionsLoc Hoang

    CS 157B

  • 8/7/2019 Ch 15 Transaction

    2/26

    Definition Atransaction is a discrete unit of

    work that must be completely

    processed or not processed at all. Example: Transferring funds from

    a checking account to a savingaccount.

  • 8/7/2019 Ch 15 Transaction

    3/26

    Desirable Properties To ensure data data integrity. The

    database system must maintain

    the ACID properties.

    Atomicity

    Consistency

    Isolation

    Durability

  • 8/7/2019 Ch 15 Transaction

    4/26

    ACID continue Atomicity. Either all operations of

    the transaction are reflected

    properly in the database, or noneare.

    Consistency. Execution of a

    transaction in isolation (that is, withno other transaction executingconcurrently) preserves theconsistency of the database.

  • 8/7/2019 Ch 15 Transaction

    5/26

    ACID continue... Isolation. Even though multiple

    transactions may execute

    concurrently, the system guaranteesthat, for every pair of transactions Ti,and Tj, it appears to Ti, that either Tjfinished execution before Ti started,

    or that Tj started execution after Tifinished. Thus, each transaction isunaware of the transactionsexecuting concurrently in the

    system.

  • 8/7/2019 Ch 15 Transaction

    6/26

    ACID continue... Durability. After a transaction

    completes successfully, the

    changes it has made to thedatabase persist, even if there aresystem failures.

  • 8/7/2019 Ch 15 Transaction

    7/26

    Transaction States Because failures occurs, transaction are

    broken up into states to handle various

    situation.

  • 8/7/2019 Ch 15 Transaction

    8/26

    Transaction States Active, the initial state; the

    transaction stays in this state until

    while it is still executing.

    A transition is terminated only if ithas either been committed oraborted.

  • 8/7/2019 Ch 15 Transaction

    9/26

    Transaction States Partially committed, After the

    final statement has been executed

    At this point failure is still possiblesince changes may have been onlydone in main memory, a hardwarefailure could still occur.

  • 8/7/2019 Ch 15 Transaction

    10/26

    Transaction States The DBMS needs to write out

    enough information to disk so that,

    in case of a failure, the systemcould re-create the updatesperformed by the transaction oncethe system is brought back up.

    After it has written out all thenecessary information, it iscommitted.

  • 8/7/2019 Ch 15 Transaction

    11/26

    Transaction States Committed- after successful

    completion.

    Once committed, the transactioncan no longer be undone byaborting it.

    Its effect could be undone only bya compensating transaction.

  • 8/7/2019 Ch 15 Transaction

    12/26

    Transaction States Failed, after the discovery that

    normal execution can no longer

    proceed

    Once a transaction can not becompleted, any changes that itmade must be undone rolling itback.

  • 8/7/2019 Ch 15 Transaction

    13/26

    Transaction States Aborted, after the transaction has

    been rolled back the the database

    has been restored to its state prior tothe start of the transaction.

    The DBMS could eitherkill the

    transaction orrestart the transaction. A transaction may only be restarted as a

    result of some hardware or softwareerror, and a restarted transaction isconsidered a new transaction.

  • 8/7/2019 Ch 15 Transaction

    14/26

    Concurrent execution

    DBMS usually allow multipletransaction to run at once.

    The transaction could by runserially (one after another) or theycould be interleaved(switchingback and forth betweentransactions)

  • 8/7/2019 Ch 15 Transaction

    15/26

    Concurrent execution Pro:

    Improved throughput and resource

    utilizationCould take advantage of multi-processsing

    Reduce waiting time.Avoid short transaction waiting on long transaction

    Improve average response time.

  • 8/7/2019 Ch 15 Transaction

    16/26

    Concurrent execution

    Con:

    Creates many complications in

    data consistency, includingcascading roll-backs.

    Consistency could be compromiseeven if each individual transactionis correct.

  • 8/7/2019 Ch 15 Transaction

    17/26

    Schedules

    DBMS needs to make sure that alltransaction schedules preserves

    the consistency of the database.

    A schedule is the chronologicalorder in which instructions areexecuted in a system.

  • 8/7/2019 Ch 15 Transaction

    18/26

    Schedule

    A schedule for a set of transactionmust consist of all the instruction of

    those transaction and mustpreserve the order in which theinstructions appear in eachindividual transaction.

  • 8/7/2019 Ch 15 Transaction

    19/26

    Schedule exampleT1

    -----------------

    read(A)

    A:=A-50

    write(A)

    read(B)B:= B+ 50

    write(B)

    T2

    -----------------

    read(A)

    temp: A * 0.1

    A: A-temp

    write (A)

    read(B)

    B:=B +temp

    write(B)

    Schedule 1.

  • 8/7/2019 Ch 15 Transaction

    20/26

    Serial Schedule

    In schedule 1 the all the instructions ofT1 are grouped and run together. Thenall the instructions of T2 are groupedand run together. This type of schedulesare called serial.

    Concurrent transactions do not have to

    run serially as in the next examples.

  • 8/7/2019 Ch 15 Transaction

    21/26

    Interleaved ScheduleT1

    -----------------

    read(A)

    A:=A-50

    write(A)

    read(B)

    B:= B+ 50

    write(B)

    T2

    -----------------

    read(A)temp: A * 0.1

    A: A-temp

    write (A)

    read(B)

    B:=B +temp

    write(B)

    Schedule 2

  • 8/7/2019 Ch 15 Transaction

    22/26

    Conflict Equivalent

    Schedule 1 and 2 produce the sameresult even though they have differentsequence. It could be shown thatSchedule 2 could be transform intoSchedule 1 with a sequence of swaps,so Schedule 1 and 2 conflict equivalent.

    Not all schedules are conflict equivalent.Consider Schedule 3

  • 8/7/2019 Ch 15 Transaction

    23/26

    Inconsistent ScheduleT1

    -----------------

    read(A)

    A:=A-50

    write(A)

    read(B)

    B:= B+ 50

    write(B)

    T2

    -----------------

    read(A)

    temp: A * 0.1

    A: A-tempwrite (A)

    read(B)

    B:=B +temp

    write(B)

    Schedule 3.

  • 8/7/2019 Ch 15 Transaction

    24/26

    Serializability

    Suppose that A and B were bankaccounts with initial amounts of $1,000and $2,000 respectively. Then afterSchedule 1 and Schedule 2 are run. Theresult is $850 forA and $2,150 for B.And the sum A+B is still $3000.

    After Schedule 3 runs, account A is $950and B $2100. The sum A+B is now$3,050 which is incorrect, since $50 wasmagically created in the process.

  • 8/7/2019 Ch 15 Transaction

    25/26

    Conflict Serializability

    Schedule 2 is consistent becauseis it conflict equivalent to Schedule

    1. Since Schedule 1 is serial,Schedule two is said to be conflictserializable.

    Instructions within a schedulecould be swapped if they do notconflict. Instructions do not conflictif they access different data item orif the access the same data item

  • 8/7/2019 Ch 15 Transaction

    26/26

    Conflict Serializability

    Schedule 2 could be turn into Schedule 1with the by the following steps:

    Swap write(A) of T2 with read(B) of T1

    Swap read(B) of T1 with read(A) of T2

    Swap write(B) of T1 with write(A) of T2

    Swap write(B) of T1 with read(A) of T2