-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransactions.sql
More file actions
131 lines (94 loc) · 4.64 KB
/
transactions.sql
File metadata and controls
131 lines (94 loc) · 4.64 KB
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
-- Agora Transaction Management
-- Demonstrates COMMIT, ROLLBACK, SAVEPOINT, and isolation level changes
USE agora_db;
-- -----------------------------------------------------------------------
-- Transaction 1: Post a problem and tag it atomically
-- If either the problem insert or the tag assignment fails,
-- neither change is committed.
-- -----------------------------------------------------------------------
START TRANSACTION;
INSERT INTO Problem (user_id, course_id, title, description, created_at)
VALUES (1, 2, 'What is a covering index?',
'I keep seeing the term covering index in query optimization articles. How does it differ from a regular index?',
NOW());
-- Capture the new problem_id
SET @new_problem_id = LAST_INSERT_ID();
-- Tag it with 'databases' (tag_id = 1) and 'sql' (tag_id = 2)
INSERT INTO ProblemTag (problem_id, tag_id) VALUES (@new_problem_id, 1);
INSERT INTO ProblemTag (problem_id, tag_id) VALUES (@new_problem_id, 2);
COMMIT;
-- -----------------------------------------------------------------------
-- Transaction 2: ROLLBACK demonstration
-- Attempts to update a user's reputation score, then rolls back intentionally
-- to show that uncommitted changes are fully discarded.
-- -----------------------------------------------------------------------
START TRANSACTION;
-- Give user 1 a temporary bonus
UPDATE User SET reputation_score = reputation_score + 50 WHERE user_id = 1;
-- Verify the change is visible within this transaction
SELECT user_id, name, reputation_score FROM User WHERE user_id = 1;
-- Decide to undo — perhaps input validation failed
ROLLBACK;
-- The score is now back to its original value
SELECT user_id, name, reputation_score FROM User WHERE user_id = 1;
-- -----------------------------------------------------------------------
-- Transaction 3: SAVEPOINT — Borrow request approval workflow
-- Approving a borrow request involves updating the request status and
-- incrementing the owner's reputation. SAVEPOINT allows partial rollback
-- if the reputation update fails while preserving the status change.
-- -----------------------------------------------------------------------
START TRANSACTION;
-- Step 1: approve the borrow request
UPDATE BorrowRequest
SET status = 'approved', updated_at = NOW()
WHERE request_id = 2;
SAVEPOINT after_status_update;
-- Step 2: reward the resource owner for sharing
UPDATE User
SET reputation_score = reputation_score + 10
WHERE user_id = (
SELECT owner_id FROM BorrowRequest WHERE request_id = 2
);
-- If the reputation update fails, roll back only that step
-- (uncomment to simulate failure):
-- ROLLBACK TO SAVEPOINT after_status_update;
-- Both steps succeeded — commit everything
COMMIT;
-- -----------------------------------------------------------------------
-- Transaction 4: Prevent double enrollment anomaly
-- Without a transaction, two concurrent requests could both pass the
-- duplicate check and both insert, violating the UNIQUE constraint.
-- The transaction plus the UNIQUE constraint on (user_id, course_id)
-- guarantees atomicity.
-- -----------------------------------------------------------------------
START TRANSACTION;
-- Check if the enrollment already exists
SELECT COUNT(*) AS already_enrolled
FROM Enrollment
WHERE user_id = 5 AND course_id = 3;
-- Only insert if the above count is 0
-- In application code this is a conditional; here we show the intent
INSERT INTO Enrollment (user_id, course_id, enrolled_at)
VALUES (5, 3, NOW());
COMMIT;
-- If user 5 is already enrolled in course 3, MySQL raises a duplicate key error
-- and the transaction is rolled back automatically.
-- -----------------------------------------------------------------------
-- Isolation Level Demonstration
-- -----------------------------------------------------------------------
-- Default isolation level in MySQL InnoDB
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
-- READ COMMITTED: allows non-repeatable reads; prevents dirty reads
-- Use when you want fresh reads and can tolerate slightly inconsistent snapshots
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
-- SERIALIZABLE: highest isolation; each transaction sees a fully consistent snapshot
-- Prevents dirty reads, non-repeatable reads, and phantom reads
-- Use for financial or academic integrity operations
SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- READ UNCOMMITTED: lowest isolation; allows dirty reads
-- Rarely used in production; shown here for completeness
SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
-- Verify current isolation level
SELECT @@SESSION.transaction_isolation;
-- Reset to default
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;