Advanced Database three: Talking about database transaction (transaction)

Source: Internet
Author: User
definition of a transaction

A transaction (TXN) is a series of behaviors performed on a shared database to achieve higher levels of more complex logic. Transactions are the most basic units in the DBMS, and the transactions are inseparable. ACID

ACID, refers to the four characteristics that a transaction (transaction) should have in a reliable database management system (DBMS): atomicity (atomicity), consistency (consistency), isolation (isolation), Persistence (durability). Atomic Nature

Atomicity means that a transaction is a non-divisible unit of work, and the operations in the transaction either occur or do not occur. When an error occurs during execution, the transaction is rolled back (Rollback) to the state before the transaction begins, as if the transaction had never been executed.

Suppose a wants to transfer money to B, the deductions and the two statements in the transaction are either executed or not executed. Otherwise, if only the execution of the deduction statement, on the submission, at this time suddenly power outage, a account has been deducted, B account has not received a bonus, in life will cause disputes. Consistency

Consistency refers to a transaction that transitions a system from one consistent state to another consistent state. This means that database transactions do not break the integrity of relational data and the consistency of business logic.

For bank transfer transactions, whether the transaction succeeds or fails, you should ensure that the total deposit for A and B in the Account table at the end of the transaction is always $2000. If a person deducted 100 yuan, a person to 50 yuan, it destroyed the consistency.

The consistency of transactions determines the complexity of a system's design and implementation. Transactions can be of varying degrees of consistency: strong consistency: Read operations can read immediately to committed update operations. Weak consistency: The submitted update operation is not necessarily read immediately by the read operation, there is an inconsistency window, which refers to a time when the read operation can read to the latest value. Final consistency: A special case of weak consistency. The transaction updates a copy of the data, and eventually the consistency guarantees that the same value is updated in the absence of other transactions, and eventually all transactions are read to the latest value of the previous transaction update. If no error occurs, the size of the inconsistency window depends on: Communication delay, system load, and so on. Other consistency variants are: monotonic consistency: If a process has read a value, subsequent values will not be read earlier. Session consistency: During a session that guarantees client-server interaction, read operations can read the latest values after the update operation. Isolation of

When multiple transactions are concurrently accessed, the transactions are isolated, and one transaction should not affect the performance of other transactions.

This means that in a concurrent environment, each transaction has its own full data space when different transactions manipulate the same data at the same time. Modifications made by a concurrent transaction must be isolated from modifications made by any other concurrent transaction.

Problems that can occur when you are in parallel: Dirty reads: Transaction a modifies a data, but does not commit, transaction B reads the result of an uncommitted update to transaction A, and if transaction a commits fails, transaction B reads dirty data. Non-repeatable READ: In the same transaction, the results read to the same data are inconsistent. For example, the result that transaction B reads before transaction A is submitted may be different from the results read after the commit. Phantom read: In the same transaction, the same query returns multiple times with inconsistent results. Transaction A adds a new record, and transaction B performs a query operation before and after transaction a commits, discovering that one more record than the previous one.

Different isolation levels: READ UNCOMMITTED: The lowest isolation level, nothing needs to be done, and one transaction can read the uncommitted results of another transaction. All concurrent transaction problems can occur. Read Committed: Only after a transaction commits, its update results are seen by other transactions. Can solve the dirty read problem. Repeated read: In a transaction, the result of reading the same data is always the same, regardless of whether there are other transactions to manipulate the data, and whether the transaction is committed. Can resolve dirty read, non-repeatable read. Serialization: Transaction serialization execution, the highest isolation level, sacrificing the concurrency of the system. All problems with concurrent transactions can be resolved.

Isolation Level Dirty Read Missing Updates non-repeatable read Phantom Reading Concurrency Model Update Conflict Detection
UNCOMMITTED read: Read uncommited √\sqrt{} √\sqrt{} √\sqrt{} √\sqrt{} Pessimistic X\times
Submitted read: Read commited X\times √\sqrt{} √\sqrt{} √\sqrt{} Pessimistic X\times
REPEATABLE READ: Repeatable READ X\times X\times X\times √\sqrt{} Pessimistic X\times
Serializable read: Serializable X\times X\times X\times X\times Pessimistic X\times
Durability

Persistence means that after the transaction is completed, the changes made to the database by the firm are persisted in the database and are not rolled back.

Even if there is an accident such as a power outage, the transaction is persisted in the database once it is committed. Behavior Classification unprotected behavior (unprotected actions):
No need for undone/redone in addition to consistency, no other ACID properties, their role can not be relied upon for example, the operation of temporary files during a transaction protected behavior (Protected actions):
The Undone/redone can also have to be performed without showing results before the end, with full acid properties such as the real action of the database operation (real actions):
Cannot be manipulated by undone these operations act directly on the physical world, which is very difficult or almost impossible to revoke the ATM machine take money transaction Mode plane transaction (FLAT transactions)

The local variables are computed and are not seen by the DBMS, so they are not discussed much later.
Access to the DBMS using a call or statement-level interface typically begins with begin, followed by a series of operations, ending with commit or rollback.

features No internal structure can only be oriented to a single DBMS, only one transaction can be run at the same time only for simple applications

Once rolled back, all operations of the entire transaction are rolled back, and all operations before the commit are lost all improvements

Archive Point (SavePoint)

When a transaction detects that a database needs to roll back some of its previous operations, the transaction can either cancel its own operation directly or use the rollback mechanism to undo the operation.

When we release the Savepoint2, the operation changes are still there, but we are not able to return to that position through savepoint, and Savepoint3 relies on Savepoint2 to release the latter while releasing the former. So we can't go back to Savepoint3.

Nested transactions (NESTED transactions)

SavePoint the transaction into a series of behaviors that each can roll back independently.
A nested transaction is a set of hierarchical operations, and the result of a child transaction depends on the operation of its father's transaction.

Chained transactions (Chained txns)

Multiple transaction order execution, one transaction commits, another transaction immediately begin, and no transaction can modify the data between two transactions.

Unlike SavePoint, a commit allows a DBMS to release a child transaction after all locks cannot be rolled back to the previous child transaction, so the transaction does not have atomicity

Batch update issues

The bulk update (bulk update) itself is a natural transaction and is rolled back if it fails.

Although the chained transaction model solves this problem, it requires the application to solve the problems of maintaining the database state and resolving the failed operation. Because the DBMS has no way to roll back the entire transaction. Compensation transactions (compensating transactions)

A special type of transaction that removes the result of the operation of a previous transaction. This elimination can only be a logical, not physical, aspect. Sage Affairs

Consists of two chain-type transactions, each of which is

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.