-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
112 lines (97 loc) · 3.39 KB
/
code.py
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
from threading import Thread
import mysql.connector
import time
def connect(host='localhost', db='zd_shema_tup', user='david', passwd='1234'):
return mysql.connector.connect(host=host, database=db, user=user, password=passwd)
def insert(n, commit, sleep):
db = connect()
cursor = db.cursor()
for i in range(n):
cursor.execute('INSERT INTO zdravstveni_domovi (ime_zd, naslov_zd) VALUES (%s, %s)', ['ZD '+str(i), 'Naslov '+str(i)])
time.sleep(sleep)
if commit == 1:
db.commit()
else:
db.rollback()
def select(table='zdravstveni_domovi'):
db = connect()
cursor = db.cursor()
cursor.execute('SELECT * FROM '+table)
query_result = cursor.fetchall()
for entry in query_result:
print(entry)
def select_twice(table='zdravstveni_domovi'):
db = connect()
cursor = db.cursor()
cursor.execute('SELECT * FROM '+table)
query_result = cursor.fetchall()
for entry in query_result:
print(entry)
time.sleep(4)
cursor.execute('SELECT * FROM '+table)
query_result = cursor.fetchall()
for entry in query_result:
print(entry)
def select_twice_where():
db = connect()
cursor = db.cursor()
cursor.execute('SELECT * FROM zdravstveni_domovi WHERE ime_zd LIKE "ZD%"')
query_result = cursor.fetchall()
for entry in query_result:
print(entry)
time.sleep(4)
cursor.execute('SELECT * FROM zdravstveni_domovi WHERE ime_zd LIKE "ZD%"')
query_result = cursor.fetchall()
for entry in query_result:
print(entry)
def update(id, new_value, commit, sleep):
db = connect()
cursor = db.cursor()
cursor.execute('SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE')
cursor.execute('UPDATE zdravstveni_domovi SET ime_zd = %s WHERE id_zd = %s', [new_value, id])
time.sleep(sleep)
if commit == 1:
db.commit()
else:
db.rollback()
def dirty_read():
db = connect()
cursor = db.cursor()
cursor.execute('SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED')
Thread(target = insert, args=(3, 0, 2.0)).start()
Thread(target = select).start()
time.sleep(5.0)
Thread(target = select).start()
time.sleep(2.0)
cursor.execute('SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ')
def non_rep_read():
db = connect()
cursor = db.cursor()
cursor.execute('SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED')
Thread(target = select_twice).start()
Thread(target = update, args=(1, 'updated', 1, 0)).start()
time.sleep(6.0)
cursor.execute('SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ')
def first():
db = connect()
cursor = db.cursor()
cursor.execute('UPDATE zdravstveni_domovi SET naslov_zd = "prvi" WHERE id_zd = 1')
db.commit()
def second():
db = connect()
cursor = db.cursor()
cursor.execute('UPDATE zdravstveni_domovi SET naslov_zd = "drugi" WHERE id_zd = 1')
db.commit()
def phantom_read():
db = connect()
cursor = db.cursor()
cursor.execute('SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED')
Thread(target = select_twice_where).start()
Thread(target = update, args=(3, 'updated', 1, 0)).start()
time.sleep(6.0)
cursor.execute('SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ')
def lost_update():
Thread(target = first).start()
Thread(target = second).start()
time.sleep(1)
Thread(target = select).start()