-
Notifications
You must be signed in to change notification settings - Fork 0
/
repetable_reads.py
53 lines (38 loc) · 1.4 KB
/
repetable_reads.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
import time
import random
import multiprocessing
from faker import Faker
from pyodbc import Connection
from db.utils import in_transaction
@in_transaction
def repetable_reads(conn: Connection):
cursor = conn.cursor()
count_one = cursor.execute("SELECT * FROM dbo.student where id = 1").fetchone()
print(f"First name from row with id 1 using transaction 1:", count_one[1])
print("Sleeping for 10 seconds...")
time.sleep(10)
count_two = cursor.execute("SELECT * FROM dbo.student where id = 1").fetchone()
print(f"First name from row with id 1 using transaction 1:", count_two[1])
@in_transaction
def repetable_reads_modifier(conn: Connection):
time.sleep(5)
fake = Faker()
cursor = conn.cursor()
first_name = fake.first_name()
print(first_name)
update_student_query = f"""
UPDATE dbo.student SET first_name = '{first_name}' WHERE id=1;
"""
cursor.execute(update_student_query)
print("Updated firstname for row with id 1 using transaction 2...")
cursor.execute("SELECT * FROM dbo.student where id = 1").fetchone()
if __name__ == "__main__":
# Creating processes
process1 = multiprocessing.Process(target=repetable_reads)
process2 = multiprocessing.Process(target=repetable_reads_modifier)
# Starting processes
process1.start()
process2.start()
# Waiting for processes to complete
process1.join()
process2.join()