Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save shivamka1/d623e9fa709e5a70afa21bbc534791d1 to your computer and use it in GitHub Desktop.

Select an option

Save shivamka1/d623e9fa709e5a70afa21bbc534791d1 to your computer and use it in GitHub Desktop.
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.