How to avoid the creation of Duplicate Numbers

Hello,

We have a requirement where we generate a unique number to the customer. The number generation criteria is that, look for the recently created unique number in DB, increment it by 1 and then persist it to the DB. All these rules are carried out in the Button Submit. The Problem here is, when two users Submit the Form at the same time then the Same unique number(Actually Duplicates) is created for them. Is there someway to prevent this ?

Thanks

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Hi

    I have couple of quick question to better understand about the various approaches you have tried.

    • Which DB are you working with?
    • Why no AUTO_INCREMENT / SEQUENCE (depending upon the DB vendor)
    • Are there any specific reason behind performing this operation on Button Submit?
    • Are you triggering an instance of a process upon button click or using smart service function to persist the data into the database?
    • If it's smart service function, then why don't you opt for process model approach?

    Few suggestions

    • I would recommend you to perform these set of actions in a process and make the use of Auto generation strategy for the Id generation
    • If you are making the use of smart service function, logically you will loose the trace of the action, whereas if it's in process you do have an option to monitor & debug the process. This means, debugging would be difficult if any issue gets flagged in production while using smart service functions to persist the data

    Hope this will help you in debugging and resolving this.

  • You need to look at what locking mechanisms are in place or available for the database that you're using. Locking means that even if two transactions appear to be concurrent only one can take place at a time, so this will solve the issue that you're concerned about.