0

I've got a weird situation here. I've used triggers and sequences to implement auto-increment. I insert the data into my tables from my web app which uses Hibernate. I test the web app in my machine (Netbeans) as well as on my office network (the web app is also deployed on our server with Wildfly).

It has always worked fine, until I started getting exceptions due to the unique constraint (Primary key). Then I discovered that the problem was the sequence that generates values for the ids. Example, For my table xtable, its sequence's last_number is 78400, the max id in xtable is 78308, but the sequence's nextval is 78304. I have no idea how that happens because I created the sequence with the following:

   CREATE SEQUENCE  XTABLE_SEQUENCE  INCREMENT BY 1 START WITH 1;

I tried the following to update the sequence and make its NEXTVAL greater than the max(id) in the table, but I'm still getting the same result after n inserts

declare
maxval number(10);
begin
   select max(ID) into maxval from XTABLE;
maxval := maxval+1;
execute immediate 'DROP SEQUENCE XTABLE_SEQUENCE';
execute immediate 'CREATE SEQUENCE XTABLE_SEQUENCE START WITH '|| maxval+50 ||' INCREMENT BY 1'; 
end;

Here is the trigger statement:

create or replace TRIGGER xtable_sequence_tr
BEFORE INSERT ON xtable FOR EACH ROW
WHEN (NEW.id IS NULL)
BEGIN
SELECT xtable_sequence.NEXTVAL INTO :NEW.id FROM DUAL;
END;

Or what is the proper way to implement autoincrement in Oracle in order to avoid the issue I am facing? At some point, I start getting unique key constraint violation on the primary key due to the fact that (I don't know for what reason) the max id in the table happens to be greater than the sequence.nextval used in the trigger. What is causing that and how to fix it?

6
  • what happens if you recreate the sequence but with nocache, instead of cache 100? Also, is your database a RAC database?
    – Boneist
    Oct 17, 2016 at 14:53
  • You mentioned triggers. Show us their content, please. And sql statements generated by hibernate as well Oct 17, 2016 at 14:53
  • You have wrote Its sequence's last_number is 78400, the max id in xtable is 78308, but the sequence's nextval is 78304. But Oracle sequence can't rollback a nextval value. Did you change it manually?
    – AlexSmet
    Oct 17, 2016 at 15:02
  • @KonstantinSorokin here is the sql statement generated by hibernate Hibernate: insert into ADMIN.XTABLE (COL0, COL1, COL2, COL3, COL4, COL5, ID) values (?, ?, ?, ?, ?, ?, ?)
    – bkn
    Oct 17, 2016 at 16:12
  • You may log parameters received by trigger into a log table using autonomous transaction. Maybe it never fired. Oct 17, 2016 at 17:51

2 Answers 2

1

To be honest this post is quite confusing on its own.

You state that,

"For my table xtable, Its sequence's last_number is 78400, the max id in xtable is 78308, but the sequence's nextval is 78304."

  1. What it tells me is by having sequence last number as 78400, there were 100 sequences that were cached in memory and that would have to be started at 78300. Once 100 sequences are cached they can only be used as long as server is not restarted and they change sequence last value to show 78400 in your case but it doesn't mean that is how many sequences have already been used that are just sequences which are cached in memory to be used by next insert, unless the database is restarted in that case you will lose those sequence numbers that were cached. BTW sequence cache is shared among different sessions.

  2. "but the sequence's nextval didn't change" Again you are assuming it that Last Value of sequence is same as sequence.nextval it is not the case. When you query dba_sequences view and look at Last_NUMBER column it represent last value CACHED not the last value generated by sequence.nextval or used in table.

To be honest to resolve this shouldn't take much effort.

A. Verify every time you insert row you must use sequence instead of running with procedures or triggers and then coming back to sequences, don't mix and match. (Remember one draw back of using direct sequences in insert is the order is not guaranteed like there could be entries like 1, 2 ,3 for id and next could be 10 reason could be that server was restarted and you lost unused cached value for sequences, if you really always want order than don't use sequence instead use procedure or other means).

B. Instead of first querying max id in table and then dropping sequence and then recreating again.

Drop the sequence first then get max value from table and then create sequence from that point onward. This will save you from losing track of sequence which may have been used already by dirty transaction from other sessions which may have been committed right when you were doing query to find max id on table.... but it is still not safe.

To make sure better results I would just create the new sequence starting from value above one value shown by below query, which should be used right before dropping the sequence.

select LAST_NUMBER from dba_sequences where sequence_name='YOUR_SEQUENCE_NAME'

Basically what I am saying is to be safe create the new sequence with greater value than the one currently been cached.

1
  • What i did is, I created new sequences with starting number as max(id) + 1000. It temporarily solved the problem, then my webapp started showing error messages again due to Unique constraint violations on primary key columns(As seen in the server log). When i check a sequence.nextval, it's again less than the max(id) on the related table.
    – bkn
    Dec 8, 2016 at 16:25
0

I figured the condition in which i was getting that problem. The thing is, while i was loading tens of thousands of records, for example executing a file containing 250000 insert queries, someone whould try to insert records (Through my webapp) at the same time. So probably, the problem occured when two insert query where gonna be executed at the same time.

2
  • I have the same problem only that users are only updating the tables and not inserting records.And i found last sequence way behind max ID by 20k records at a point.And i am trying to figure out how the users updating is causing such a problem.But in my case a procedure removes and adds records but still sequence should get next value on insert.
    – Evangelos
    Mar 21, 2017 at 12:04
  • This is tricky, I'm still getting the same problem and still haven't figure out the cause. This time what happened is, ids 1433 and 1434 has already been inserted, then a "unique constraint" was thrown because the sequence's CURRVAL was 1433 , but was supposed to be 1434, so that on the next call for NEXTVAL, it would be 1435.
    – bkn
    Apr 17, 2017 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.