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