Tuesday, August 6, 2013

DBMS Practical (Transaction)

DBMS မွာ Transaction  ပိုင္းကို Lab လုပ္တာ ဒီႏွစ္မွ စလုပ္တာပါ။ အဲဒါေႀကာင့္ Lab test မွာေမးမယ္ဆို ေမးလို ့ရတာေပါ့ေနာ္..သိပ္ေတာ့မေသခ်ာဘူး။ ဘာပဲျဖစ္ျဖစ္ ေမးလာရင္ ေျဖလို ့ရေအာင္ ႀကိဳတင္ျပင္ဆင္ထားသင့္တာေပါ့။ :D
Transaction ပိုင္းက နဲနဲ ရွုပ္တာကလြဲရင္ လြယ္လြယ္ေလးပါ။ Transaction မွာ Commit နဲ့ RollBack ဆိုျပီး ႏွစ္မ်ိဳးရွိတယ္... (ေအာက္မွာ ဥပမာေတြနဲ့ နားလည္ေအာင္ ရွင္းျပထားပါတယ္)
သေဘာေျပာရရင္... Database တစ္ခုကို User ႏွစ္ေယာက္သံုးတယ္...။ User 1 က table တစ္ခုကို Create လုပ္ထားမယ္။ User 2 ကလဲ အဲဒီ Database ကို သံုးေနတယ္။ အဲဒီအခ်ိန္မွာ Transaction စတယ္။ User 1 က table ထဲက Data တစ္ခုကို Update လုပ္လိုက္တယ္။
တစ္ခ်ိန္ထဲမွာပဲ User 2 က အဲဒီ Table ကို Select * လုပ္ျပီး ႀကည့္မယ္။ User 1 က Commit/ Roll back တစ္ခုခု မလုပ္ေသးတဲ့အတြက္ User 2 ကႀကည့္ရင္ နဂို Table အတိုင္းပဲ ျမင္ရမွာပါ။ Update လုပ္လိုက္တာကိုေတာ့ User 2 က ျမင္ရမွာမဟုတ္ပါဘူး။ Commit/Roll Back တစ္ခုခု လုပ္လိုက္ရင္ေတာ့  User 2 ဘက္မွာပါ Update လုပ္ျပီး Data ေတြကို ေတြ ့ရမွာပါ။
ဥပမာေလးတစ္ခုနဲ့ ႀကည့္တာေပါ့... (ဒီ Sample program က အခန္းထဲမွာ ဆရာမေပးသြားတာပါ)
ပထမဆံုး Database တစ္ခုေဆာက္လိုက္မယ္။

mysql> Create Database TRANSACTION;

ျပီးရင္ေတာ့ Database ကိုသံုးမယ္လို ့ command တစ္ခုေပးမယ္

mysql> use TRANSACTION;

Table တစ္ခုေဆာက္မယ္...

mysql> Create Table account(id integer primary key, balance integer);

Table ထဲကို Data ထည့္မယ္။

mysql> insert into account values (1001,1000),(1002,2000);

Data ေတြျပန္ႀကည့္ ႀကည့္မယ္..။

ဒါေတြကို User 1 မွာ လုပ္တယ္လို ့ထားလိုက္.. ေနာက္ထပ္ Mysql Command Prompt တစ္ခုဖြင့္မယ္။ ထံုးစံအတိုင္း root နဲ့ ၀င္လိုက္ :D ... အဲဒီ ေနာက္ထပ္ဖြင့္လိုက္တာက User 2 ေပါ့ေနာ္။ အဲဒီ User 2 ကေန TRANSACTION Database ကို use လုပ္လိုက္။

mysql> Use TRANSACTION;

အဲဒီကေနပဲ... Select * လုပ္ျပီး Table ထဲက Data ေတြကို ႀကည့္ႀကည့္။

mysql> select * from account;

ျမင္ရတယ္ေနာ္... အရင္အတိုင္းပဲ... Data ေတြဘာမွမေျပာင္းဘူးေနာ္.... ပံုနဲ့မျပေတာ့ဘူး တူတူပဲမလို့။


အခု User 1 ကို ျပန္သြားမယ္ (ပထမ Mysql Command Prompt)
Transaction method ကိုသံုးဖို ့အတြက္ အရင္ဆံုး begin နဲ့ ေခၚလိုက္မယ္။

mysql> begin;

ဘာမွေထြေထြထူးထူးမရွိဘူးေနာ္... ပံုမွန္အတိုင္း  Query Ok ..... ပဲေပၚမွာ။
အခု Update လုပ္ေတာ့မယ္... User 1 ကေနေနာ္... မရွုပ္သြားနဲ့ဦး။ ;)

mysql> update account set balance = balance - 200 where id = 1001;

balance ထဲကေန 200 ကို ႏွုတ္လိုက္တာ... အဲေတာ့ balance ထဲမွာ 200 ေလ်ာ့သြားျပီေနာ္...
ဟုတ္မဟုတ္ျပန္ႀကည့္မယ္။။


Ok ေနာ္... 1001 ထဲက balance မွာ 800 ပဲရွိေတာ့တယ္....
User 2 (ဒုတိယ Mysql Command Prompt) ကို သြားလိုက္...
User 2 ကေနလဲ select * လုပ္ျပီး Data ေတြႀကည့္ႀကည့္လိုက္ ေလ်ာ့သြားလားလို ့


မေလ်ာ့ဘူးေနာ္... User 2 ဘက္မွာက Data ေတြကို Update မလုပ္ခင္က အတိုင္းပဲ ျမင္ရမယ္။ ဘာလို့လဲ ဆိုေတာ့ Commit (or) Roll Back မလုပ္ရေသးလို ့။  User 1 ဘက္ကေန Commit လုပ္ႀကည့္လိုက္မယ္။

mysql> commit;

ျပီးရင္ Select * နဲ့ ျပန္ႀကည့္ႀကည့္မယ္ (User 1 ဘက္ကေနအရင္ႀကည့္ေနာ္)

mysql> select * from account;



 Date ေတြ Update ျဖစ္သြားတယ္ေနာ္... ေကာင္းျပီ.. User 2 ကေနလဲ အဲလို ႀကည့္ႀကည့္ပါဦး။

mysql> select * from account;


ေတြ ့ျပီေနာ္... User 2 ဘက္က Data ေတြပါ ခ်ိန္းသြားတယ္...။ User 2 ဘက္က Data ဆိုေပမယ့္ တကယ္က Database တစ္ခုထဲကုိ User ႏွစ္ေယာက္ ျပိဳင္သံုးတာပါ။
အခုလုပ္သြားတာက Transaction က Commit နဲ့ လုပ္ျပသြားတာပါ။
ေနာက္တစ္မ်ိဳး roll back နဲ့လုပ္ႀကည့္တာေပါ့။ Roll back ကိုေတာ့ User 2 ဘက္ကေန လုပ္ႀကည့္မယ္။ ပထမဆံုး User 2 ကေန begin ဆိုျပီး စလုပ္မယ္။

mysql>begin;

Table ထဲက id= 1002  ရဲ ့balance ကို update လုပ္မယ္။

mysql> update account set balance= balance+500 where id=1002;

table ကိုျပန္ႀကည့္ ႀကည့္မယ္။

mysql> select * from account;

ေတြ ့တယ္ေနာ္... id 1002 ရဲ ့ balance တန္ဖိုးက 2500 ျဖစ္သြားပီ။ user2 ကိုခဏထား။
user 1 ကေနျပန္ႀကည့္ႀကည့္မယ္။

mysql> select * from account;




သူ ့မွာေတာ့ balance တန္ဖိုးမေျပာင္းဘူး။ ဘာလို့လဲ ဆိုေတာ့ Transaction method အတိုင္း commit/ roll back မလုပ္ေသးလို ့ တစ္ဘက္ user မွာ update မလုပ္ေသးဘူး။ အခု Roll back လုပ္ႀကည့္မယ္။ ဘယ္မွာလုပ္ရမလဲဆိုေတာ့  user 2 မွာလုပ္ရမွာေပါ့။

mysql>rollback;

roll back လုပ္ျပီးလို ့ မွန္သြားျပီဆိုတဲ့ Query Ok ဆိုတာေပၚလာျပီဆိုရင္ ကြ်န္ေတာ္တို့ Table ကို ျပန္ႀကည့္မယ္။ user 2 ကေန အရင္ႀကည့္မယ္။

mysql> select * from account;
 

ေတြ ့ပါတယ္ေနာ္... Table ထဲက id 1002 ရဲ ့တန္ဖိုးက နဂိုတန္ဖိုးအတိုင္းပဲ ျပန္ျဖစ္သြားျပီ။ Update လုပ္တာကို Cancel လုပ္လိုက္တဲ့ သေဘာေပါ့။ (commit ဆိုတာကေတာ့ Update လုပ္တာကို Save လုပ္လိုက္တဲ့ သေဘာပါပဲ)
user 1 ကေန ျပန္ႀကည့္ႀကည့္ပါဦး။

mysql> select * from account;


သူလဲ ဒီတိုင္းပါပဲ... တန္ဖိုးေတြ မေျပာင္းလဲဘဲ ထြက္လာတယ္။
ဒါပါပဲ Transaction ရဲ ့သေဘာကို နားလည္မယ္ထင္ပါတယ္။

No comments:

Post a Comment