Docsity

Transactions and Concurrency Control in Database Systems, Slides of Operating Systems

Partial preview of the text

Download Transactions and Concurrency Control in Database Systems and more Slides Operating Systems in PDF only on Docsity!

Synchronization

Transactions

Transactions

  • Example Transaction: Transfer amount X from A to B debit(Account A; Amount X): A = A – X; credit(Account B; Amount X): B = B + X;
  • Either do the whole thing or nothing

ACID Properties – ID

  • Isolation : T looks like it is the only program running
    • Notice the looks like!
  • Durability : T produces unforgettable by the system results
    • T’s results become part of reality

Some Transaction Primitives

Examples of primitives for transactions. Primitive Description BEGIN_TRANSACTION Make the start of a transaction END_TRANSACTION Terminate the transaction and try to commit ABORT_TRANSACTION Kill the transaction and restore the old values READ Read data from a file, a table, etc. WRITE Write data to a file, a table, etc.

Flat Transactions – All or Nothing

  • Simplest Type
  • Basic building block for organizing an application into atomic actions
  • Encloses a program with brackets:
    • BEGIN_TRANSACTION
    • END_TRANSACTION All that is between the brackets is performed or nothing at all
  • Strictly satisfies ACID

Flat Transactions – Limitations

  • All-or-nothing is not always appropriate
  • It is beneficial to commit partial results (violate atomicity)
  • Trip Planning: commit part of a transaction
  • Bulk Updates: can be expensive to undo all updates

Flat Transactions with SavePoints

T: BEGIN_TRANSACTION S 1 … S m SAVEPOINT S m+ … … END_TRANSACTION

Failure here

ROLLBACK

Flat T

w/o

SavePoint

W SavePoint

  • Nested TransactionsThey organize T actions into a hierarchy^ –^ No D
  1. A nested T is a tree of T’s; sub-trees are flat or nested
  2. Leaf T’s are flat
  3. Root T = top-level T; all other are subTs
  4. SubT can commit or roll back. Its commit will not take effect unless the root commits
  5. When a subT rolls back, all its children are rolled back
  • Satisfy ACI, not the D (except for top level)

Distributed Transactions

  • A flat transaction that runs in a distributed environment:
    • Visit several nodes in the system
  • Distributed T: structure is determined by the distribution of data in the DS
  • Nested T: structure is determined by the functional decomposition of the application

Nested versus Distributed Ts

a) A nested transaction

b) A distributed transaction

Achieving Atomicity (I)

  • Private Workspace :
    • Change a copy of data, keeping original intact
    • COMMIT: copy changed data to original
    • ROLLBACK: discard copy

Private Workspace

a) The file index and disk blocks for a three-block file b) The situation after a transaction has modified block 0 and appended block 3 c) After committing

Writeahead Log

a) A transaction

b) – d) The log before each statement is executed

x = 0; y = 0; BEGIN_TRANSACTION; x = x + 1; y = y + 2 x = y * y; END_TRANSACTION; (a) Log [x = 0 / 1] (b) Log [x = 0 / 1] [y = 0 / 2] (c) Log [x = 0 / 1] [y = 0 / 2] [x = 1 / 4] (d)

Achieving Consistency & Isolation

  • Concurrency Control : controlling the execution of concurrent Ts operating on shared data
  • Consistency & Isolation : All Ts must appear as if they executed in some sequential order, one after another ( Serializability )