Wednesday, 2 October 2013

Oracle blocking repeated inserts

Oracle blocking repeated inserts

Consider a system that has multipile requests for course enrollments
coming in. We need a way to block duplicate enrollments in the system. I
created a trigger as follows, but when I get two requests from different
connections at the same time (ms apart) they are both inserted. What am I
doing wrong
create trigger enrollment_duplicates
before insert
on enrollment
for each row
begin
select count(*) into cnt from enrollment where user = :new.user and
course = :new.course and status = 'Enrolled';
if cnt > 0 then
raise_application_error(-20001, 'User already enrolled in course');
end if;
end;

No comments:

Post a Comment