Overview mysql transaction rollback mechanism

Scenarios:
   bank money, withdraw money from the ATM machine, divided into the following steps
       1 ATM machine login, password;
    2 connected to a database, the authentication password;
    3 authentication is successful, to obtain user information, such as deposits and the like;
    4 user input required withdrawal amount, press enter;
    5 subtract the amount corresponding to the user account from the background database;
    . 6 discharging the ATM money;
    7 the user to take the money.
    For withdrawals above this thing, if there is an error in one step, then it will cancel the entire operation to withdraw money, but if in step 5, the system has been put back money cut, but not taken out of the ATM machine, then the application mysql in the transaction. Simply
, is to get money these seven steps are either completed, or to do what is not, is the truth in the database.


    A transaction is a sequence of user-defined database operations, these operations are either full or do not do the whole, an indivisible unit of work, transaction rollback means that the transaction has completed an update operation of the database revoked, in a transaction, each correct atomic
operations are sequentially performed until an error is encountered atomic operation. In fact, even if the rollback means is inserted before the operation, then performs inserted before deleting records, if it is to modify the operation, then performs the record before the update restore.
Therefore, the correct atomic operation is actually being performed, and a physical execution.

    A transaction is one or more pieces from the sql statements in the operation of its services in either of these sql statements are executed or not executed.

    ACID properties of transactions: atomicity, consistency, isolation, durability.
   
   You can really see the inserted record in the current transaction, only the last is deleted, but does not delete but to change the value auto_increament


   Why auto_increament no rollback: Because auto_increment is the current value of the primary key of the record count innodb storage engine is stored in memory, it does not exist when mysql server is running, and the count value only with disk
insert increase, not decrease as delete. And when mysql server startup, when we need to query auto_increment count value, mysql will automatically execute: SELECT MIX (ID) FROM table name FOR UPDATE; auto_increment this statement to obtain
the maximum value of the column, and then put this value to auto_increment counter, so ROLLBACK MYSQL will not do the auto_increment counter negative operation


    Transaction which is divided into species: flat transactions with transaction savepoints flat, the transaction chain, nested transactions, a distributed transaction.


    MYSQL used in the transaction:
  transactions are automatically filed in the MYSQL command line commands that execute Sql statement will execute COMMIT operation immediately. So you want to display on a transaction must use the command BEGIN or START TRANSACTION, or execute commands SET AUTOCOMMIT = 0 to
disable automatic submission of the current answer


   Transaction control statements:
the BEGIN / the START TRANSACTION: display starts a transaction
COMMIT: COMMIT WORK may be used both are equivalent. COMMIT commits the transaction, and that all changes have been made to the database permanent.
ROLLBACK: You can also use ROLLBACK WORK, both also equivalent, end user transaction rollback, and will remove all uncommitted changes under way.
SAVEPOINT identifier: allows you to create a save point in the transaction, a transaction can have multiple SAVEPOINT
Release SAVEPOINT identifier: Delete a transaction save point, when you save a point not developed, it will throw an exception. 
SET TRANSACTION: to set the transaction isolation level. Innodb transaction isolation level storage engine provided with a READ UNCOMMITED, READ COMMITED, REPEATABLE READ and SERIALIZABLE.
   
    Transaction isolation level: a database operation, to ensure the accuracy of data read concurrent proposed isolation level, as
  the following differences:
  Isolation the level of dirty read (dirty read) non-repeatable read (nonRepeatable read) magic reading (Phantom read)
  read uncommitted read uncommited possible possible possible
  read Committed read commited impossible possible may
  be impossible to repeat impossible to read repeatable read may
  be serial of serializable impossible impossible impossible


   Dirty read: A transaction reads data to another transaction did not submit the
example: Transaction T1 updated contents of his record, but did not submit your changes. Read transaction T2 to the T1 line after update, and then T1 rollback operation, just cancel the edits. Now read the T2 line is invalid


   Non-repeatable read: in the same transaction, read the same data twice, resulting in different contents
, for example: Transaction T1 reads a row, followed by modify transaction T2 T1 of the row just read is recorded. T1 line and then read the records again and found that the results have just read different. This is called "non-repeatable" read, because it reads the original rows T1 has changed


   Magic Reading: in the same transaction, read twice by the same operation, the number of different records obtained
, for example: Transaction T1 set a reading result of the WHERE clause specifies returned. The new transaction T2 then insert a row, rows just to meet this condition T1 query criteria used in the WHERE clause. T1 and then use the same query to retrieve the table again,
     but this time they see a new line transaction T2 just inserted. The new line is called "phantom" because this line of T1 is the same as the sudden appearance of
   
   the lower isolation level, the less the transaction request petty or trivial to maintain a shorter period of time, Innodb supported by default storage engine isolation sector is REPEATALE READ; in this default transaction isolation level has been able to fully guarantee transaction isolation.  


   How to implement the code mysql transaction rollback can refer to this bolg: http: //bbs.csdn.net/topics/390876901 

   To modify the database at the same time two different tables, if they are not a transaction, so that when the first table complete modification, there may be a second table to modify the course of abnormalities did not modify, then only the second table still unmodified state before,
   and the first table has been modified completed. And when you put them set a transaction time, when you modify the first table, the second table to modify abnormal and could not amend, the first table and the second table should be returned unmodified state, this is called a transaction rollback.
---------------------
Disclaimer: This article is CSDN blogger "aschulianwuyanzu 'original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/aschulianwuyanzu/article/details/77879727

Guess you like

Origin www.cnblogs.com/lxwphp/p/11364549.html