1
+ /* ***********************************************/
2
+ /* Transactions and Concurrency */
3
+ /* ***********************************************/
4
+ /*
5
+ ACID properties
6
+
7
+ Atomicity
8
+ Consistency
9
+ Isolation
10
+ Durability
11
+ */
12
+
13
+ /* ***********************************************/
14
+ /* Creating Transactions */
15
+ /* ***********************************************/
16
+ SHOW VARIABLES like ' autocommit' ;
17
+ SET autocommit = 0 ;
18
+
19
+ USE mosh_sql_store;
20
+
21
+ START TRANSACTION ;
22
+
23
+ INSERT INTO orders (customer_id, order_date, status)
24
+ VALUES (1 ,' 2020-04-05' ,1 );
25
+
26
+ INSERT INTO ordessr_items
27
+ VALUES (LAST_INSERT_ID(),1 ,1 ,1 );
28
+
29
+ COMMIT ;
30
+
31
+ SET autocommit = 1 ;
32
+
33
+ /* ***********************************************/
34
+ /* Concurrency and Locking */
35
+ /* ***********************************************/
36
+
37
+ /* ***********************************************/
38
+ /* Cocurrency Problems */
39
+ /* ***********************************************/
40
+
41
+ /*
42
+ - Lost Updates
43
+ - Dirty Reads
44
+ - Non Repating Reads
45
+ - Phantom Reads
46
+
47
+
48
+ 1) Lost Updates
49
+ - This problem occurs when multiple transactions execute concurrently and updates from one or more transactions get lost.
50
+
51
+ Solution Isolation Level: LOCK
52
+
53
+
54
+ 2) Dirty Reads
55
+ - Reading the data written by an uncommitted transaction is called as dirty read.
56
+
57
+ Solution Isolation Level: READ COMMITTED
58
+
59
+
60
+ 3) Non Repating Reads / Unrepeatable Reads
61
+ - This problem occurs when a transaction gets to read unrepeated
62
+ i.e. different values of the same variable in its different read operations even when it has not updated its value.
63
+
64
+ Solution Isolation Level: REPEATABLE READ
65
+
66
+
67
+ 4) Phantom Reads
68
+ - This problem occurs when a transaction reads some variable from the buffer
69
+ and when it reads the same variable later, it finds that the variable does not exist.
70
+
71
+ Solution Isolation Level: SERIALIZABLE
72
+
73
+ */
74
+
75
+ /* ***********************************************/
76
+ /* Transaction Isolation Levels */
77
+ /* ***********************************************/
78
+ /*
79
+
80
+ - the higher the isolation level, the lower the concurrency issues as it can solve these.
81
+ - but the price is cost is very expensive as it can slow the queries.
82
+ - SERIALIZABLE make sure each queries are running in sequences and wait other to be completed.
83
+
84
+ Lost Updates Dirty Reads Non-Repeating Reads Phantom Reads
85
+
86
+ (isolation level)
87
+ 4) SERIALIZABLE SOLVED SOLVED SOLVED SOLVED
88
+
89
+ 3) REPEATABLE READ SOLVED SOLVED SOLVED
90
+
91
+ 2) READ COMMITED SOLVED
92
+
93
+ 1) READ UNCOMMITED
94
+ */
95
+
96
+ SHOW VARIABLES LIKE ' transaction_isolation' ;
97
+
98
+ /* for session only*/
99
+ SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
100
+
101
+ /* for global*/
102
+ SET GLOBAL TRANSACTION ISOLATION LEVEL SERIALIZABLE;
103
+
104
+
105
+
106
+ /* ***********************************************/
107
+ /* DeadLocks */
108
+ /* ***********************************************/
109
+ /*
110
+ A deadlock is a situation in which two or more transactions are waiting for one another to give up locks.
111
+ */
0 commit comments