returning unique numbers concurrency in multi threaded application in JAVA

Posted on 2014-08-11
Last Modified: 2014-09-05
I have written a web service. It returns a number based on todays date+count
eg 201408111 will be first number returned when it is hit on 11th August 2014 first time.
Second time it will return 201408112, third time 201408112....same on
the initial date remains todays date and concatenate with the number of times it is called.

How its currently implemented is each time a hit comes it fetches the last record in a column and returns it
and then inserts a new row with next column value. So next time the service automatically returns the last column and adds the next.

Problem with this approach is that there can be synchronization or race condition issues.
Like if two there are multiple requests from different machines. They may get the same number resulting in inconsistency.
Please suggest how to avoid this.

Question by:Rohit Bajaj

    Author Comment

    by:Rohit Bajaj
    No this is not a database question. Its just an approach i used in Java. There can be multiple approaches which does not use database.

    Author Comment

    by:Rohit Bajaj
    yes Java advice
    LVL 16

    Expert Comment

    Not sure if you actually have a problem, or if I don't understand your problem, but since you know which day it is all day long, you just increment and dish out that Integer every time there's a hit.
    LVL 21

    Accepted Solution

    Since you are using the database for your web service 'state', I would use the database for concurrency locking.  You could do a select..for update that would "lock" the last row in the table.  If any other threads call your web service before you inserted the new row, the row lock would block at the "select...for update".  Then after the insert completes and the new row is the "last Row", release the lock created by the "select..for update".  

    You also have to make sure that you wrap the code in a try/catch/finally so that if an exception is generated, you still release the lock on the database.
    LVL 26

    Assisted Solution

    In the case where you aren't using a database and you're storing the state in memory, then you'd just wrap it with a synchronized call:

    private int counter = 0 ;
    private String lastDate = null ;

    // synchronized makes sure only one thread can update at a time
    public synchronized String updateCounter() {
         String date = getDate() ;
          if (!date.equals(lastDate))
               counter = 0 ;

          lastDate = date ;
          counter++ ;
          return date + counter ;
    LVL 5

    Assisted Solution

    by:Jan Louwerens
    If you want to do it in memory, you can use an AtomicInteger object.

    If you want to keep it in the database, you can use a sequence within the database.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now