Transaction Management (ACID) and transaction isolation level

https://blog.csdn.net/dengjili/article/details/82468576

Speaking affairs are generally the following four points

Atomicity (Atomicity)
Atomicity means that a transaction is an indivisible unit of work operations in a transaction either occur or do not occur.
Consistency (Consistency)
the integrity of the data before and after the transaction must be consistent.
Isolation (Isolation)
transaction isolation when a plurality of users concurrent access to the database, the user opens a database for each transaction, the operation data can not be disturbed by other transactions, for isolation between a plurality of concurrent transactions.
Persistent (Durability)
Persistence means that once a transaction is committed, it changed data in the database is permanent, then even if the database fails nor should it have any impact

As a simple example, understood that the above four points

Atomicity

For the same transaction

 

This process comprises two steps

A: 800 - 200 = 600
B: 200 + 200 = 400

Atoms of said two steps with success, or failure together, wherein one action occurs not only

Consistency (Consistency)

For a transaction consistent state before the operation and after operation

 

Before the operation A: 800, B: 200
After the operation A: 600, B: 400

After the transaction is complete consistency representation, logical operation

Persistent (Durability)

The data indicate that the transaction does not end with the external causes of data loss

Before the operation A: 800, B: 200
After the operation A: 600, B: 400
if the (not yet committed the transaction) before the operation of the server is down or powered off, then after restarting the database, the status data should be
A: 800, B: 200
If (the transaction is committed) after operation of the server is down or powered off, then after restarting the database, the status data should be
A: 600, B: 400

Isolation (Isolation)

For multiple simultaneous users, mainly to eliminate the influence of other matters on this transaction

 

Two simultaneous transactions, wherein a read transaction to another transaction data not yet submitted, B


Transaction isolation level

Dirty read:

It refers to a transaction reads data of another uncommitted transactions.

 

### Non-repeatable read:
reading a data table row in a transaction, read many times different results. (This is not necessarily wrong, but some cases do not)

Page statistical value queries

Click Run report when, B transfer someone come in 300 (the transaction has been submitted)


Dummy read (phantom read)

It refers to a transaction in another transaction to read data insertion, before and after the read result in inconsistent.
(Typically affecting the line, more than a line)

 

The four isolation levels set

#### Database
set transaction isolation level set transaction isolation level
select @@ tx_isolation query the current transaction isolation level

Setting Description
Serializable avoid dirty reads, non-repeatable read, dummy read from happening. (Serialization)
Repeatable Read avoid dirty reads, non-repeatable read from happening. (Repeatable read)
the Read committed to avoid dirty reads from happening (read committed).
Read uncommitted lowest level, the above are not guaranteed. (Uncommitted read)
#### Java
suitable Connection method such setAutoCommit or setTransactionIsolation

Setting Description
TRANSACTION_SERIALIZABLE constant indicating that dirty reads, non-repeatable read and dummy read.
TRANSACTION_REPEATABLE_READ indicating that dirty reads and non-repeatable read occurs not constant; phantom reads can occur.
TRANSACTION_READ_UNCOMMITTED indication may dirty reads (dirty read), the read and dummy read constants (phantom read) is not repeated.
TRANSACTION_READ_COMMITTED constant indicating that dirty reads may not occur; non-repeatable reads and phantom reads can occur.
mysql simulated transaction isolation test

SELECT @@session.tx_isolation;
SELECT @@tx_isolation;

SET SESSION TRANSACTION ISOLATION LEVEL read uncommitted;
SET SESSION TRANSACTION ISOLATION LEVEL read committed;
SET SESSION TRANSACTION ISOLATION LEVEL repeatable read;
SET SESSION TRANSACTION ISOLATION LEVEL serializable;

start transaction;

- to build the table
drop Table AMOUNT;
the CREATE TABLE AMOUNT` `(
` id` VARCHAR (10) NULL,
`money` numeric NULL
)
;
- to insert data
insert into amount (id, money) values ( 'A', 800) ;
insert into amount (id, Money) values ( 'B', 200 is);
insert into amount (id, Money) values ( 'C', 1000);
- test repeatable read, insert data
insert into amount (id, money) values ( 'D', 1000);

- Set the transaction
the SET SESSION TRANSACTION ISOLATION the LEVEL the Read Uncommitted;
the SELECT @@ TX_ISOLATION;
- open transaction
start transaction;

- dirty reads demo, read uncommitted data from other transactions
- the case of column 1, a transaction: A turn to B 200, the transaction II: B to view the amount of change, a transaction rolls back the transaction
update amount set money = money - 200 ID = WHERE 'A';
Update SET AMOUNT Money Money + = 200 is WHERE ID = 'B';

- Non-repeatable read presentation data read other transactions submitted
- column 2 case, a transaction: B to turn 200 A, two transactions: B 200 rpm to 100 rpm C
the SET TRANSACTION the SESSION Read the ISOLATION the LEVEL committed;

- Open Transaction
Start Transaction;
- two transactions are check data (required before transfer, check whether the amount enough to satisfy the transfer)
SELECT * from AMOUNT;
- a transaction: B to 200 rpm A
Update Money = Money SET AMOUNT - WHERE ID = 200 is 'B';
Update SET AMOUNT Money Money + = 200 is WHERE ID = 'A';

the commit;
- two transactions: B 200 rpm to 100 rpm C
Update SET AMOUNT Money Money = - 100 WHERE ID = 'B';
Update Money SET AMOUNT WHERE ID = Money + 100 = 'C';
the commit;
- from the transaction two point of view, a read data transaction commits the transaction, resulting in the amount of negative numbers

- Magic Reading Demo
- row 3 case, a transaction: B to turn 200 A, two transactions: B C 200 to turn switch 100
the SET TRANSACTION the ISOLATION the LEVEL Repeatable Read the SESSION;

- Open Transaction
Start Transaction;
- two transactions are check data (required before transfer, check whether the amount enough to satisfy the transfer)
SELECT * from AMOUNT;
- a transaction: B to 200 rpm A
Update Money = Money SET AMOUNT - WHERE ID = 200 is 'B';
Update SET AMOUNT Money Money + = 200 is WHERE ID = 'A';

the commit;
- two transactions: B 200 rpm to 100 rpm C
Update SET AMOUNT Money Money = - 100 WHERE ID = 'B';
Update Money SET AMOUNT WHERE ID = Money + 100 = 'C';
the commit;
- from the transaction two angles of view, a read data transaction commits the transaction, resulting in the amount of negative numbers
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
52 is
53 is
54 is
55
56 is
57 is
58
59
60
61 is
62 is
63 is
64
65
66
67
68
69
70
71 is
72
Serializable two transaction will be waiting transaction commit operation then

https://blog.csdn.net/dengjili/article/details/82468576

 

Guess you like

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