Detailed explanation of the four characteristics of transactions

1. Affairs

       Definition: The so-called transaction, it is a sequence of operations, these operations are either performed or not performed, it is an indivisible unit of work.

       Preparation: In order to illustrate the ACID principle of transactions, we use the case of bank account and money management for analysis.

 

       

  1. // create database  
  2. createtable account(   
  3.    idint  primary key not null ,     
  4.    namevarchar(40),  
  5.    moneydouble  
  6. );  
  7.   
  8. // Two people open an account and deposit money  
  9. insertinto account values(1,'A',1000);   
  10. insertinto account values(2,'B',1000);   
       // create database
       create table account(
          idint primary key not null,
          namevarchar(40),
          moneydouble
       );
 
       // Two people open an account and deposit money
       insert into account values(1,'A',1000);
       insert into account values(2,'B',1000);


2. ACID

       ACID refers to the four characteristics that a transaction should have in a reliable database management system ( DBMS ) : Atomicity , Consistency , Isolation , Durability . These are several features that a reliable database should have . The following will explain these features one by one .


3. Atomicity

       Atomicity means that a transaction is an indivisible unit of work, and the operations in the transaction either all happen or none of them happen.

       1. Case

              A transfers 100 yuan to B

 

       

  1. begintransaction   
  2. update account set money= money - 100where name='A';  
  3. update account set money= money +100where name='B';  
  4. if Error then  
  5.        rollback  
  6. else  
  7.        commit  
       begin transaction
       update account set money= money - 100where name='A';
       update account set money= money +100where name='B';
       if Error then
              rollback
       else
              commit

 

       2. Analysis 

       The two statements of deduction and addition in a transaction are either executed or neither of them are executed. Otherwise, if only the deduction statement is executed, it will be submitted. At this time, if the power is suddenly turned off, account A has already been debited, but account B has not received the additional payment, which will cause disputes in life.

 

       3. Solution

       In a database management system ( DBMS ), a SQL is a single transaction by default , and the transaction is automatically committed. A block of code can only be executed in a transaction by explicitly starting a transaction with start transaction . It is the responsibility of the database management system to ensure the atomicity of transactions, and many data sources use logging mechanisms for this. For example, SQL Server uses a write-ahead transaction log where data is written to the transaction log before committing to the actual data page.


4. Consistency

       Consistency means that the integrity constraints of the database are not violated before the transaction begins and after the transaction ends. This means that database transactions cannot destroy the integrity of relational data and the consistency of business logic.

       1. Case

       For bank transfer transactions, regardless of whether the transaction succeeds or fails, it should be ensured that the total deposits of aaa and bbb in the ACCOUNT table after the transaction is completed is 2,000 yuan.

 

       2. Solution

To ensure the consistency of transactions, you can start from the following two levels

       2.1 Database mechanism level

       Consistency at the database level is that before and after a transaction is executed, the data will conform to the constraints you set (unique constraints, foreign key constraints , Check constraints, etc. ) and trigger settings. This is guaranteed by SQL SERVER . For example, transfer, you can use CHECK to constrain the sum of two accounts to be equal to 2000 to achieve consistency

       2.2 Business level

   For the business level, consistency is maintaining business consistency. This business consistency needs to be guaranteed by developers. Of course, the consistency of many business aspects can also be guaranteed by transferring to the database mechanism level.


5. Isolation

       When multiple transactions access concurrently, the transactions are isolated, and one transaction should not affect the running effect of other transactions.

       This refers to in a concurrent environment, when different transactions manipulate the same data at the same time, each transaction has its own complete data space. Modifications made by concurrent transactions must be isolated from modifications made by any other concurrent transactions. When a transaction views data updates, the state of the data is either the state before another transaction modifies it, or the state after another transaction modifies it, and the transaction does not view the data in the intermediate state.

       In Windows , if multiple processes are not allowed to modify the same file, Windows ensures the isolation of different processes in this way :

   

       In enterprise development, the most complex transaction problems are caused by transaction isolation. When multiple transactions are concurrent, SQL Server uses locking and blocking to ensure different levels of isolation between transactions. In general, complete isolation is unrealistic. Complete isolation requires the database to execute only one transaction at a time, which will seriously affect performance. To understand the guarantees of isolation in SQL Server , we must first understand how concurrent transactions interfere .

6. Persistence

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

       Even if there is any accident such as power failure, once the transaction is committed, it will be persisted in the database.

       SQL SERVER guarantees durability through write-ahead transaction log . Write-ahead transaction log means that changes to the database in the transaction are first written to the transaction log before being written to the database. The transaction log is in sequential order ( LSN ). When the database crashes or the server breaks, restart SQL SERVER , SQL SERVER will first check the log sequence number, and persist the part that should have been changed to the database but not made to the database, thus ensuring durability.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325216892&siteId=291194637