简体   繁体   中英

Sequence Generator

I got a DB which store customer details(I have a Form to enter details and customer number is generated automatically).If I have 1,2,3,4,5 as customer IDs then next time when I create a customer the Form should default Customer ID as 6.

I achieved the Above Goal.But the Thing is if I simultaneously create two customers both will show up as 6.But What I want every Time open a Form the value of Customer ID should be unique.(ie) If I click create customer Now it should show 6 and after that i open another form simultaneously i should get customer ID as 7 ...

How to achieve this?

I would suggest to make Customer ID as primary key with auto increment.

So whenever you add the values into customer table, Id will be Unique and it will be incremented automatically.

Reason Behind this is : In your code if two users are using the same form at a time, both will receive the same maxPartyId and when both users submit the form, the same Id maxPartyId + 1 will be stored. That means customer id will be duplicate in your database

Two possibilities:

  • Database : Let your DBMS do the primary key generation for you; don't assume you have a valid ID until you finish record generation. Implementation will heavily depend on your database of choice.
  • Singleton/mutual-exclusion lock : Let a single class generate and distribute IDs. Use the lock statement to prevent parallel generation.

Personally I would recommend the first approach; Implementation is usually cleaner, and you don't have to bother about thread locking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM