Transaction
Transaction
Transaction
Often, a collection of several operations on the database appears to be a single unit from the point of view of the database user. For example, a transfer of funds from a checking account to a savings account is a single operation from the customers standpoint; within the database system, however, it consists of several operations. Clearly, it is essential that all these operations occur, or that, in case of a failure, none occur. It would be unacceptable if the checking accounts were debited, but the savings accounts were not credited. Collections of operations that form a single logical unit of work are called transactions A database system must ensure proper execution of transactions despite failureseither the entire transaction executes, or none of it does. Furthermore, it must manage concurrent execution of transactions in a way that avoids the introduction of inconsistency. Transaction Concept A transaction is a unit of program execution that accesses and possibly updates various data items. Usually, a transaction is initiated by a user program written in a high-level data-manipulation language or programming language (for example, SQL, COBOL, C, C++, or Java), where it is delimited by statements (or function calls) of the form begin transaction and end transaction. The transaction consists of all operations executed between the begin transaction and end transaction. A transaction is a sequence of one or more SQL statements that together form a logical unit of work. The SQL statements that form the transaction are typically closely related and perform interdependent actions. Each statement in the transaction performs some part of a task, but all of them are required to complete the task. Grouping the statements as a single transaction tells the DBMS that the entire statement
1 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU
Transaction Processing
sequence should be executed in a manner that passes what is known as the ACID test. ACID is an acronym commonly used when referring to four characteristics of a transaction:
Atomic A transaction has an all-or-nothing nature. Either all operations in a transaction are performed or none of them are performed. If some statements are executed and the transaction fails, the results of the statements that executed must be rolled back. Only when all statements are executed properly can a transaction be considered complete and the results of the transaction applied to the database. Consistent A transaction must transform the database from one consistent state to another. The database must be consistent at the end of each transaction, meaning that all rules that define and constrain the data must be adhered to before the transaction can end. Also, no user should see inconsistent data because of changes made by transactions that have not yet completed. Isolated Each transaction without interference from isolated, no transaction can other transactions until those must execute on its own other transactions. To be act upon changes made by transactions are complete.
Durable Once a transaction is complete, all changes made by it should be preserved. The data should be in a consistent state, even if a hardware or application error occurs after completion of the transaction. In objectoriented programming, the term persistence is used for this property.
These properties are often called the ACID properties; the acronym is derived from the first letter of each of the four properties.
Transaction Processing
To gain a better understanding of ACID properties and the need for them, consider a simplified banking system consisting of several accounts and a set of transactions that access and update those accounts. For the time being, we assume that the database permanently resides on disk, but that some portion of it is temporarily residing in main memory. Transactions access data using two operations: read(X), which transfers the data item X from the database to a local buffer belonging to the transaction that executed the read operation. write(X), which transfers the data item X from the local buffer of the transaction that executed the write back to the database. In a real database system, the write operation does not necessarily result in the immediate update of the data on the disk; the write operation may be temporarily stored in memory and executed on the disk later. For now, however, we shall assume that the write operation updates the database immediately. Let Ti be a transaction that transfers $50 from account A to account B. This transaction can be defined as
Consistency: The consistency requirement here is that the sum of A and B be unchanged by the execution of the transaction. Without the consistency requirement, money could be created or destroyed by the transaction! It can be verified easily that, if the database is consistent before an execution of the transaction, the database remains consistent after the execution of the transaction.
3 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU
Transaction Processing
Atomicity: Suppose that, just before the execution of transaction Ti the values of accounts A and B are $1000 and $2000, respectively. Now suppose that, during the execution of transaction Ti, a failure occurs that prevents Ti from completing its execution successfully. Examples of such failures include power failures, hardware failures, and software errors. Further, suppose that the failure happened after the write(A) operation but before the write(B) operation. In this case, the values of accounts A and B reflected in the database are $950 and $2000. The system destroyed $50 as a result of this failure. In particular, we note that the sum A + B is no longer preserved. Thus, because of the failure, the state of the system no longer reflects a real state of the world that the database is supposed to capture. We term such a state an inconsistent state. We must ensure that such inconsistencies are not visible in a database system. Note, however, that the system must at some point be in an inconsistent state. Even if transaction Ti is executed to completion, there exists a point at which the value of account A is $950 and the value of account B is $2000, which is clearly an inconsistent state. This state, however, is eventually replaced by the consistent state where the value of account A is $950, and the value of account B is $2050. Thus, if the transaction never started or was guaranteed to complete, such an inconsistent state would not be visible except during the execution of the transaction. That is the reason for the atomicity requirement: If the atomicity property is present, all actions of the transaction are reflected in the database, or none are. The basic idea behind ensuring atomicity is this: The database system keeps track (on disk) of the old values of any data on which a transaction performs a write, and, if the transaction does not complete its execution, the database system restores the old values to make it
4 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU
Transaction Processing
appear as though the transaction never executed. Ensuring atomicity is the responsibility of the database system itself; specifically, it is handled by a component called the transaction-management component.
Durability: Once the execution of the transaction completes successfully, and the user who initiated the transaction has been notified that the transfer of funds has taken place, it must be the case that no system failure will result in a loss of data corresponding to this transfer of funds. The durability property guarantees that, once a transaction completes successfully, all the updates that it carried out on the database persist, even if there is a system failure after the transaction completes execution. We assume for now that a failure of the computer system may result in loss of data in main memory, but data written to disk are never lost. We can guarantee durability by ensuring that either The updates carried out by the transaction have been written to disk before the transaction completes. Information about the updates carried out by the transaction and written to disk is sufficient to enable the database to reconstruct the updates when the database system is restarted after the failure. Ensuring durability is the responsibility of a component of the database system called the recovery-management component.
Isolation: Even if the consistency and atomicity properties are ensured for each transaction, if several transactions are executed concurrently, their operations may interleave in some undesirable way, resulting in an inconsistent state. For example, as we saw earlier, the database is temporarily inconsistent while the transaction to transfer
5 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU
Transaction Processing
funds from A to B is executing, with the deducted total written to A and the increased total yet to be written to B. If a second concurrently running transaction reads A and B at this intermediate point and computes A+B, it will observe an inconsistent value. Furthermore, if this second transaction then performs updates on A and B based on the inconsistent values that it read, the database may be left in an inconsistent state even after both transactions have completed. The transaction concept is critical for programs that update a database, because it ensures the integrity of the database. A SQL-based DBMS makes this commitment about the statements in a transaction: The statements in a transaction will be executed as an atomic unit of work in the database. Either the results of all of the statements will be applied to the database, or none of the statements will have results posted to the database. The DBMS is responsible for keeping this commitment even if the application program aborts or a hardware failure occurs in the middle of the transaction, as shown in Figure A. In each case, the DBMS must make sure that when failure recovery is complete, the database never reflects a partial transaction.
Transaction Processing
Figure AThe SQL Transaction Concept Transaction State In the absence of failures, all transactions complete successfully. However, as we noted earlier, a transaction may not always complete its execution successfully. Such a transaction is termed aborted. If we are to ensure the atomicity property, an aborted transaction must have no effect on the state of the database. Thus, any changes that the aborted transaction made to the database must be undone.
7 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU
Transaction Processing
Once the changes caused by an aborted transaction have been undone, we say that the transaction has been rolled back. It is part of the responsibility of the recovery scheme to manage transaction aborts. A transaction that completes its execution successfully is said to be committed. A committed transaction that has performed updates transforms the database into a new consistent state, which must persist even if there is a system failure. Once a transaction has committed, we cannot undo its effects by aborting it. The only way to undo the effects of a committed transaction is to execute a compensating transaction. For instance, if a transaction added $20 to an account, the compensating transaction would subtract $20 from the account. However, it is not always possible to create such a compensating transaction. Therefore, the responsibility of writing and executing a compensating transaction is left to the user, and is not handled by the database system. We need to be more precise about what we mean by successful completion of a transaction. We therefore establish a simple abstract transaction model. A transaction must be in one of the following states: Active, the initial state; the transaction stays in this state while it is executing Partially committed, after the final statement has been executed Failed, after the discovery that normal execution can no longer proceed Aborted, after the transaction has been rolled back and the database has been restored to its state prior to the start of the transaction Committed, after successful completion, The state diagram corresponding to a transaction The state diagram corresponding to a transaction appears in Figure B. We say that a transaction has committed only if it has entered the committed state. Similarly, we say that a
8 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU
Transaction Processing
transaction has aborted only if it has entered the aborted state. A transaction is said to have terminated if has either committed or aborted. A transaction starts in the active state. When it finishes its final statement, it enters the partially committed state. At this point, the transaction has completed its execution, but it is still possible that it may have to be aborted, since the actual output may still be temporarily residing in main memory, and thus a hardware failure may preclude its successful completion.
Read Write Operation
End Transaction
Begin Transaction Active
Partially Committ ed
Commit Transaction
Committ ed
Abort
Failed
Terminat ed
Figure BThe Transaction State The database system then writes out enough information to disk that, even in the event of a failure, the updates performed by the transaction can be re-created when the system restarts after the failure. When the last of this information is written out, the transaction enters the committed state.
9 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU
Transaction Processing
A transaction enters the failed state after the system determines that the transaction can no longer proceed with its normal execution (for example, because of hardware or logical errors). Such a transaction must be rolled back. Then, it enters the aborted state. At this point, the system has two options: It can restart the transaction, but only if the transaction was aborted as a result of some hardware or software error that was not created through the internal logic of the transaction. A restarted transaction is considered to be a new transaction. It can kill the transaction. It usually does so because of some internal logical error that can be corrected only by rewriting the application program, or because the input was bad, or because the desired data were not found in the database. We must be cautious when dealing with observable external writes, such as writes to a terminal or printer. Once such a write has occurred, it cannot be erased, since it may have been seen external to the database system. Most systems allow such writes to take place only after the transaction has entered the committed state. One way to implement such a scheme is for the database system to store any value associated with such external writes temporarily in nonvolatile storage, and to perform the actual writes only after the transaction enters the committed state. If the system should fail after the transaction has entered the committed state, but before it could complete the external writes, the database system will carry out the external writes (using the data in nonvolatile storage) when the system is restarted. Handling external writes can be more complicated in some situations. For example suppose the external action is that of dispensing cash at an automated teller machine, and the system fails just before the cash is actually dispensed (we assume that cash can be dispensed atomically). It makes no sense to dispense cash when the system is restarted, since the user may have left the machine. In such a case a compensating transaction, such as depositing the cash back in the users account, needs to be executed when the system is restarted.
10 Created By Mr. Deependra Rastogi, Lecturer Department of Computer Science,TMU