Sequences are outside the control of transactions, and interfering with them to obtain brushless numbers will cause problems only because another transaction can simultaneously increase the sequence, which leads to spaces + duplicates, and not to spaces:
- start: generator value = 1
- T1: increment: value is 2
- T2: increment: value is 3
- T1: 'rollback', decrement: the value is 2 (not 1, as you expect)
- T3: increment: value is 3 => duplicate value
Sequences should primarily be used to generate artificial primary keys, and you should not care about the existence of spaces: it does not matter if the number uniquely identifies the record.
If you need a proven sequence of numbers, and the requirement is that there are no spaces, you should not use a database sequence to create it. You can use the sequence to assign numbers after creating and making the invoice itself (so that it is sure that it is saved). An invoice without a number is not yet final. However, even here there is a window of opportunity to get a space, for example, if an error occurs or another failure occurs between assigning an account number and fixing.
Another way could be to explicitly create a null account (marked as a canceled / lost number) with space numbers so that the auditor knows what happened to that account.
Depending on local laws and regulations, you should not βreuseβ or recycle lost numbers, as this could be interpreted as fraud.
You can find other ideas in the "Audible Series of Numbers" . It also contains a Delphi project using IBObjects, but the document itself very well describes the problem and possible solutions.
source share