← Back to MySQL Course | Chapter 17: Transactions | Lesson 2 of 4

COMMIT और ROLLBACK

COMMIT changes के एक group पर हमेशा के लिए save दबाने जैसा है, और ROLLBACK शुरुआत में वापस 'undo everything' दबाने जैसा है।
Syntax
sql
START TRANSACTION;
-- SQL statements
COMMIT;    -- save the changes
-- or
ROLLBACK;  -- undo the changes

Transactions का Introduction

COMMIT और ROLLBACK एक transaction के खत्म होने के दो तरीके हैं: COMMIT START TRANSACTION के बाद किए गए हर change को permanent की तरह lock कर देता है, जबकि ROLLBACK उन सबको ऐसे फेंक देता है जैसे transaction कभी हुआ ही न हो।

उदाहरण: Introduction to Transactions

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

ROLLBACK से Changes Undo करना

ROLLBACK आपका safety net है जब transaction के बीच में कुछ गलत हो जाए — एक failed validation, एक unexpected error, या एक business rule violation — आपको अब तक किए गए हर change cancel करके data को exactly वैसे छोड़ने देते हुए जैसा वह था।

उदाहरण: Undoing Changes with ROLLBACK

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
-- Validation failed -- cancel everything since START TRANSACTION
ROLLBACK;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

MySQL में Auto-Commit Mode

Autocommit mode बंद करना आपको इस पर पूरा manual control देता है कि data कब permanent बने, जो दो accounts के बीच पैसे transfer करने जैसे multi-step operations के लिए सबसे ज़्यादा मायने रखता है जिन्हें साथ succeed या fail होना ज़रूरी है।

उदाहरण: Auto-Commit Mode in MySQL

sql
SET autocommit = 0;
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Transaction Status Check करना

आप यह confirm करने के लिए current autocommit setting check कर सकते हैं कि MySQL आपके statements तुरंत save कर रहा है या एक explicit COMMIT का wait कर रहा है, जो sensitive multi-step logic चलाने से पहले verify करना उचित है।

उदाहरण: Checking Transaction Status

sql
SELECT @@autocommit AS autocommit_enabled;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Transactions के लिए Best Practices

Transactions को छोटा रखना एक real performance concern है — एक long-running transaction rows या tables पर locks को एक लंबे समय तक hold कर सकता है, दूसरे users को block करते हुए, इसलिए इसे हमेशा COMMIT या ROLLBACK से जल्दी बंद करें।

उदाहरण: Best Practices for Transactions

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
COMMIT;  -- close it promptly, don't hold locks longer than needed

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि COMMIT के बाद ROLLBACK काम करता है, जबकि committed changes permanent हैं।
  2. एक transaction को लंबे समय तक खुला छोड़ना, जो locks hold करता है और दूसरे users को block करता है।
  3. SET autocommit = 0 से autocommit बंद करना और फिर commit करना भूल जाना।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.