SlideShare a Scribd company logo
1 of 8
A Green Solution
that fixed a real-life Race Condition problem
by Kai Zhou
1
Problem
Production system now lacks functionality to provide concurrent customers
assuredly different set of numbers for their number selection.
So there might be confliction that if two or more customers both have chosen
number N within a same time span which is considerably short, any customer
might fail to order the number N because it may have been assigned to
another customer a moment ago.
Requirement
Improve “Get Available Number List” service module, make it return different
number-lists when it is called concurrently so that any certain number will not
be presented in more than one returned number-lists within a short time
span which can be defined as lock period.
2
Solution
The solution uses RDBMS’ inherent features such as
• Row Level Lock;
• Primary Key constraint
to resolve the problem described above.
An Oracle PL/SQL implementation is presented in later slides. The same logic
can also be written in other languages such as Pro*c, Java etc with the SQL.
And the final delivery can be a C library or a Web Services etc., depends on
the tech circumstances where the solution is implemented.
3
this slide unless you have read the script from following slides and want
to have a test
TEST
Take the following steps:
1) Create table available_phone_number_pool ( phone_number numeric(10) primary
key ) ; then insert 1000 records into it.
---- This table will mock the existing table in the production system that contains
available phone numbers.
2) Create table phone_number_selection_history ( phone_number numeric(10)
primary key, last_lock_time datetime)
---- This is the new table introduced to the production to help with the solution.
3) Use SQL*Plus to run the script concurrently multiple times from different terminal and
observe the screen output of their execution.
4
/* The Script -- non-conflict number selection */
set serveroutput on size 1000000
var v_c number
declare
cursor c_get_number_selection_list is
select num_pool.phone_number, num_select.last_lock_time
from available_phone_number_pool num_pool,
phone_number_selection_history num_select
where num_pool.phone_number=num_select.phone_number (+)
and (
num_select.last_lock_time is null
or
num_select.last_lock_time < sysdate - 10/(60*24)
);
v_rec c_get_number_selection_list%rowtype;
v_count int;
5
/* To be continued */
Implementation
begin
dbms_output.put_line('hellow world!');
v_count:=0;
open c_get_number_selection_list;
loop
<<next>>
fetch c_get_number_selection_list into v_rec;
if c_get_number_selection_list%notfound then
goto out;
end if;
if v_rec.last_lock_time is null then
begin
insert into phone_number_selection_history
values ( v_rec.phone_number, sysdate );
exception
when DUP_VAL_ON_INDEX then
dbms_output.put_line(‘Number ‘ ||v_rec.phone_number ||‘ is skipped ');
goto next;
end;
dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’);
commit;
v_count:=v_count+1;
/* to be continued */ 6
else
begin
update phone_number_selection_history
set last_lock_time= sysdate
where phone_number= v_rec.phone_number
and last_lock_time < sysdate - 10/(60*24) ;
exception
when NO_DATA_FOUND then
dbms_output.put_line (‘Number '|| v_rec.phone_number || ‘ is skipped’);
goto next;
end;
dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’);
commit;
v_count:=v_count+1;
end if;
if v_count=10 then
goto out;
end if;
end loop;
<<out>>
end;
7
Why it is GREEN ?
A costly solution may request a new column to be added to the
existing production DB table ( which is obviously bad idea), or it
may generate temporary DB data which keeps growing thus a
clean-up job must be added to production’s operation schedule,
or it may hire too complicated techs to resolve concurrency
problem … resulted stability will be questioned.
However with our green solution, apart from the necessary
Service Module change (which implements the logic described
above ), only one new DB table is added to the production, and it
does NOT need initialization or cleanup hence causing no extra
maintenance cost. More importantly, the resulted stability is
predicted as the logic is simple and natural.
8

More Related Content

What's hot

Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Thuan Nguyen
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSmohdoracle
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Thuan Nguyen
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handlingSmitha Padmanabhan
 

What's hot (20)

Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02Oracle - Program with PL/SQL - Lession 02
Oracle - Program with PL/SQL - Lession 02
 
Oracle sql tuning
Oracle sql tuningOracle sql tuning
Oracle sql tuning
 
Procedure n functions
Procedure n functionsProcedure n functions
Procedure n functions
 
PLSQL
PLSQLPLSQL
PLSQL
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
PL/SQL
PL/SQLPL/SQL
PL/SQL
 
Oracle query optimizer
Oracle query optimizerOracle query optimizer
Oracle query optimizer
 
PLSQL Tutorial
PLSQL TutorialPLSQL Tutorial
PLSQL Tutorial
 
Pl sql guide
Pl sql guidePl sql guide
Pl sql guide
 
Function and types
Function  and typesFunction  and types
Function and types
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Packages - PL/SQL
Packages - PL/SQLPackages - PL/SQL
Packages - PL/SQL
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05Oracle - Program with PL/SQL - Lession 05
Oracle - Program with PL/SQL - Lession 05
 
ORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERSORACLE PL SQL FOR BEGINNERS
ORACLE PL SQL FOR BEGINNERS
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
 

Similar to Green solution fixes race condition using row locks

Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 solIIUM
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.comHarrisGeorg12
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comStephenson22
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comWilliamsTaylorza48
 
Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Abhishek Bose
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdfVcTrn1
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardMax Kleiner
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowAlex Zaballa
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newLast7693
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newscottbrownnn
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab neweyavagal
 
M150 A Fall2010 T01
M150 A Fall2010 T01M150 A Fall2010 T01
M150 A Fall2010 T01abdalodainat
 
Md university cmis 102 week 5 hands
Md university cmis 102 week 5 handsMd university cmis 102 week 5 hands
Md university cmis 102 week 5 handseyavagal
 

Similar to Green solution fixes race condition using row locks (20)

Mid term sem 2 1415 sol
Mid term sem 2 1415 solMid term sem 2 1415 sol
Mid term sem 2 1415 sol
 
Cmis 102 Effective Communication / snaptutorial.com
Cmis 102  Effective Communication / snaptutorial.comCmis 102  Effective Communication / snaptutorial.com
Cmis 102 Effective Communication / snaptutorial.com
 
Cmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.comCmis 102 Enthusiastic Study / snaptutorial.com
Cmis 102 Enthusiastic Study / snaptutorial.com
 
Cmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.comCmis 102 Success Begins / snaptutorial.com
Cmis 102 Success Begins / snaptutorial.com
 
Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)Lab3 testbench tutorial (1)
Lab3 testbench tutorial (1)
 
cscript_controller.pdf
cscript_controller.pdfcscript_controller.pdf
cscript_controller.pdf
 
maXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO StandardmaXbox Starter 43 Work with Code Metrics ISO Standard
maXbox Starter 43 Work with Code Metrics ISO Standard
 
DBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should KnowDBA Commands and Concepts That Every Developer Should Know
DBA Commands and Concepts That Every Developer Should Know
 
Oopp Lab Work
Oopp Lab WorkOopp Lab Work
Oopp Lab Work
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Md university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab newMd university cmis 102 week 4 hands on lab new
Md university cmis 102 week 4 hands on lab new
 
Express 070 536
Express 070 536Express 070 536
Express 070 536
 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
 
M150 A Fall2010 T01
M150 A Fall2010 T01M150 A Fall2010 T01
M150 A Fall2010 T01
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Matopt
MatoptMatopt
Matopt
 
Md university cmis 102 week 5 hands
Md university cmis 102 week 5 handsMd university cmis 102 week 5 hands
Md university cmis 102 week 5 hands
 
overview of c#
overview of c#overview of c#
overview of c#
 
Assignment 2
Assignment 2Assignment 2
Assignment 2
 

Recently uploaded

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Green solution fixes race condition using row locks

  • 1. A Green Solution that fixed a real-life Race Condition problem by Kai Zhou 1
  • 2. Problem Production system now lacks functionality to provide concurrent customers assuredly different set of numbers for their number selection. So there might be confliction that if two or more customers both have chosen number N within a same time span which is considerably short, any customer might fail to order the number N because it may have been assigned to another customer a moment ago. Requirement Improve “Get Available Number List” service module, make it return different number-lists when it is called concurrently so that any certain number will not be presented in more than one returned number-lists within a short time span which can be defined as lock period. 2
  • 3. Solution The solution uses RDBMS’ inherent features such as • Row Level Lock; • Primary Key constraint to resolve the problem described above. An Oracle PL/SQL implementation is presented in later slides. The same logic can also be written in other languages such as Pro*c, Java etc with the SQL. And the final delivery can be a C library or a Web Services etc., depends on the tech circumstances where the solution is implemented. 3
  • 4. this slide unless you have read the script from following slides and want to have a test TEST Take the following steps: 1) Create table available_phone_number_pool ( phone_number numeric(10) primary key ) ; then insert 1000 records into it. ---- This table will mock the existing table in the production system that contains available phone numbers. 2) Create table phone_number_selection_history ( phone_number numeric(10) primary key, last_lock_time datetime) ---- This is the new table introduced to the production to help with the solution. 3) Use SQL*Plus to run the script concurrently multiple times from different terminal and observe the screen output of their execution. 4
  • 5. /* The Script -- non-conflict number selection */ set serveroutput on size 1000000 var v_c number declare cursor c_get_number_selection_list is select num_pool.phone_number, num_select.last_lock_time from available_phone_number_pool num_pool, phone_number_selection_history num_select where num_pool.phone_number=num_select.phone_number (+) and ( num_select.last_lock_time is null or num_select.last_lock_time < sysdate - 10/(60*24) ); v_rec c_get_number_selection_list%rowtype; v_count int; 5 /* To be continued */ Implementation
  • 6. begin dbms_output.put_line('hellow world!'); v_count:=0; open c_get_number_selection_list; loop <<next>> fetch c_get_number_selection_list into v_rec; if c_get_number_selection_list%notfound then goto out; end if; if v_rec.last_lock_time is null then begin insert into phone_number_selection_history values ( v_rec.phone_number, sysdate ); exception when DUP_VAL_ON_INDEX then dbms_output.put_line(‘Number ‘ ||v_rec.phone_number ||‘ is skipped '); goto next; end; dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’); commit; v_count:=v_count+1; /* to be continued */ 6
  • 7. else begin update phone_number_selection_history set last_lock_time= sysdate where phone_number= v_rec.phone_number and last_lock_time < sysdate - 10/(60*24) ; exception when NO_DATA_FOUND then dbms_output.put_line (‘Number '|| v_rec.phone_number || ‘ is skipped’); goto next; end; dbms_output.put_line(‘Number '|| v_rec.phone_number || ‘ is available’); commit; v_count:=v_count+1; end if; if v_count=10 then goto out; end if; end loop; <<out>> end; 7
  • 8. Why it is GREEN ? A costly solution may request a new column to be added to the existing production DB table ( which is obviously bad idea), or it may generate temporary DB data which keeps growing thus a clean-up job must be added to production’s operation schedule, or it may hire too complicated techs to resolve concurrency problem … resulted stability will be questioned. However with our green solution, apart from the necessary Service Module change (which implements the logic described above ), only one new DB table is added to the production, and it does NOT need initialization or cleanup hence causing no extra maintenance cost. More importantly, the resulted stability is predicted as the logic is simple and natural. 8