Sandeep Mishra, August 26, 2016 - 10:03 am UTC
HI Connor,
Thank you for the clarification.
So what is the triggering point, where a reader need to block another reader?
Does 'Select for update' is a triggering event, to block one more reader?
Thanks and Regards,
Sandeep
August 26, 2016 - 1:31 pm UTC
You only need to block... when the integrity and/or validation of your data needs it.
For example, using the standard DEPT and EMP tables.
If you have a rule "no-one's salary can be more than $100", then a simple check constraint will do.
But if you have a rule "no department can have more than 10 employees", then doing this:
- before insert on EMP do a 'select count(*) from DEPT where deptno = ...'
is *not* good enough, because there may be 4 people doing this insert all at once, and we dont see each other's uncommmitted transactions. So we might all see total of "9", then we all commit, and ... bang...now there are 13 people in a department and we've broken our rule.
One way we could stop that is to do:
- before insert on EMP
- select for update the row in DEPT for that department
- now do my 'select count(*)' for that department, because I know that I have exclusive access
- if I come in less than 10, then I insert and commit (thus allowing the next person to get a lock if they need it)
So blocking is sometimes needed to ensure data is correct. And thats fine. What is often a pain is blocking that occurs just because the database technology you are using has no other mechanisms available. (and Oracle doesn't do that :-))