Another transaction isolation level transaction to open the way

There is a transaction isolation level

read uncommited (read uncommitted): without any isolation, have dirty reads, non reread right, phantom read problems

read committed (read submission): prevents dirty reads, does not prevent non-repeatable read and phantom read problems

repeated read (Repeatable Read): it is possible to prevent dirty reads, non-repeatable read, not on the phantom read (default mysql isolation level)

serializable (serialization): the database is running as a serial, the above problems can be prevented, but low performance

The following explanations with the user table

 

Dirty read (a, b at the same time begin a transaction, a, b and then the transaction, which is not an end, after all, only two went into business, to end a transaction period, two transactions are alive)

1 A transaction will turn into jason age of 18 years, but did not submit the transaction
 2 b begin transaction reads jason age was found to be 18 years old. 

The above problems, 
assume that a transaction is rolled back, the transaction data b used is wrong, the program has led to incorrect data.

Unrepeatable degree (after a, b at the same time begin a transaction, a, b and then the transaction, which is not the end, only two have entered a transaction, a transaction before the end of the period, two transactions are alive) the Read committed

1 a transaction will begin to change the age of 18 years old jason, but submitted
 2 b open transaction reads jason jason age can be read after the age of 18 years of a transaction modification. 

# If a uncommitted, b read jason ages will get stuck

Re-read (a, b at the same time begin a transaction, a, b and then the transaction, which is not an end, after all, only two went into business, to end a transaction period, two transactions are surviving) a REPEATED the Read

1 a transaction will begin to change the age of 18 years old jason, but submitted
 2 b open transaction reads jason jason age can not read after the age of 18 years of a transaction modification. But reading is 38 years old

Magic Reading (a, b at the same time begin a transaction, a, b and then the transaction, which is not an end, after all, only two went into business, to end a transaction period, two transactions are alive) Serializable solve the problem of phantom read

# Concept and not a bit like re-read, non-repeatable degree of modification is the basis for standing on, and phantom reads are standing on the basis of the new 
1 A to start the transaction all of the age of 18 years into
 2 b begin a new transaction Tank data = name, Age = 19 , and submitted.
3 a re-investigation, we found that a Data age = 19, which is the so-called phantom reads.

Msql see how isolation levels

@@ the SELECT , Ltd. Free Join .tx_isolation;     # default is REPEATABLE-READ

How to modify the transaction isolation level

# Modify the configuration files in the folder mysql configuration file my.ini view 
Transaction-Isolation = Read-The Committed
 # modified must restart the database (after deleting a default reply REPEATABLE-READ)

Optimistic and pessimistic locking lock database (the basis for the establishment of another transaction on)

Pessimistic locking

Pessimistic locking concept, he felt someone will modify the data I read. Then I check the time again, the data is locked. 
(Pessimistic lock: assuming sent concurrently from FIG shield may violate data integrity of all operations, others can check theoretically) 

SELECT Age from User ID = WHERE. 1 for Update; 
such as the above check out Age = 18 is  
Update change Age as Age = Age + 1 
if the queries gone index that row lock, if not take the index is a table lock. 

How to release the lock it? End of the transaction will release the lock. (commit or ROLLBACK) 
Django in orm is if pessimistic locking? 
user.objects.select_for_update (). filter (ID =. 1) .first ()
Native sql statement:
 1 open transactions
 when a query is locked 2 --- "the SELECT * from the User the WHERE the above mentioned id = 1 for Update
 3 when the transaction ends the lock is released 
django of:
 1 start transaction
 2 when the query - -. "User.objects.select_for_update () filter (the above mentioned id = 1 ) .first ()
 3 can modify the data, the data is never modified such people, only you active
 4 when the transaction ends the lock is released

Optimistic locking

Nature is not optimistic locking lock. He is achieved through the lock code. He felt his data of others will not be modified. He studied the data will not back someone else changes. Finally, modification of the record, and then add a condition where, prior to check out the data. To ensure the security of data 
SELECT Age from User ID = WHERE. 1 ; 
such as the above check out Age = 18 is    
20 is  
Update changed Age Age = Age +. 1 ==== ". 19 

optimistic locking: 
Update changed Age Age = Age +. 1 Age = 18 the WHERE and the above mentioned id = 1 ==== "19 
If I find instead of 18, the number is above the impact row 0 

# If repeatable read, the above lock is not optimistic, must be changed to read committed * django2.0 later , with no need to modify optimistic locking isolation level (should be partially modified) * 
A change Update Age age = age + 1 ==== "19 Submit 

B SELECT Age from User ID = WHERE. 1; ---> 18 is      # repeatable read the case of isolated sectors, optimistic locking failure 

update age changed Age = Age +. 1 wHERE Age = 18 is and id = 1 ==== "19 ----> data has been changed
Optimistic nature is not a lock, data security is achieved by code level
 1 begins a transaction
 when a query 2 Do not do anything = User.objects.filter the Data (the above mentioned id = 1 ) .first () in this age of data in the original basis 1 is added to
 3 when modifying data. User.objects.filter (the above mentioned id = 1, Age = data.age) .Update (Age = data.age + 1 ) to ensure that in my time I queried this modification, no one had my data.
4 If the number of rows updated impact of 3 to 1, indicating that the data is not change others, only I move, if the impact of the number of rows in the 3 to 0, indicating that the data has been modified others. At this time we want to change the data, it must be repeated 2-3 two not until a few influential 3 lines.

 

Another way to open the transaction

django transaction usage
 from django.db Import Transaction
 from django.http Import HttpResponse
 from django.utils.decorators Import method_decorator 


# add a decorator class, to ensure that all database operations are a thing in this class 
@method_decorator (transaction.atomic , name = ' dispatch ' )
 class MyView1 (Object): 
    
    DEF GET (Self, Request):
         return the HttpResponse ( ' the GET ' ) 
    
    DEF POST (Self, Request):
         return the HttpResponse ( ' the POST ') 


Class MyView2 (Object): 
    
    # ensure the function of all the operations in a database things 
    @ transaction.atomic
     DEF POST (Self, Request): 

        # Set things savepoint (can be provided a plurality of) 
        T1 = transaction.savepoint () 

        # if there are unusual circumstances can roll back to a specified savepoint 
        transaction.savepoint_rollback (T1) 

        # If there are no abnormalities can submit things 
        transaction.savepoint_commit (T1)
         return HttpResponse ( ' POST ' )

Reference point may roll back

https://blog.csdn.net/rockpk008/article/details/25437415

 

Guess you like

Origin www.cnblogs.com/ludingchao/p/12554513.html