Transaction, Rollback, Commit

एक transaction कई SQL statements को एक all-or-nothing unit में group करता है: COMMIT हर बदलाव को एक साथ permanent बनाता है, ROLLBACK उन सबको ऐसे undo करता है जैसे कुछ हुआ ही नहीं।

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

दो UPDATEs के बीच power cut

ResultDesk को अपना पहला डरावना काम मिलता है: exam cell ने paper 7 recheck किया, और Riya का DBMS score 78 से 88 जाना चाहिए, जबकि class-total table को match करने के लिए 10 ऊपर जाना चाहिए।

दो UPDATE statements। आप पहला चलाते हैं। Lab की power चली जाती है।

Database अब कहता है Riya के पास 88 है, पर total अभी भी उसके पुराने 78 गिनता है। दोनों numbers असहमत हैं, और किसी को याद नहीं कौन सा आधा चला। इसे एक bank में एक हज़ार daily updates से multiply कीजिए और आप देखेंगे SQL इसे luck पर छोड़ने से क्यों मना करता है।

Theory

Pencil-first rule

एक careful clerk एक paper register पहले pencil में correct करता है: दोनों entries बदलिए, check कीजिए वे match करती हैं, और तभी उन्हें pen से करिए।

अगर बीच में कुछ ग़लत लगे, pencil erase कीजिए: register कभी officially बदला नहीं।

एक transaction pencil phase है। COMMIT pen है। ROLLBACK eraser है। Register (आपका database) सिर्फ़ finished, consistent states दिखाता है।

Theory

तीन keywords, formally

एक transaction SQL operations की एक sequence है जो एक logical unit of work के रूप में execute होती है: या तो यह सब होता है, या इसमें से कुछ नहीं।

  • BEGIN TRANSACTION; unit खोलता है (pencil बाहर)।
  • COMMIT; BEGIN से हर बदलाव को permanent, साथ में बनाता है।
  • ROLLBACK; BEGIN से हर बदलाव undo करता है, जैसे यह कभी चला ही न हो।

All-or-nothing property का एक marks लायक़ नाम है: atomicity, ACID का A (Atomicity, Consistency, Isolation, Durability), और SQLite चारों guarantee करता है।

Practical

Recheck, safely किया गया

BEGIN TRANSACTION;

UPDATE marks
SET score = 88
WHERE roll = 101 AND subject = 'DBMS';

UPDATE class_totals
SET total = total + 10
WHERE subject = 'DBMS';

-- both worked? make them permanent TOGETHER:
COMMIT;

-- had anything gone wrong instead:
-- ROLLBACK;   and the database is exactly as before BEGIN

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Power cut चलाइए

वही दो UPDATEs, BEGIN ... COMMIT में wrapped। Power पहले UPDATE के BAAD मरती है पर COMMIT से PEHLE। Machine restart होने पर और SQLite के college.db दोबारा खोलने पर, Riya का score क्या दिखाता है, और क्यों?

Show the answer

78, पुरानी value। Transaction कभी COMMIT तक नहीं पहुँचा, तो restart पर SQLite incomplete काम को rollback करता है: pencil marks automatically erase हो जाते हैं। Database आख़िरी consistent state दिखाता है, दोनों numbers सहमत। यही atomicity ठीक तब अपना काम करती है जब चीज़ें ग़लत होती हैं, जो पूरा point है: transactions accident से PEHLE ख़रीदा गया insurance हैं।

Quiz

एक clerk BEGIN; UPDATE ...; COMMIT; चलाता है और फिर, एक mistake realize करते हुए, ROLLBACK; चलाता है। Database किस state में है?

  1. UPDATE रहता है: ROLLBACK एक committed transaction undo नहीं कर सकता
  2. UPDATE undo हो जाता है: ROLLBACK हमेशा आख़िरी transaction reverse करता है
  3. Error: COMMIT के बाद ROLLBACK type नहीं किया जा सकता
  4. UPDATE का आधा undo होता है
Show the answer

UPDATE रहता है: ROLLBACK एक committed transaction undo नहीं कर सकता

COMMIT pen है: एक बार लिखा, transaction permanent है (ACID का D, durability)। बाद वाले ROLLBACK के पास cancel करने के लिए कोई खुला transaction नहीं है, तो यह committed change पर कुछ नहीं करता (ज़्यादा से ज़्यादा एक harmless warning)। Option B वह misconception है जिसे पकड़ने के लिए यह सवाल मौजूद है: ROLLBACK सिर्फ़ CURRENT, अभी भी खुले transaction के BEGIN तक पीछे पहुँचता है। एक committed mistake fix करने के लिए एक NEW correcting transaction चाहिए।

Watch out

Auto-commit surprise

बिना BEGIN के, SQLite हर statement को अपने छोटे auto-commit transaction में चलाता है: अकेले statements के लिए ठीक, recheck जैसी जुड़ी जोड़ियों के लिए बेकार।

और उल्टा trap Unit 3 में आता है: Python का sqlite3 आपके changes auto-commit NAHI करता। Python से INSERTs चलाइए, conn.commit() भूलिए, program close कीजिए, और हर row चुपचाप ग़ायब हो जाता है। Students हर साल इसमें असली project data खोते हैं; आपको जल्दी warn कर दिया गया है।

Theory

जहाँ आप पहले से transactions पर भरोसा करते हैं

हर UPI payment एक transaction है: एक account debit कीजिए, दूसरा credit कीजिए, दोनों या कोई नहीं। एक train seat book करना: seat reserve कीजिए, पैसे लीजिए, साथ में। जब इस subject की Python unit एक CSV से marks update करेगी, आप पूरे import को एक transaction में wrap करेंगे, ताकि एक बुरी row आधी class import करने के बजाय पूरे को abort कर दे।

Summary

Key takeaways

  • एक transaction = कई statements जो एक all-or-nothing unit के रूप में execute होते हैं (atomicity)।
  • BEGIN TRANSACTION इसे खोलता है; COMMIT सभी changes permanent बनाता है; ROLLBACK उन सबको undo करता है।
  • COMMIT से पहले एक crash आख़िरी consistent state तक auto-roll back करता है।
  • ROLLBACK एक committed transaction undo नहीं कर सकता: committed = permanent (durability)।
  • ACID: Atomicity, Consistency, Isolation, Durability; SQLite पूरी तरह ACID है।
  • Python के sqlite3 को explicit conn.commit() चाहिए, वह Unit 3 trap यहाँ बोया गया।
  • Memory hook: pencil, pen, eraser।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to SQLite

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Transaction, Rollback, Commit · Database Handling using Python · Gri-Learn