I have written an accounting module for my java app recently.
The module is based on MYSQL tables and INNODB Engine.
one of the requirements is of course a "running" ID for each invoice.
I tried to generate the id using the auto_increment
method and by using a sequence table, but in both I have the same problem: since the id is generated when I persist the invoice entity, if an error occurs during flushing of the persisted entity to the database, the auto_id is incremented, causing a "hole" in my series of invoices.
I can of course drop this method and assign the new invoice with the maximum ID of previous invoices + 1, but I think this is a bad practice. What other methods can I use to ensure I don't have holes in my invoices series, assuming that once in a while an invoice can fail to save due to some validation issue.