Java教程

数据库中数据存取、事务恢复处理、redo、undo和检查点

本文主要是介绍数据库中数据存取、事务恢复处理、redo、undo和检查点,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Data Access 数据存取

  • Physical blocks are those blocks residing on the disk
  • Buffer blocks are the blocks residing temporarily in main memory
  • Block movements between disk and main memory are initiated through the following two operations
  • input(A) transfers the physical block A to main memory
  • output(B) transfers the buffer block B to the disk

Transaction transfers data items between system buffer blocks and its private work-area using the following operations :

  • read(X) assigns the value of data item X to the local variable xi
  • write(X) assigns the value of local variable xi to data item X in the buffer block
  • both these commands may issue an input(BX) instruction before the assignment, if the block BX in which X resides is not in main memory

Log-Based Recovery

  • When transaction Ti starts, it registers itself by writing a log record
  • Before Ti executes write(X), a log record <Ti, X, V1, V2> is written, where V1 is the value of X before the write (the old value), and V2 is the value to be written to X (the new value)
  • When Ti finishes it last statement (partially committed state), the log record is written. Then the transaction enters the committed state

Concurrency Control and Recovery

Undo and Redo Operations

  • undo(Ti) – restores the value of all data items updated by Ti to their old values, going backwards from the last log record for Ti
    1. Each time a data item X is restored to its old value V a special log record <Ti , X, V> is written out
    2. When undo of a transaction is complete, a log record is written out
  • redo(Ti) – sets the value of all data items updated by Ti to the new values, going forward from the first log record for Ti
    1. No logging is done in this case

Recovering from Failure

When recovering after failure:

  • Transaction Ti needs to be undone if the log

    Contains the record ,
    But does not contain either the record or

  • Transaction Ti needs to be redone if the log

    Contains the records
    And contains the record or

Checkpoints

  • Streamline recovery procedure by periodically performing checkpointing

    1. Output all log records currently residing in main memory onto stable storage
    2. Output all modified buffer blocks to the disk
    3. Write a log record onto stable storage where L is a list of all transactions active at the time of checkpoint
    4. All updates are stopped while doing checkpointing
  • During recovery we need to consider only the most recent transaction Ti that started before the checkpoint, and transactions that started after Ti

    1. Scan backwards from end of log to find the most recent record
    2. Only transactions that are in L or started after the checkpoint need to be redone or undone
    3. Transactions that committed or aborted before the checkpoint already have all their updates output to stable storage

Redo phase:

  1. Find last record, and set undo-list to L
  2. Scan forward from above record
    1. Whenever a record <Ti, Xj, V1, V2> or <Ti, Xj, V2> is found, redo it by writing V2 to Xj
    2. Whenever a log record is found, add Ti to undo-list
    3. Whenever a log record or is found, remove Ti from undo-list

Undo phase:

  1. Scan log backwards from end
    1. Whenever a log record <Ti, Xj, V1, V2> is found where Ti is in undo-list perform same actions as for transaction rollback:
      • perform undo by writing V1 to Xj
      • write a log record <Ti , Xj, V1>
    2. Whenever a log record is found where Ti is in undo-list
      • Write a log record
      • Remove Ti from undo-list
    3. Stop when undo-list is empty, i.e., has been found for every transaction in undo-list
这篇关于数据库中数据存取、事务恢复处理、redo、undo和检查点的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!