UniqueNumbersService is missing/skipping value

Hi,

I’m using CUBA 7 and using UniqueNumberService in preInsert like below.

There is some strange scenario where one number is not produced and next is continued.

Below is the entity log where #3 is create and then #5 created after an hour time. #4 is missing.

Thanks
Hari

Hi Hari,

The UniqueNumbers implementation is based on database sequences and doesn’t guarantee a continuous sequence without gaps. It guarantees only uniqueness of the values.

So it could be that #4 was acquired but the document was not saved due to some error and transaction rollback.

Regards,
Konstantin

Hi Konstantin,

Happy new Year!

Thanks for your reply.

Is there a way to handle this scenario of error while saving and reverting the sequence?

Regards,
Hari

Happy New Year :slight_smile:

Usually, you cannot just decrement the sequence on an error, because if another user already got another number, it would break uniqueness.

To have a unique sequence without gaps, you have to block on some database record which contains the current number. Think twice before implementing such an algorithm, because it can seriously affect performance.