-
Notifications
You must be signed in to change notification settings - Fork 2
/
cs623_acid_properties.py
68 lines (52 loc) · 2.46 KB
/
cs623_acid_properties.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
# importing packages
import mysql.connector
import pyodbc
def implementACIDtransaction(sql_query):
try:
# establishing connection to MySQL
conn = mysql.connector.connect(host='localhost',
database='TEST',
user='root',
password='password')
##############################
# IMPLEMENTING ACID PROPERTIES
##############################
# turning off AutoCommit to implement Atomicity property
conn.autocommit = False
# setting isolation level to Serizalizable to implement Isolation property
conn.start_transaction(isolation_level='SERIALIZABLE')
# establishing a cursor instance
cursor = conn.cursor()
# executing our SQL query in MySQL
cursor.execute(sql_query)
print("Transaction has been succcesfully implemented!")
# committing our changes after the transaction successfully executes
conn.commit()
except mysql.connector.Error as error:
# ACID property was not successfully implemented
# need to prevent commit to database to comply with Atomicity property
print("Transaction failed with the following error! {}".format(error))
print("Transaction has been aborted!")
# doing rollback to database state before transaction
conn.rollback()
finally:
# securely closing database cursor and connection
if conn.is_connected():
cursor.close()
print("Database cursor closed successfully!")
conn.close()
print("Database connection closed successfully!")
#################################################
# TESTING THE ACID IMPLEMENTATION
#################################################
# SQL queries for repopulating tableS in case data needed again
repopulate_Product = "INSERT INTO Product VALUES ('p1','tape',2.5)"
repopulate_Stock = "INSERT INTO Stock VALUES ('p1','d1',1000),('p1','d2',-100),('p1','d4',1200)"
# SQL queries for deleting 'p1' from tables
delete_from_Product = "DELETE FROM Product WHERE ProdID = 'p1'"
delete_from_Stock = "DELETE FROM Stock WHERE ProdID = 'p1'"
# using function to ensure ACID properties implemented on these SQL queries
# implementACIDtransaction(repopulate_Product)
# implementACIDtransaction(repopulate_Stock)
implementACIDtransaction(delete_from_Product)
implementACIDtransaction(delete_from_Stock)