-2

In general, either in Java or in DB design, is it possible to have a record where a field can be in a sequential, ordered number with no gaps from each other? Like you have multiple users and you are both submitting a form, is it possible for the records to be labeled 1,2,3,4...n in order and with no gaps?

I know that we have a sequence in database design that allows uniqueness in fields/columns, but in the case of multiple users and no serializing the risk of gaps is still there. Is there something in Java or in Database design that can incorporate sequences with no gaps?

This has been bothering me for months and I can't figure it out.

WattMonki
  • 29
  • 3
  • 8
  • 2
    Theoretically yes. Anything is possible. But as posed this is a very broad question, and the OP is mixing client and server concerns. Post a code attempt. – RamblinRose Dec 29 '21 at 03:45
  • This could be a good a question if rewritten to focus on one scenario. Asking "in general" questions is not appropriate on Stack Overflow, in general. – Basil Bourque Dec 29 '21 at 05:40

1 Answers1

1

Yes, of course, but such a functionality typically clashes with the logic of an interactive multiuser/multithreaded application that would ideally produce immediate results. If this is not your case, then a simple trigger is the solution.

On the other hand, if you need immediate results, read on. You would probably need to implement delayed processes that assign these numbers in an orderly manner instead of doing it interactively when each row is inserted in a table. For example, you could record every new data insertion in a "pending" table or queue for further processing.

There are many options but off the top of my head you could:

  • Use a process behind the scenes that will run every 5 minutes, and processes all pending rows.
  • If you use a queue then you can have a reactive process that is continually running and waiting for rows to process and assign numbers, one at a time.
  • You can even delay all number assignation for the nightly process if that's an option.

For higher performance, you can implement any solution above in parallel, even in separate hardware if you are able to partition the data somehow. For example all IDs ending in 01-03 (partition #1) will be processed by node/process #1, then IDs ending in 04-07 (partition #2) could be processed by node/process #2 and so on.

As you see there's nothing built-in in the database for your use case, but there are plenty of good solutions for it, if you can relax some contraints, like perform the number assignation with a 5-minute delay.

The Impaler
  • 45,731
  • 9
  • 39
  • 76
  • Also, if you want to maintain the sequence over a long period of time, you can never delete a row. However, you can mark rows as deleted with a boolean column. – Gilbert Le Blanc Dec 29 '21 at 07:49