-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXulyDongThoi.sql
171 lines (149 loc) · 4.74 KB
/
XulyDongThoi.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
--lost update
-- T1
START TRANSACTION ;
select @qty := products.quanity from products WHERE products.id = 2 limit 1;
DO sleep(5);
UPDATE products SET products.quanity = @qty - 2 where products.id = 2;
COMMIT;
-- T2
START TRANSACTION ;
select @qty := products.quanity from products WHERE products.id = 2 limit 1;
DO sleep(5);
UPDATE products SET products.quanity = @qty - 2 where products.id = 2;
COMMIT;
--Giải quyêt
--T1
START TRANSACTION ;
select @qty := products.quanity from products WHERE products.id = 2 limit 1 for Update ;
DO sleep(5);
UPDATE products SET products.quanity = @qty - 2 where products.id = 2;
COMMIT;
--T2
START TRANSACTION ;
UPDATE products SET products.quanity = products.quanity - 5 where products.id = 2 ;
COMMIT ;
---DIRTY READ----
--T1
START TRANSACTION;
select * from products WHERE 1 ;
--T2
SET GLOBAL TRANSACTION ISOLATION LEVEL UNCOMMITTED ;
START TRANSACTION;
CALL Sp_ThemSanPham ( 'nh0007', 'Khay son lì Mira Hydro Shine nh0007 ', 'nh0007-khay-sson-li-mira-22064', 'null khong co',
96750, 129000, 0,
'Không có !!!',
225,
1,
1,
0,
1,
'products/June2018/Ba0jgp3eeSwQ7wuxWlA2.png',
15 , 1 );
DO sleep(8);
ROLLBACK;
--Giai Quyet
--T1
SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED ;
START TRANSACTION;
select * from products WHERE 1 ;
--T2
START TRANSACTION;
CALL Sp_ThemSanPham ( 'nh0007', 'Khay son lì Mira Hydro Shine nh0007 ', 'nh0007-khay-sson-li-mira-22064', 'null khong co',
96750, 129000, 0,
'Không có !!!',
225,
1,
1,
0,
1,
'products/June2018/Ba0jgp3eeSwQ7wuxWlA2.png',
15 , 1 );
DO sleep(10);
ROLLBACK;
----------Unrepeatable data--------------
--T1
START TRANSACTION ;
SELECT SUM(customer.point) as sum from customer WHERE customer.member = 1 group by customer.member ; -- customer.user_id = 22
DO sleep(8);
SELECT sum(customer.point) as sum from customer WHERE customer.member = 1 group by customer.member ;
COMMIT ;
--T2
START TRANSACTION ;
CALL Sp_ThemHoaDon ( 22 , 'Nguyen hoang hiep' ,50000,'nh003' ,165000, 'ATM' , 'Đang chờ' , 'null' );
COMMIT ;
--Giai Quyet
--T1
SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ ;
START TRANSACTION ;
SELECT SUM(customer.point) as sum from customer WHERE customer.member = 1 group by customer.member ;
DO sleep(8);
SELECT sum(customer.point) as sum from customer WHERE customer.member = 1 group by customer.member ;
COMMIT ;
--T2
START TRANSACTION ;
CALL Sp_ThemHoaDon ( 22 , 'Nguyen hoang hiep' ,50000,'nh003' ,165000, 'ATM' , 'Đang chờ' , 'null' );
COMMIT ;
--------------PhanTom---------------------
--T1
START TRANSACTION ;
SELECT orders.id , orders.billing_name_on_card , orders.billing_total from orders WHERE orders.created_at = CURDATE() ;
DO sleep(8) ;
select @toangdanhthu := (SELECT SUM(orders.billing_total) from orders WHERE orders.created_at = CURDATE() ) ;
COMMIT ;
--T2
START TRANSACTION ;
CALL Sp_ThemHoaDon ( 22 , 'Nguyen hoang hiep' ,50000,'nh003' ,165000, 'ATM' , 'Đang chờ' , 'null' );
COMMIT;
--Giai Quyet
--T1
SET GLOBAL TRANSACTION ISOLATION LEVEL SERIALIZABLE ;
START TRANSACTION ;
SELECT orders.id , orders.billing_name_on_card , orders.billing_total from orders WHERE orders.created_at = CURDATE() ;
DO sleep(8) ;
select @toangdanhthu := (SELECT SUM(orders.billing_total) from orders WHERE orders.created_at = CURDATE() ) ;
COMMIT ;
--T2
START TRANSACTION ;
CALL Sp_ThemHoaDon ( 22 , 'Nguyen hoang hiep' ,50000,'nh003' ,165000, 'ATM' , 'Đang chờ' , 'null' );
COMMIT;
----------------DEADLOCK-------------------
--T1
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_XoaSanPham`$$
CREATE PROCEDURE Sp_XoaSanPham ( product_code nvarchar(191) )
BEGIN
DECLARE sl int ;
-- start transaction ;
SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE ;
START TRANSACTION;
SET sl = ( SELECT products.quanity FROM db_admin_final.products where products.code_product = product_code);
do sleep(5);
IF (sl = 0 ) then
DELETE FROM products WHERE products.code_product = product_code ;
ELSE
ROLLBACK ;
END IF ;
commit;
END; $$
DELIMITER ;
CALL Sp_XoaSanPham ('GK0540') ;
--T2
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_XoaSanPham`$$
CREATE PROCEDURE Sp_XoaSanPham ( product_code nvarchar(191) )
BEGIN
DECLARE sl int ;
-- start transaction ;
SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE ;
START TRANSACTION;
do sleep(5);
SET sl = ( SELECT products.quanity FROM db_admin_final.products where products.code_product = product_code);
IF (sl = 0 ) then
DELETE FROM products WHERE products.code_product = product_code ;
ELSE
ROLLBACK ;
END IF ;
commit;
END; $$
DELIMITER ;
CALL Sp_XoaSanPham ('GK0540') ;