Examples of database isolation level

Four isolation levels:

  Read uncommitted: --- not isolated to prevent any problems, can not prevent the dirty read / unrepeatable read / phantom read problem.

  Read commit: --- prevents the dirty read problem, but can not prevent non-repeatable read / phantom read problem. (Oracle default isolation level)

  Repeatable read: --- possible to prevent dirty reads / unrepeatable read, phantom read can not be prevented. (Mysql default isolation level)

  Serializable: - database is designed to be single-threaded, all these problems can be prevented

  Dirty read: A transaction reads data to another uncommitted transactions.

  Scene: a account 1000, b account 1000, a shop to buy a hundred dollars b's shoes. . . .

  a:start transaction

  update table set money=money-100 where name='a';//a账户扣除100

  update table set money = money + 100 where name = 'b'; // b accounts increased 100

  At this point a not commit the transaction, said to b successful payment

  b: start transaction; // b began to check their account balance

  select money form table where name='b';

  b:commit;

  If the data is not submitted a read, then check out the b would be 1100, when shipments start b

  In this case a rollback

  a:rollback;

  Then b go check account balances will find their accounts or the original 100

 

  Non-repeatable read: in the current transaction, read the transaction data submitted by another update and delete.

  Scenario: If there is a current capital of 10 regular 1000, fixed assets 10

  a: 1000 1000 1000
  b: bank staff, bank staff let b calculate a customer's total assets
  ---------
  b: Start Transaction;
  the SELECT demand deposits from account where name = 'a' ; ---- demand deposits: 1000
  SELECT deposits from account where name = 'a' ; ---- deposits: 1000
  SELECT fixed assets from account where name = 'a' ; ---- fixed assets: 1000 
    ------ - At this point a customer to take their current $ 1000
    a:
    Start Transaction;
    Update accounset SET current = current WHERE name = -1000 'a';
    the commit;
  -------
  SELECT + fixed periodic current from account + where name = 'a'; --- total assets: 2000, as previously found out should be 3,000, 2,000 yuan but now
  the commit;
  ----------

  But although in this scenario, the emergence of non-repeatable read, but if another scenario, if you and your partner share a total of one million bank account, your partner if she had removed 50 million, and this you when reading the remaining 50 million and will comply with the scene.

  Phantom read: in the current transaction, read the data into another transaction commits

  Scenario: If the bank now has a, b two each 1000 customer deposits, the bank boss asked staff to count b Average Average deposits per customer, and have a new customer deposit c 4,000 yuan.

  A: 1000
  B: 1000
  D: banking staff

  -----------
  D:
  Start Transaction;
  SELECT SUM (Money) from Account; 2000 --- membered
  select count (name) from account; - - 2

  ------
  C:
Start Transaction;
INSERT INTO Account values (C, 4000);
the commit;
------

SELECT SUM (Money) / COUNT (name) from Account; --- average: 2000 yuan / month, the result should be the average deposit 1000, but the actual result is indeed 2000
the commit;
------------

Guess you like

Origin www.cnblogs.com/YuyuanNo1/p/11401072.html