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

Transaction Isolation Levels

Isolation levels इस बारे में rules जैसे हैं कि आप किसी classmate के काम के अभी भी लिखे जाने के दौरान उसमें कितना झाँक सकते हैं।
Syntax
sql
SET TRANSACTION ISOLATION LEVEL
  READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE;

Isolation Levels को समझना

Isolation levels यह decide करते हैं कि एक transaction के in-progress changes उसी समय चल रही दूसरी transactions को कितने visible हैं, concurrent access के तहत data consistency और performance के बीच tradeoff control करते हुए।

उदाहरण: Understanding Isolation Levels

sql
SELECT @@transaction_isolation;

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

READ UNCOMMITTED (Dirty Reads)

READ UNCOMMITTED सबसे loose isolation level है, एक transaction को किसी दूसरी transaction के changes commit होने से पहले ही देखने देते हुए — एक so-called dirty read जो ऐसा data expose कर सकता है जो बाद में roll back हो जाए।

उदाहरण: READ UNCOMMITTED (Dirty Reads)

sql
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
START TRANSACTION;
SELECT * FROM accounts;
COMMIT;

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

READ COMMITTED (Non-Repeatable Reads)

READ COMMITTED guarantee करता है कि आप कभी किसी दूसरी transaction का uncommitted data नहीं देखेंगे, लेकिन एक transaction के अंदर same query दो बार चलाना अभी भी अलग results दे सकता है अगर बीच में कोई दूसरी transaction commit हो जाए।

उदाहरण: READ COMMITTED (Non-Repeatable Reads)

sql
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
START TRANSACTION;
SELECT * FROM accounts;
COMMIT;

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

REPEATABLE READ (MySQL में Default)

REPEATABLE READ MySQL का default isolation level है, और यह guarantee करता है कि अगर आप एक transaction के अंदर same row दो बार पढ़ें, आपको दोनों बार identical values मिलेंगी, भले ही कोई दूसरी transaction बीच में उस row को बदल दे।

उदाहरण: REPEATABLE READ (Default in MySQL)

sql
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;
SELECT balance FROM accounts WHERE id = 1;
SELECT balance FROM accounts WHERE id = 1;
COMMIT;

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

SERIALIZABLE (Highest Isolation)

SERIALIZABLE available सबसे strictest level है, effectively concurrent transactions को एक साथ के बजाय एक के बाद एक चलने जैसा behave करवाते हुए — यह concurrency anomalies को पूरी तरह खत्म कर देता है लेकिन एक busy system को noticeably धीमा कर सकता है।

उदाहरण: SERIALIZABLE (Highest Isolation)

sql
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
START TRANSACTION;
SELECT * FROM accounts;
COMMIT;

⚠️ 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. यह मान लेना कि MySQL का default READ COMMITTED है, जबकि InnoDB का default REPEATABLE READ है।
  2. एक चल रही transaction के अंदर SET TRANSACTION ISOLATION LEVEL इस्तेमाल करना, या इसके रहने की उम्मीद करना, जबकि यह सिर्फ अगली transaction पर apply होता है।
  3. real code में READ UNCOMMITTED इस्तेमाल करना और ऐसा data पढ़ना जो बाद में roll back हो सकता है।
चैप्टर सारांश
  • एक transaction statements को group करता है ताकि वे साथ succeed या fail हों।
  • COMMIT changes save करता है, और ROLLBACK उन्हें undo करता है।
  • SAVEPOINT एक partial rollback point mark करता है, और isolation levels control करते हैं कि transactions एक-दूसरे के changes कैसे देखती हैं।
🔒

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.