Create 1K IDs, assign transaction ID to all rows with concurrency

  • The client will request 1K strings of IDs from the server.
  • I have to make sure we have a 1K ID with clientID = -1, if not, I need to insert a 1K new ID into the table.
  • Then I need to associate these 1K IDs with the client ID
  • returns a 1K reserved id to the client.

Is it as easy as transferring it all into a transaction?

0


source to share


2 answers


You want to add a second table containing the available IDs, for example:

AssignedIdsByClient:

  • AssignedId int identity

  • ClientId int is the client you gave this ID range,



But if you need 1000 IDs, don't insert 1000 records here: instead take this AssignedID and multiply it by 1000. For example, if someone gets AssignedID 15, that means they have IDs 15,000 to 15999 in another table where you really need IDs.

Otherwise, if you are trying to insert 1000 records in one transaction every time someone wants an ID range, you will have a concurrency nightmare.

+2


source


Yes, you can wrap them all in a single transaction and other clients won't be able to see them until you commit.



0


source







All Articles