I have an Oracle database with a table with a unique field in it:
create table t(n number, CONSTRAINT n_unq UNIQUE (n));
Then from a session-1 someone is trying to insert some data:
insert into t(n) values(1);
-- no commit yet
and in session-2 someone is trying to do the same:
insert into t(n) values(1);
-- hangs here, wating for commit/rollback in session-1
What is the correct way to overcome such pity situations? Session-2 seemes trapped, it may hangs for a vary long time 'being caught' by session-1.
I know the way with sequences, but what if the task is more complicated, say: to write a function which checks if a value exists and return it, and if not exists then INSERT (and here is the trap just inroduced, when running the same code in parallel) and then return it.
-- Ex:
create or replace function f(pn number) return number
is
pret number;
begin
select n into pret from t where n = pn;
return pret;
exception when no_data_found then
insert into t(n) values (pn);
-- hangs here in parallel calls f(<new_value>)
return pn;
end;
n
must be unique, let the server do the job.