ACID properties of the transaction isolation level

  ACID properties of transactions is an essential element to ensure the proper execution of the transaction, these four properties is not only database security mechanism for security affairs, expanding open, but also for us in a multithreaded programming environment provides a reference to ensure the proper execution of the task. Database transaction is a special kind of multi-channel programming tasks, is special because these tasks on the surrounding environment is a function of the presence of side effects, because they changed the status of the shared resource defined outside the task (data stored in the database).

  Atomicity (Atomicity)
  Atomicity means that a transaction is an indivisible unit of work operations in a transaction either occur or do not occur.

  In the multi-channel programming, we are talking about atomic mostly stressed indivisible whole operation, the thread is exclusive. But the atomicity of transactions is the emphasis on the side effects of sharing data, either all of these side effects occur or not occur. Define a transaction is the definition of an atomic operation, if the atomic operation is not completed, the task should not produce side effects on the surrounding environment, as these side effects are wrong. Ensure the integrity of complex logic operations, and to avoid the side effects of external error task, the database provides a commit / rollback mechanism, which provides a reference for us to deal with similar scenes.

  Consistency (Consistency)
  the integrity of the data before and after the transaction must be consistent. Here does not refer to data consistency CAP theorem, data consistency CAP theorem means consistent with the data in each node in a cluster environment. The ACID Consistency refers to the execution of the transaction need to meet our definition of business logic, to ensure the integrity logic.

  The simplest example is the transfer, A minus money account, B will add money to the account, we need to ensure that the total amount of AB accounts is unchanged.
  Isolation (Isolation)
  transaction isolation when a plurality of users concurrent access to the database, the user opens a database for each transaction, the operation data can not be disturbed by other transactions, for isolation between a plurality of concurrent transactions.

  Multi-channel programming in the most common point shared variables to ensure data security, data definition mutually exclusive relationships around shared between multiple threads of action to prevent data corruption.

  The simplest example of multi-barrier properties to ensure programming channel is a ++, because the action is not atomic. Pre-operation and its modification operations - there is data dependent read operation, in order to ensure correct operation of the program, they must be indivisible and thread exclusive. Two actions performed to ensure that the value of a process will not be changed by other threads. But the situation matters more complex than this, because the transaction involves more sharing of data, and there may be data-dependent relationship between operations on data within a transaction, for efficiency, we can not beat him to death, the entire transaction and other transactions the whole mutually exclusive, so a finer efforts to deal with matters in a mutually exclusive relationship for data manipulation.

  To the transaction as a unit, the case of multi-channel programming are possible:

  Dirty read: A transaction can read data from another transaction is not committed.

  Magic Reading: When a transaction modifies all of the data in the table, another transaction inserts a new data led to the first transaction found that there is a data is not modified.

  Non-repeatable read: repeatedly reading the same data in a transaction, the result will be different, since the two read gaps, other modifications of this transaction data.

  For this reason, most databases provide transaction isolation level four kinds:

  Read Uncommitted: Unlock query operation, phantom reads dirty reads and non-repeatable read occurs.

  Read Committed: The easiest way is to realize query data locking, but this will lead to a sharp decline in the concurrent performance, especially the query under a lot of circumstances. As compensation, the commonly used "snapshot reading" mechanism to avoid dirty reads. This can prevent dirty reads without locking, but can not avoid the non-repeatable read and phantom read. This is SQLSERVER default isolation level.

  Repeatable read: non-repeatable read problem for providing isolation level, the transaction once opened for data modification operations do not allow other things, such problems can be avoided repeatable read. But because there is no prohibition insert and delete operations, it is not possible to avoid phantom reads. This is the default isolation level of Mysql.

  Serialization: the highest level of security, but the efficiency is extremely poor. A transaction of a serial execution, so there is no dirty read / Magic Reading / concurrency issues can not be repeated reading.

  When database handle concurrency issues transactions, the lock granularity points is very small. Table lock / lock rows, and the type of lock is very large, respectively, for the add / delete / search / change four operations. Isolation level of the database, that is, in the case of four operating units of the transaction, a summary of the concurrency problems may cause as well as their solutions. If the daily development will often encounter thread synchronization design scene mutually exclusive relationship, it must be so well organized summary and stunning design to. Multi program design must be the most complete account of all possible problems, the problem according to the most granular formulation of targeted solutions to specific problems of the finer efforts, the more efficient solution.

  Persistent (Durability)
  Persistence means that once a transaction is committed, it changed data in the database is permanent, then even if the database fails nor should it have any impact.

  

Guess you like

Origin www.cnblogs.com/niuyourou/p/12640048.html