I have a web jsp app and a database. I would like to know how to properly handle sequences and inserts. For example, I have a table of bookings, with columns FLIGHT_ID, PASSPORT_ID
. This pair is unique. Now, when I want to insert a new booking from data I received on the website, this is how I do it:
- Parse incoming data
- Get new
BOOKING_ID
value from a sequence (SELECT SEQ.nextval FROM dual;
) - Create an
INSERT ...(new_booking_id,...)
query - Look for exceptions thrown by Oracle DBMS (most often unique constraint violation)
This works fine, but I "lose" some of the values of the sequence, because I receive them when calling select, but I don't use them.
What might work is to make the sequence go one step back everytime I catch an exception:
ALTER SEQUENCE SEQ INCREMENT BY -1;
SELECT SEQ.NEXTVAL FROM DUAL;
ALTER SEQUENCE SEQ INCREMENT BY 1;
But this doesn't seem like the best way to go. What is?