1
votes

Currently i don't have allocation size set in my @SequenceGenerator annotation. So the default value is 50

int allocationSize() default 50;

As expected there is gap in ids of the correspoding table.

Now i want to change the allocationSize to 1. Is there any chance of id collision then?

1
i want to know how it actually handles uniqueness and know the theory well. - shakhawat

1 Answers

0
votes

I did some experiments to exactly know what will be happening if suddenly someone change the allocationSize from default one.

Short answer to my question is - Yes there will be duplicacy in Id generation.


Before changing allocation size in my application allocationSize = 50 (default)

when sequence value is 4 in db, hibernate start generating id from 4*5 = 200

DEBUG AbstractSaveEventListener - Generated identifier: 200, using strategy: org.hibernate.id.SequenceHiLoGenerator

Which is expected behavior of Hilo generator with this default allocationSize


Then when i change my allocationSize = 1 and deploy my application again.

Now,

Hilo generator start id generation from currentSequenceVal * allocationSize as it did before, for currentSequenceValue = 5, it will generate id 6, 7, 8 ....

which will eventually lead it to duplicate id generation.


Now the solution is to drop the sequence and create the sequence again with starting value currently used maximum id.