I have a plsql procedure used to insert/update records to a table and this procedure can be called from more than 1 place at a given moment from parallel execution threads.
This method checks whether a record exists in the table using the keys and updates it or inserts a new one.
We have noticed that this procedure sometimes gives us issues and after some investigation, we noticed that the procedure gets called from 2 places at the same time (same millisecond) trying to insert the same record. So the Check_Exist will return FALSE to both executions and then when both executions try to insert the same record where one fails with an Error and one completes successfully.
PROCEDURE Insert_Aaa(
rec_ IN Aaa_TAB%ROWTYPE)
IS
--some code
BEGIN
--some code
IF NOT Check_Exist___(rec_.a,
rec_.b,
rec_.c) THEN
Insert___(rec_);
ELSE
Update___(rec_);
END IF;
END Insert_Aaa;
I thought of using dbms_session.sleep() with a random value in seconds, but it will add around 5-10 mins additional time to this execution as this procedure will be hit from around 120 places. So that is not a good solution for me.