ट्रांज़ैक्शन आइसोलेशन लेवल्स

SQL Server में Transactions और Error Handling

Miriam Antona

Software Engineer

Concurrency क्या है?

Concurrency: दो या अधिक ट्रांज़ैक्शन जो एक ही समय में साझा डेटा को पढ़ें/बदलें।

अपने ट्रांज़ैक्शन को दूसरों से अलग रखें

SQL Server में Transactions और Error Handling

ट्रांज़ैक्शन आइसोलेशन लेवल्स

  • READ COMMITTED (डिफॉल्ट)
  • READ UNCOMMITTED
  • REPEATABLE READ
  • SERIALIZABLE
  • SNAPSHOT
SET TRANSACTION ISOLATION LEVEL 
    {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE | SNAPSHOT}
SQL Server में Transactions और Error Handling

वर्तमान आइसोलेशन लेवल जानना

SELECT CASE transaction_isolation_level 
    WHEN 0 THEN 'UNSPECIFIED' 
    WHEN 1 THEN 'READ UNCOMMITTED' 
    WHEN 2 THEN 'READ COMMITTED' 
    WHEN 3 THEN 'REPEATABLE READ ' 
    WHEN 4 THEN 'SERIALIZABLE' 
    WHEN 5 THEN 'SNAPSHOT' 
END AS transaction_isolation_level 
FROM sys.dm_exec_sessions 
WHERE session_id = @@SPID
| transaction_isolation_level |
|-----------------------------|
| READ COMMITTED              |
SQL Server में Transactions और Error Handling

READ UNCOMMITTED

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  • सबसे कम प्रतिबंधात्मक आइसोलेशन लेवल
  • ऐसे रो पढ़ें जिन्हें किसी अन्य ट्रांज़ैक्शन ने बदला है, जो अभी कमिट/रोलबैक नहीं हुआ है
SQL Server में Transactions और Error Handling

READ UNCOMMITTED

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  • सबसे कम प्रतिबंधात्मक आइसोलेशन लेवल
  • अन्य ट्रांज़ैक्शन्स द्वारा बदले गए रो पढ़ें, भले ही कमिट/रोलबैक न हुए हों।
Dirty reads Non-repeatable reads Phantom reads
READ UNCOMMITTED yes yes yes
SQL Server में Transactions और Error Handling

डर्टी रीड्स

असली बैलेंस अकाउंट 5 = $35,000

Transaction1

BEGIN TRAN
  UPDATE accounts
  SET current_balance = 30000
  WHERE account_id = 5;

ROLLBACK TRAN;

Transaction2

...

...

...

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT current_balance
FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 30000,00        |
SQL Server में Transactions और Error Handling

नॉन-रीपीटेबल रीड्स

Transaction1

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN TRAN
    SELECT * FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00        |

Transaction2

...

...

BEGIN TRAN
    UPDATE accounts
    SET current_balance = 30000 WHERE account_id = 5;
COMMIT TRAN
SQL Server में Transactions और Error Handling

नॉन-रीपीटेबल रीड्स

Transaction1

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN TRAN
    SELECT * FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 35000,00        |
    SELECT * FROM accounts WHERE account_id = 5;
| current_balance |
|-----------------|
| 30000,00        |

Transaction2

...

...

BEGIN TRAN
    UPDATE accounts
    SET current_balance = 30000 WHERE account_id = 5;
COMMIT TRAN
SQL Server में Transactions और Error Handling

फैंटम रीड्स

Transaction1

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN TRAN
SELECT * FROM accounts
    WHERE current_balance BETWEEN 45000 AND 50000
| account_number       | ... | current_balance |
|----------------------|-----|-----------------|
| 55555555552020202020 | ... | 50000,00        |

Transaction2

...

...

BEGIN TRAN
INSERT INTO accounts
    VALUES ('55555555553939393939', 1, 45000)
COMMIT TRAN
SQL Server में Transactions और Error Handling

फैंटम रीड्स

Transaction1

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
BEGIN TRAN
SELECT * FROM accounts 
    WHERE current_balance BETWEEN 45000 AND 50000
| account_number       | ... | current_balance |
|----------------------|-----|-----------------|
| 55555555552020202020 | ... | 50000,00        | 
SELECT * FROM accounts 
    WHERE current_balance BETWEEN 45000 AND 50000
| account_number       |...| current_balance |
|----------------------|---|-----------------|
| 55555555553939393939 |...| 45000,00        | Phantom!
| 55555555552020202020 |...| 50000,00        |

Transaction2

...

...

BEGIN TRAN
INSERT INTO accounts
    VALUES ('55555555553939393939', 1, 45000)
COMMIT TRAN
SQL Server में Transactions और Error Handling

READ UNCOMMITTED - सारांश

फायदे:

  • तेज हो सकता है, अन्य ट्रांज़ैक्शन्स को ब्लॉक नहीं करता।

नुकसान:

  • डर्टी रीड्स, नॉन-रीपीटेबल रीड्स, और फैंटम रीड्स की अनुमति।

कब उपयोग करें?:

  • आप ब्लॉक नहीं होना चाहते, और इन concurrency समस्याओं से परहेज़ नहीं।
  • आप जानबूझकर अनकमिटेड डेटा देखना चाहते हैं।
SQL Server में Transactions और Error Handling

अभ्यास करते हैं!

SQL Server में Transactions और Error Handling

Preparing Video For Download...