returning unique numbers concurrency in multi threaded application in JAVA

Hi,
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.

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rohit BajajAuthor Commented:
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.
0
Rohit BajajAuthor Commented:
yes Java advice
0
krakatoaCommented:
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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

MogalManicCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dpearsonCommented:
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 ;
}
0
Jan LouwerensSoftware EngineerCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.