The four isolation levels of database transaction database

Four isolation level of the database

 

  Recently read a book MYSQL high performance, it has consolidated its own analysis of examples impression

  Transaction operation of the database is actually a set of atomic operation, either all operations succeed, or all the operation fails.

  

  For example, I need an external sales of movie tickets, registration and information about sales to another table, you need at least the following three steps

  1. Query whether the number of movie ticket sales meet a movie ticket SELECT remain_count FROM cinema WHERE film_id = 123456789;

  2. Update the number of movie tickets UPDATE remain_count = remain_count -1 FROM cinema WHERE film_id = 123456789;

  3. Insert sales information INSERT INTO sell_mes (id, mes) values ​​(id, mes);

  Imagine if one of us is wrong step or other disruption of operations is very prone to problems. For example, there are two sales systems A, B in selling the same ticket, this time only one ticket, A received orders to sell a ticket to see his movie ticket number is greater than 1, then sold to when, that is, when the first step in the implementation of the second step is completed, B also received orders, we also see the ticket is greater than 1, B 1 tickets have sold. At this point there have been absurd cases more than one ticket was sold only two appear.

  Therefore, a good system is required by ACID test ACID as to what is, on its own Baidu it, this blog talking about the isolation level isolation and examples

  The transaction isolation level four, high reliability, high isolation level of the database, but the low concurrency, low low isolation level database reliability, high concurrency, low system overhead

  

  1.READ UNCIMMITTED (uncommitted read)

  Modify the transaction, even if not submitted, other transactions can also be seen, such as the above two steps This phenomenon is called a dirty read, this isolation level can cause a lot of problems, if not necessary, do not use

  Example: or ticketing system, Xiao Ming and flowers are the conductor, they are employees of two different windows, ticketing system now only three votes, this time A Xiaohua come here to buy three tickets, B tickets to Xiao Ming, vote Xiaohua found there then gave the order, it is necessary to perform the third step, when Xiao Ming received a request query B has no more than votes. See Xiaohua sold three tickets, and refused to sell tickets. But Xiaohua system is a problem, the third step fails, the database has been rolled back to ensure atomicity, data, that did not sell a ticket.

  Summary: This is the transaction has not submitted, and you can see the consequences of his other affairs which the modified data, that is dirty read.

  

  2.READ COMMITTED (read committed)

  The default isolation level for most database systems is READ COMMITTED, this isolation level is the beginning of a transaction, only to see the results of already completed transactions, being executed can not be seen by other transactions. This level will read the old data phenomenon

  Example: Xiao-Ming Xiaohua or salesman, I vote three, A Xiaohua there to request three single booking, Xiaohua accept orders to sell three tickets, sales above steps in time, B also came Xiao Ming to buy a ticket there, due to the sales transaction execution Xiaohua half, Xiao-Ming Xiaohua of the transaction did not see the transaction execution, read three votes, ready to accept orders when Xiaohua sales transaction is completed, and this time Xiao Ming the display system becomes 0 votes, Xiao Ming was about to press the mouse click to accept orders for hand and then quickly shrink back.

  Summary: This is the Xiaohua transaction execution half, while Xiao Ming not see him executed operation, so to see that the old data is invalid data

 

  3.REPEATABLE READ (repeatable read)

   REPEATABLE READ solve the problem of dirty read, the record level to ensure that the results of each line is the same, that is to say above problems reading old data, but it can not solve another problem, phantom lines, by definition is a sudden jump out of rows of data. It refers to a transaction in reading a range of data, but another transaction ED data to insert this range of data, resulting in multiple reads of time, the number of rows inconsistent data.

  Examples: sales provisions, if sales records below the specified value, to deduct wages, this time to see the manager in the back end a bit console sales records Xiaoming, found that less than the required number of sales records, and my heart feel pleased, ready Print a list of good sales, righteous and made Xiao Ming, did not expect to find the time to print out a list of which increased sales volume selling a few, just right to meet the requirements, the manager of tear gas inventory paper. Xiao Ming was originally printed in an instant will sell some tickets, and therefore avoid a wage reduction of mass annihilation.

  Summary: While reading the same data ensures consistency, but can not guarantee that no insert new data

 

  4.SERIALIZABLE (serializable)

  SERIALIZABLE isolation level is the highest, it performs a transaction by forcing a serial (note the serial), to avoid the situation in front of the phantom read, plus a lot because of his lock, resulting in a large number of requests time out, so the performance would be more underneath, then special It requires data consistency and concurrency does not require so much when it might consider this isolation level.

  Recently read a book MYSQL high performance, it has consolidated its own analysis of examples impression

  Transaction operation of the database is actually a set of atomic operation, either all operations succeed, or all the operation fails.

  

  For example, I need an external sales of movie tickets, registration and information about sales to another table, you need at least the following three steps

  1. Query whether the number of movie ticket sales meet a movie ticket SELECT remain_count FROM cinema WHERE film_id = 123456789;

  2. Update the number of movie tickets UPDATE remain_count = remain_count -1 FROM cinema WHERE film_id = 123456789;

  3. Insert sales information INSERT INTO sell_mes (id, mes) values ​​(id, mes);

  Imagine if one of us is wrong step or other disruption of operations is very prone to problems. For example, there are two sales systems A, B in selling the same ticket, this time only one ticket, A received orders to sell a ticket to see his movie ticket number is greater than 1, then sold to when, that is, when the first step in the implementation of the second step is completed, B also received orders, we also see the ticket is greater than 1, B 1 tickets have sold. At this point there have been absurd cases more than one ticket was sold only two appear.

  Therefore, a good system is required by ACID test ACID as to what is, on its own Baidu it, this blog talking about the isolation level isolation and examples

  The transaction isolation level four, high reliability, high isolation level of the database, but the low concurrency, low low isolation level database reliability, high concurrency, low system overhead

  

  1.READ UNCIMMITTED (uncommitted read)

  Modify the transaction, even if not submitted, other transactions can also be seen, such as the above two steps This phenomenon is called a dirty read, this isolation level can cause a lot of problems, if not necessary, do not use

  Example: or ticketing system, Xiao Ming and flowers are the conductor, they are employees of two different windows, ticketing system now only three votes, this time A Xiaohua come here to buy three tickets, B tickets to Xiao Ming, vote Xiaohua found there then gave the order, it is necessary to perform the third step, when Xiao Ming received a request query B has no more than votes. See Xiaohua sold three tickets, and refused to sell tickets. But Xiaohua system is a problem, the third step fails, the database has been rolled back to ensure atomicity, data, that did not sell a ticket.

  Summary: This is the transaction has not submitted, and you can see the consequences of his other affairs which the modified data, that is dirty read.

  

  2.READ COMMITTED (read committed)

  The default isolation level for most database systems is READ COMMITTED, this isolation level is the beginning of a transaction, only to see the results of already completed transactions, being executed can not be seen by other transactions. This level will read the old data phenomenon

  Example: Xiao-Ming Xiaohua or salesman, I vote three, A Xiaohua there to request three single booking, Xiaohua accept orders to sell three tickets, sales above steps in time, B also came Xiao Ming to buy a ticket there, due to the sales transaction execution Xiaohua half, Xiao-Ming Xiaohua of the transaction did not see the transaction execution, read three votes, ready to accept orders when Xiaohua sales transaction is completed, and this time Xiao Ming the display system becomes 0 votes, Xiao Ming was about to press the mouse click to accept orders for hand and then quickly shrink back.

  Summary: This is the Xiaohua transaction execution half, while Xiao Ming not see him executed operation, so to see that the old data is invalid data

 

  3.REPEATABLE READ (repeatable read)

   REPEATABLE READ solve the problem of dirty read, the record level to ensure that the results of each line is the same, that is to say above problems reading old data, but it can not solve another problem, phantom lines, by definition is a sudden jump out of rows of data. It refers to a transaction in reading a range of data, but another transaction ED data to insert this range of data, resulting in multiple reads of time, the number of rows inconsistent data.

  Examples: sales provisions, if sales records below the specified value, to deduct wages, this time to see the manager in the back end a bit console sales records Xiaoming, found that less than the required number of sales records, and my heart feel pleased, ready Print a list of good sales, righteous and made Xiao Ming, did not expect to find the time to print out a list of which increased sales volume selling a few, just right to meet the requirements, the manager of tear gas inventory paper. Xiao Ming was originally printed in an instant will sell some tickets, and therefore avoid a wage reduction of mass annihilation.

  Summary: While reading the same data ensures consistency, but can not guarantee that no insert new data

 

  4.SERIALIZABLE (serializable)

  SERIALIZABLE isolation level is the highest, it performs a transaction by forcing a serial (note the serial), to avoid the situation in front of the phantom read, plus a lot because of his lock, resulting in a large number of requests time out, so the performance would be more underneath, then special It requires data consistency and concurrency does not require so much when it might consider this isolation level.

Guess you like

Origin www.cnblogs.com/hejj-bk/p/11445299.html