Introduction
In the database system, the transaction is a discrete unit of work, it can modify a user's account balance may be a write operation inventory item. In single-user, single-database environment to perform relatively simple affairs, but in a distributed environment, maintain the integrity of multiple databases is more complicated. Most online transaction processing system is implemented on mainframe computers, this is due to the complexity of its operations, the need for fast input / output and improve management. If a transaction to be modified in a number of venues, it would need to rewrite the data management mechanism to prevent and provide synchronization. It also needs to have the ability to return a transaction failure to provide security and data recovery capabilities.
For example, we go to the bank transfer, the operation can be divided into the following two areas:
(1) to draw money from an account.
(2) the payment to the second account.
In this process, two areas are linked. The first account to draw funds must be guaranteed the right into the second account, if the second part is not completed, the entire process should be canceled, or else lose money problems will occur. The whole process can be seen as a thing, success is all the success, failure, all you need to undo, to avoid operation when the middle part of the problem, resulting in data inconsistencies.
database transaction is a logical division, sometimes not very clear, it may be a plurality of steps may be steps. We can understand database of things: a series of modifications made to the database, in the modification process, not yet written to the database, but cached in the user's own terminal can preview changes until all changes are complete, and checked to confirm correct, and submit a one-time written to the database, before submitting, if necessary, changes made can be canceled. After submission, it can not be revoked, after the success of other users to submit queries before the change can browse through the data.
properties
1, atomicity (Atomicity): all operations in a transaction is an integral database, either completed or all not be executed.
2, the consistency (Consistency): several transactions executed in parallel, the results of which must be consistent with certain order of serial execution.
3, isolation (Isolation): executing a transaction without interference from other transactions, the intermediate result of execution of the transaction to other transaction must be transparent.
4, persistent (Durability): For any committed transactions, the system must ensure that the changes to the database transaction is not lost, even if the database fails.
transaction ACID properties by the relational database system (DBMS) achieved, using the DBMS transaction log to ensure atomicity, consistency and durability. The update transaction log records made to the database, a transaction if an error occurs during execution, the transaction would have been done on the database updates can be revoked under the log, so that the database is rolled back to its original state before executing the transaction.
to transaction isolation, DBMS is achieved using the locking mechanism. When multiple transactions simultaneously update the same data in the database, only allowed to hold the lock transaction can update the data, other transaction must wait until the lock is released before a transaction, other transactions have a chance to update the data.
role
typically it contains a database transaction to read a sequence database / write operations. Its presence with two purposes:
-
provides a method to recover from a failure state to a normal sequence of operations for the database while the database is still provided even in an abnormal state method to maintain consistency.
-
when multiple concurrent applications access the database can provide a method of isolation among these applications, in order to prevent interference between the operation of each other.
When a transaction is submitted to the DBMS (database management system), the DBMS (database management system) need to ensure that all operations of the transaction are successfully completed and the results are permanently stored in the database, if the transaction does not complete successfully in any operation, all operations in the transaction needs to be rolled back, back to the state before the transaction execution; at the same time, the transaction had no effect on the database or perform other transactions, all the transaction seemed to run independent.
However, in reality, a high risk of failure. During the execution of a transaction in the database, it is possible to encounter a transaction fails, the database system / operating system failure, or even a case of storage media failure and so on. This requires a DBMS transaction fails to perform the restore operation, the database will restore its state to a consistent state (data consistency guaranteed state). In order to achieve the state to restore the database to a consistent state functions, DBMS usually required to maintain a transaction log data to a database to track all transactions affecting operations.
Database Transaction Models
explicit transaction
explicit transaction known as defined transaction, which is a defined start and end of the transaction with an explicit manner explicit transaction when using start transaction and commit statement said happened.
implicit transaction
implicit transaction means that every piece of data manipulation statements are automatically becomes a transaction, start a transaction is implicit, the transaction ends are clearly marked. That is when the user data, the system automatically opens a transaction, ending a transaction need to manually call commit or rollback statement to the end of the current transaction, automatically open a new transaction after the transaction is complete.
Automatic Transaction
refers to an automatic transaction transaction automatically turned on and automatically ends the transaction. During the transaction, if no exception occurs, the transaction is automatically filed; when execution error occurs, the transaction is automatically rolled back.
advantage
transactional way access to the database, has the following advantages:
1, the related logic operations into a group;
2, before a permanent change in the data, the preview data can change;
3, read to ensure data consistency.