The transaction ACID SQL

Overview:

A transaction is a logical unit of work consisting of a series of statements. Batch transactions and stored procedures have similarities to some extent,

Usually to the completion of a business logic and a plurality of statements or "package" together, so that they occur a logical boundary in between and the other statements and form separate unit of work.

When a transaction modifies a plurality of data tables, if there is some error in the course of processing, such as system crash or a sudden power failure, the result is returned all the data is not saved.

Because the outcome of the transaction are only two: one is in the course of the transaction, if some error occurs the entire transaction is rolled back, all changes to the data of all revoked, operation of the database transaction is a single-step execution , you can roll back any time when an error is encountered;

Another is that if any errors occurred at each step of execution are successful, the entire transaction is entirely committed. Thus be seen, it can be effectively used not only improve the security of the transaction data may also enhance the efficiency of data processing.

MySQL transaction is mainly used for large data manipulation, high complexity of the process. For example, in personnel management system, you delete a person, you only need to remove the basic information of personnel, and also to delete the personnel-related information, such as mail, articles, etc., so that the database operation statement constitutes a transaction !

  • Only in MySQL database engine used Innodb database or table only support transactions.
  • Transactions can be used to maintain the integrity of the database to ensure that the bulk of the SQL statements either all executed or not executed all.
  • Transaction management for insert, update, delete statement

Transaction includes four important attributes are collectively referred to as ACID (acronym refers to the four basic elements of the database transaction properly executed)

Comprising: Atomicity (Atomicity) , consistency (Consistency) , isolation (Isolation) , persistent (Durability Rev) .

Support a transaction (Transaction) database must have four characteristics.

Otherwise, the process in which the transaction (Transaction processing) can not guarantee the accuracy of the data, the transaction process is very likely not meet the requirements of the parties to the transaction, a transaction must pass the ACID.

 

1, Atomicity:

All the operation of the entire transaction, either completed or not completed all impossible stuck in the middle of a link.

Transaction error occurs during execution, it will be rolled back (Rollback) to the state before the start of the transaction, as the transaction never performed the same.

 

2. Consistency:

A transaction can be packaged state change (unless it is a read-only).

Transaction must always keep the system in a consistent state, regardless of concurrent at any given time the number of transactions.

In other words: If the transaction is complicated by multiple, different operating systems must be as serial affairs.

Its main feature is the protection and invariance (Preserving an Invariant), to transfer the case as an example, suppose you have five accounts, each account balance is $ 100, then $ 500 five total account

If multiple transfers between this account 5, regardless of the number of concurrent, such as between A and B account transfers $ 5, between C and D account transfer $ 10, transfers between B and E 15 yuan, five total account should still $ 500, which is to protect and invariance.

 

3, isolation

Isolation transaction execution, so that if they were the only operating system executing within a given time.

If there are two transactions running at the same time, perform the same function, isolation transaction will ensure that each transaction in the system that only the transaction using the system.

This property is sometimes referred to as serialization, in order to prevent confusion between the transaction operations, or be serialized serialized request, so that only one request at the same time for the same data.

 

4, endurance

After completion of the transaction commits, the transaction changes made to the database will be stored in the database lasting, permanent preserved and will not be rolled back.

 

5, description

Because of an action often contain many sub-operations, and these sub-operations may be problematic due to hardware damage or other factors, to correctly implement ACID is not easy.

ACID recommended that the database will need to update and modify all the data once the operation is completed, but in fact is not feasible.

There are two main ways to achieve ACID: The first one is Ahead logging the Write , that is, journaling way (modern databases are based on this way). The second is Paging Shadow .

With respect to the WAL (write ahead logging) technique, shadow paging technology is relatively simple, eliminating the overhead of writing log records the speed of recovery too fast (do not need to redo and undo).

Shadow paging drawback is that when a transaction is committed to output a plurality of blocks, which makes a big overhead submitted, and in block units, difficult to apply to allow concurrent execution of multiple transactions in the case - this is its fatal flaw.

WAL central idea is to modify the data file (which is a vector tables and indexes) must only occur after these log records have been modified, that is, after the log record flushed to permanent storage.

If we follow this process, then we do not need at the time of each transaction submitted to flush data pages to disk, because we know that in the event of the collapse of the case,

We can use the log to restore the database: not yet attached to any data page records will be redone (called roll-forward recovery, also known as REDO) and then start logging those uncommitted transaction changes will be made from the data delete page (called backward scrolling recovery - UNDO).

 

Guess you like

Origin www.cnblogs.com/Sungeek/p/12162908.html