What is a transaction? Four properties of transaction isolation level, and transaction

What is a transaction

  A transaction is a series of rigorous logic operating procedures, and all operations must be completed successfully, otherwise made in each operation, all changes are undone. Popular understood to be: that is the more than one thing as a thing to deal with, like we live together in the same boat, to live, to finish finished together.

 

The four properties of things (ACID)

  ● Atomicity (Atomicity) : When the operation of these commands, performing either all succeed or is not performed. As soon as one command fails, all instructions fails, the data is rolled back, back to the state before the execution instruction data.

eg: take the transfer, if both user A and user B add up to a total of 20,000 money, no matter how transfers between A and B, turn several accounts, after the end of the transaction the money add up to two users should It had to be 20,000, which is transactional consistency.

  ● Consistency (Consistency) : execution of a transaction so that data from one state to another, but for the integrity of the whole remains stable data.

  ● Isolation (Isolation) : Isolation is when multiple users concurrent access to the database, such as operating with a table, a database for each user transaction open, operation not being disturbed by other transactions between a plurality of concurrent transactions to be isolated from each other.

              That is, to achieve such an effect: for any two concurrent transactions T1 and T2, T1 appears in the transaction, T2 or T1 has ended before the start, or started after the end of T1, so each transaction does not feel there are other matters to performing concurrently.

  ● Persistent (Durability Rev) : When the transaction is completed properly, it is for the data change is permanent.

eg:  for example, when we use the JDBC database operations, after the transaction is committed method, the user is prompted transaction is complete, when we complete the program execution until prompted, you can identify and correct the transaction submitted, even if this time the database there is a problem, To complete must also fully implement our business, otherwise it will cause us to see the prompt completion of the transaction, but because of the failure of major database without executing a transaction error.

 

Problems caused by concurrent transactions

  When many of the same transaction data, if no effective isolation mechanism, the concurrent processing of data, will bring some problems.

  ●  The first category is missing Update: When revocation of a transaction, update the data to other committed transactions covered.

eg: Xiao Ming go to the bank counter to save money, his account had a balance of $ 100, now intends to deposit $ 100. In the process of saving money in his bank $ 5 annual fee deducted, the balance only 95 yuan. Suddenly he was thinking about this 100 yuan to be used to ask his girlfriend to see a movie dinner, do not intend to keep up. After he withdrew saving operation, the balance still save 100 yuan for him before. So that the buckle 5 dollars in the end who?

  

  ● Dirty read: dirty read transaction refers to a process where the data is read uncommitted transactions in another.

eg: bank card balance Xiaoming, there are 100 yuan. Now he intends to use the mobile phone take-away point a drink, you need to pay 10 yuan. But this time, his girlfriend fancy clothes 95 yuan, she is using the bank card payment Xiaoming. So Xiaoming at the time of payment, the program reads back to his balance only five dollars, not enough $ 10, so the system rejected his deal, told insufficient balances. But Xiao Ming's last girlfriend because the password is wrong, can not be traded. Xiao Ming is very depressed, obviously there are 100 yuan bank card, how would lack balance it? (His girlfriend more depressed ...)

 

  ● Magic Reading also known as virtual read: query a transaction is performed twice, the second result set contains data for the first time there are no lines or some have been deleted, resulting in inconsistent results twice, just another transaction in which two intermediate query inserted or deleted data caused. Magic Reading is a phenomenon that occurs when a transaction non-independent execution.

eg: for example, transaction T1 to a data item in a table of all the lines made from "1" to "2" in operation, and this case transaction T2 data items and the table row is inserted into a data item, the value is set to "1" and committed to the database. The operations of the transaction T1 users to see if we just modify the data, you will find there is a line not modify, in fact, this line is added from the transaction T2, like hallucinations, like, this is what has happened phantom reads.

 

  ● non repeatable read: a transaction data of the same line is read twice, the result of the results of different states, the intermediate just another transaction to update the data, the results of two different, can not be trusted.

eg: for example, a read data transaction T1, T2 and immediately modify the transaction data and the transaction submitted to the database, transaction T1 the data is read again obtained different results, the non-repeatable read transmission.

Tips: not repeat the reading and the difference between dirty reads: is a dirty read transaction reads dirty data from another uncommitted transaction, and can not repeat the reading of data is read before a transaction commits. 

Tips: phantom reads and non-repeatable read are read another transaction has been submitted (this is different to dirty read), the difference is non-repeatable read query data items are the same, but the magic is for reading whole batch of data (such as the number of data).

 

  ●  The second category is missing Update: a special case of non-repeatable reads. If two things are read the same row, then both write and submit, changes made to the first thing lost.

eg: Xiao Ming and his girlfriend go shopping together. Girlfriend fancy a lipstick (Yes, that is used to represent girlfriend buy buy buy) Xiaoming generous took out his bank card and told his girlfriend: Honey, brush lightly, just buy, I sat waiting for you. Then Xiao Ming sat on the chair mall play phone, waiting for his girlfriend. This time, chat group of programmers someone recommended a book, Xiao Ming look, oh, really good book, or a limited edition of it, I have to buy more. So Xiao Ming quickly find purchase channels, payment operations. Meanwhile, Xiao Ming's girlfriend are enjoying themselves buy buy buy, they operate at the same time a deal, but this time the banking system is a problem, when they are successful payment, but found only the bank deducted Xiao Ming's money to buy books, but did not deduct money girlfriend at this time of the transaction. Ha ha ha, Xiao Ming was so happy!

 

Database transaction isolation level

  Transaction isolation level there are four kinds, from low to high respectively the Read Uncommitted  , the Read committed  , Repeatable Read  , the Serializable  . And it may occur in concurrent operation of a transaction dirty reads , non-repeatable read , phantom read . Below elaborated on the concept of contact through their stories.

Read uncommitted (the lowest level, in any case can not be guaranteed.)

Uncommitted Read, by definition, a transaction can read another data uncommitted transactions.

eg: the boss to give programmers wages, programmer salary is 36,000 / month. However, the salaries paid to the owner accidentally pressed the wrong number, press into a 39 000 / month, the money has hit the programmer's account, but the transaction has not been submitted, at this moment, the programmer to check their wages this month, We found three thousand dollars more than usual, that raise very happy. But the boss promptly found wrong, almost immediately roll back the transaction submitted, the numbers changed to 36000 and then submit.

Analyse: The actual programmers this month's wages, or 36,000, but the programmer see is 39000. He saw the data when the boss did not commit the transaction. This is the dirty read.

How to solve that dirty read it? Read committed! Read committed, can solve the problem of dirty read. 

 

Read committed (avoid dirty reads to occur.)

Read committed, by definition, is a transaction have to wait another transaction commits to read the data.

eg: programmer holding credit card to enjoy life (Cary course, is only 36000), when he pays the bill (programmer affairs open), charging systems detected in advance of his Cary 3.6 million and at this time! ! Programmer's wife should serve as a home all the money transferred out and submit. When the charging system is ready to charge, and then detecting the amount of Cary, we have found no money (the second test amount of course turn out to wait for his wife to submit complete the transaction amount). Programmers will be very depressed, obviously Cary wealthy ...

Analyse: This is the read-committed, if the transaction data is updated (UPDATE) operation, a read transaction to wait for the update operation after a transaction is committed to read the data, you can solve the problem of dirty read. But in this case, there has been the same query within a transaction scope but returned two different data, which is non-repeatable read.

That may be how to solve the non-repeatable read problem? Repeatable read!

 

Repeatable read (avoid dirty reads, unrepeatable reads occurring.)

When repeated reading, data is read at the beginning (open transaction), the operation is no longer permitted to modify

eg: programmer holding credit card to enjoy life (Cary course, is only 36000), when he pays the bill (open affairs, UPDATE not allowed to modify the operation of other matters), charging systems detected in advance of his Cary has 36000 . This time his wife can not be transferred out of the money. Then you can charge a toll system.

Analyse: Repeatable Read can solve non-repeatable read problem. Written here, it is to be understood that, corresponding to non-repeatable read is modified, i.e. UPDATE operation. But it may also have phantom read problem. Because the problem phantom read operation corresponding to the insertion INSERT, UPDATE operation instead.

What will appear when the phantom read?

eg: programmer one day to spend, spend $ 2,000, and then his wife went to see him spending records today (full table scan FTS, wife affairs open), see indeed spent two thousand dollars, at this , programmers spent 10,000 bought a computer, that is a new INSERT consumer records and submit. When the wife of the programmer print a list of records consumption (wife transaction commits), found spent 12,000 yuan, appears to be an illusion, this is the phantom reads.

That phantom read how to solve the problem? Serializable!

 

The Serializable (avoid dirty reads, non-repeatable read, phantom read occurs.)  Serialization

Serializable transaction isolation level is the highest, at this level, execution serialization order of the transaction, to avoid dirty reads, non-repeatable read and phantom read. But this is inefficient transaction isolation level, database comparison consumption performance, is not generally used.

Tips: Most database default transaction isolation level is Read committed, such as Sql Server, Oracle.

    Mysql default isolation level is Repeatable read.

Tips: isolation level setting is only valid for the current link. For using the MySQL command window, a window is equivalent to a link, the current window to set the isolation level is only valid for the current window of affairs; for JDBC for database operations, a Connection object is equivalent to a link, and for the Connection object set the isolation level only valid Connection object, independent of other links Connection object.

Tips: isolation level of the database must if in the open before the transaction.

Guess you like

Origin www.cnblogs.com/wukai66/p/11817961.html