Transaction Management

Database Transaction

Transaction Properties

  1. Atomicity -- all parts of a transaction must be completed or not at all
  2. Durability -- the consistent state of the database when a transaction is completed must not be lost even in the presence of system failure
  3. Serializability -- the result of the concurrent execution of several transactions is treated as if they were executed in serial order in a multi-processing environment
  4. Isolation -- data used during the execution of a transaction cannot be used by a second transaction until the first one is completed

Transaction Management

What has to be done to ensure correct transaction

Single-user databases

Multi-user databases


Transaction Commitment

  • To ensure atomicity of transaction
  • Commitment is the act of making the effect of the transaction permanent, prior to which may be work applied to temporary database only
  • Rollback reverts the database state to the a previous state, the point before the first SQL statement or the explicit marker of BEGIN TRAN
  • However, it cannot cancel a transaction once it has been committed
  • When RDBMS encounters these SQL statements: COMMIT and ROLLBACK, it takes the appropriate actions
  • If the end of a program is successfully reach, then all changes are permanently records (commit)
  • If the program is abnormally terminated, the database is rolled back to its previous state and all changes are cancelled
  • SQL Server has savepoints which are markers in the midst of transactions so that the database can be rolled back to those points instead of the beginning of the transaction: e.g.

    Transaction Log

  • It is the mechanism to keep track of all transactions that update the database and therefore support recovery
  • When the DBMS executes transactions that modify the database, it updates the log automatically
  • The before and after data about the database and any of the tables, rows and attribute values, the beginning and ending of the transaction are recorded
  • The log can be used to roll-back as well as roll-forward any committed transactions that are not written to the physical database
  • Log writing increases the transaction overhead but worthwhile as database can be recovered

    CONCURRENCY CONTROL

    Coordination of simultaneous execution of multiple transactions


    Concurrency Control Based on Locking

    Lock Granularity

    In SQL-SERVER, locks are automatically applied, you can specify under (optimizer_hints):


    Two-Phase Locking to Ensure Serializability

    Deadlock Problem

    3 Techniques to Control Deadlocks

    1. Deadlock Prevention -- A transaction requesting a new lock is aborted if there is the possibility that a deadlock can occur (more rollbacks and redoes)
    2. Deadlock Detection -- DBMS periodically checks for deadlock, if found, aborts one the transactions (the victim)
    3. Deadlock Avoidance -- Transaction must obtain all the locks it needs before it can be executed (Increases response time)

    Concurrency Control with Time Stamping


    Concurrency Control with Optimistic Methods