I have a procedure that is supposed to take an employee from a certain table and insert it into another table that records who is in a leadership role for a certain period.
The thing is, there can only be a maximum of two employees in leadership roles for a given period.
The primary key in the table is the pair (employee,month) and there is no manual insert, so it's all done trough the procedure.
How do I structure it so that concurrent accesses can't register more than 2 employees in a leadership role for the same month?
I thought about getting a COUNT(*) on the number of rows for that month, but if an insert occurs after the count is registered, it would no longer be valid. Is there any way to control this right on the procedure or do I need to resort a trigger to check after each insert and rollback if the number exceeds 2?
I cannot change the structure of the table, just the procedure.
Best Answer
Ok, the trigger is trivial I think, for
DBMS_LOCK
, just to give you the idea:With the above, concurrent calls of
dbms_lock.request
will be blocked until the previous session releases the lock.