MySQL Transactions

Introduction to MySQL Transactions

एक transaction कुछ DML statements का group होता है। इस group के सभी statements एक साथ एक statement की तरह execute किये जाते है। इसका मतलब ये हुआ की यदि इन statements में से एक भी statement का execution fail हो जाये तो पूरा transaction ही fail हो जाता है। एक transaction केवल तब ही successfully execute होता है जब सभी statements successfully execute होते है।

इसका सबसे अच्छा उदाहरण banking system होता है। मान लीजिये आप अपने किसी एक account से ₹1000 दूसरे किसी account में transfer कर रहे है। इस task को complete करने के कई steps हो सकते है जैसे की -

  • सबसे पहले ये check किया जायेगा की आपके account में ₹1000 है या नहीं है।
  • इसके बाद एक account से ₹1000 निकाले जायेंगे। (Main balance - ₹1000)
  • इसके बाद दूसरे account में ₹1000 जमा किये जायेंगे। (Main balance + ₹1000)

इन सभी steps को आप एक MySQL transaction के वो statements मान सकते है जो एक साथ execute होंगे। यदि इन steps में से एक भी step successful नहीं रहता है तो पूरा transaction ही fail हो जाता है।

किसी भी transaction के fail होने से पहले जो भी changes database में किये गए थे उन्हें rollback (database पुरानी situation में आ जाता है।) कर लिया जाता है। इससे database की integrity कायम रहती है। मान लीजिये आपने second step complete कर लिया है और third step को complete करते समय कोई technical error आ गयी तो transaction fail हो जायेगा। ऐसी situation में यदि first और second steps rollback नहीं किये जायेंगे तो database accurate नहीं माना जायेगा।

Transactions के बारे में और अधिक जानने से पहले मै आपको ये बताना चाहता हूँ की transactions केवल उन्हीं storage engines के साथ काम करते है जो transactions को support करते है जैसे की InnoDB storage engine आदि। इन्हें transnational storage engines कहा जाता है।

MySQL transactions की कुछ properties होती है जिनके बारे में निचे दिया जा रहा है।

Properties of MySQL Transactions (ACID)

MySQL transactions की निचे दी गयी 4 properties होती है। इन्हें short में ACID properties भी कहा जाता है।

  • Atomic - ये property सुनिश्चित करती है की सभी statements successfully execute किये जा चुके है। और यदि किसी statement का execution fail होता है तो उससे पूर्व के सभी statements rollback कर लिए जाते है।
  • Consistent - ये property सुनिश्चित करती है की database द्वारा सभी changes properly कर लिए गए है।
  • Isolated - एक transaction किसी दूसरे transaction से independently execute हो सकता है।
  • Durable - ये property सुनिश्चित करती है की जो transactions commit किये गए हो वो यदि system failure हो जाता है तो भी मान्य होंगे।

Transaction Execution Process

कोई भी transaction एक process के through execute होता है। यदि आप इस process को समझ ले तो काफी हद तक आपको MySQL में transactions का यूज़ समझ में आ जायेगा। निचे कुछ steps दिए जा रहे है जो किसी transaction के execute होने की process को दर्शाते है।

  1. सबसे पहले आप किसी transaction को शुरू करते है।
  2. इसके बाद transaction में define किये गए सभी DML statements को execute किया जाता है।
  3. Statements के execution के दौरान ही errors के लिए भी check किया जाता है।
  4. यदि कोई error generate होती है -
    • तो transaction को rollback कर लिया जाता है ।
  5. नहीं तो -
    • transaction को commit कर दिया जाता है।

ये एक सरल सी process है जो सभी transactions execute होने के दौरान follow करते है। Transaction को समझने के लिए इसे समझना आपके लिए महत्वपूर्ण है।

Transaction Control Statements

SQL transactions को control करने के लिए आपको built in statements provide करती है। इन statements को transaction control language भी कहा जाता है।

  1. START TRANSACTION or BEGIN
  2. COMMIT
  3. ROLLBACK
  4. AUTOMATIC

इन सभी statements के बारे में निचे detail से दिया जा रहा है। आइये इनके बारे में जानने का प्रयास करते है।

START TRANSACTION or BEGIN TRANSACTION

यदि आप एक नया transaction शुरू करना चाहते है तो इसके लिए आप START TRANSACTION या BEGIN TRANSACTION में से कोई भी एक statement यूज़ कर सकते है। इसका उदाहरण निचे दिया जा रहा है।

mysql > start transaction;

कोई भी transaction start करने के लिए आप निचे दिया गया statement भी use कर सकते है।

mysql> begin transaction;

नए transactions शुरू करने के लिए ये दोनों ही statements valid माने जाते है।

COMMIT

जब आप transactions के द्वारा perform किये गए changes को permanently save करना चाहते है तो इसके लिए आप COMMIT statement यूज़ करते है। इसका उदाहरण निचे दिया जा रहा है।

mysql > commit;

एक बात आपको हमेशा ध्यान रखनी चाहिए की commit statement के द्वारा commit किये गए statements को rollback नहीं किया जा सकता है ये changes हमेशा के लिए permanent हो जाते है। इसलिए आपको commit statement का प्रयोग सावधानी पूर्वक करना चाहिए।

ROLLBACK

जब आप changes को दुबारा revert करना चाहते है तो आप ROLLBACK statement को यूज़ करते है। ऐसा आप कई कारणों से कर सकते है जैसे की आपके transaction में कोई error आने पर या फिर किसी technical problem की वजह से भी ऐसा किया जा सकता है। इसका उदाहरण निचे दिया जा रहा है।

mysql > rollback;

Naming Transactions

आप चाहे तो हर transaction को एक नाम दे सकते है। Transactions को नाम देने से उनका task identify करने में आसानी होती है। किसी छोटी script में transactions का नाम देना ज्यादा महत्वपूर्ण नहीं होता है। लेकिन यदि आप किसी बड़े project पर काम कर रहे है और उसमे बहुत से transactions आप यूज़ कर रहे है तो ऐसी situation में आपको transactions का नाम जरूर देना चाहिए।

किसी भी transaction को नाम देना बहुत ही आसान है। इसके लिए आप START TRANSACTION statement के आगे ही transaction का नाम define करते है। इसका उदाहरण निचे दिया जा रहा है।

mysql > begin transaction t1;   

ऊपर दिए गए उदाहरण में transaction को t1 नाम दिया गया है।

AUTO-COMMIT Mode

AUTO-COMMIT mode वह mode होता है जिसमें सभी transactions automatically successfully execute होने पर commit हो जाते है। आपके database में by default ये mode enabled रहता है। यानि जब भी आप कोई query successfully execute करते है तो वह change permanent हो जाता है। यदि ये mode enable ना हो तो आपको हर change को permanent करने के लिए बार बार commit statement execute करना पड़ेगा। किसी छोटे project के लिए ये mode enabled ठीक है।

लेकिन यदि किसी बड़े project पर काम किया जा रहा है तो इसे disable कर दिया जाता है। क्योंकि बड़े projects में गलतियाँ होने से बहुत सा नुकसान हो सकता है। इसलिए हर query को सोच समझ कर execute किया जाता है और उसे commit करने से पहले उसके बारे में निर्णय लिया जाता है।

यदि आप AUTO-COMMIT mode को disable करना चाहते है तो इसके लिए आप SET command यूज़ करते है। इसका उदाहरण निचे दिया जा रहा है।

mysql > set autocommit = 0;

यदि आप इस mode को वापस enable करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।

mysql > set autocommit = 1;