Created
November 7, 2025 23:48
-
-
Save shivamka1/d623e9fa709e5a70afa21bbc534791d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function purchase(productId): | |
| BEGIN TRANSACTION (ISOLATION = SERIALIZABLE) | |
| stock = query("SELECT stock FROM products WHERE id = ?", productId) | |
| if stock <= 0: | |
| ROLLBACK | |
| return "Out of stock" | |
| exec("UPDATE products SET stock = stock - 1 WHERE id = ?", productId) | |
| COMMIT | |
| return "OK" |
Comments are disabled for this gist.