• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Get unique orderID - recursive call

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I want to generate e unique orderUID (from a timestamp converted to milliseconds).

At first I just thought I could synchronized the method, but I soon realized that this doesn't ensure that the orderUID is unique.

To solve the problem I check in the database if the orderUID exists. If it does I call the method getUniqueOrderID() recursive. I am unsure if the recursive call is made properly. It seems that the method is returning orderUID twice if the orderUID exists first time.

Any help would be appreciated.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeppe Sommer wrote:Hi.

I want to generate e unique orderUID (from a timestamp converted to milliseconds).

At first I just thought I could synchronized the method, but I soon realized that this doesn't ensure that the orderUID is unique.

To solve the problem I check in the database if the orderUID exists. If it does I call the method getUniqueOrderID() recursive.



That still won't work. Multiple threads can still create the same ID. A better approach is to just let the DB generate the ID for you.
 
Jeppe Sommer
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

I forgot to tell that I am calling the method inside a servlet, so I guess this is thread safe?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeppe Sommer wrote:I forgot to tell that I am calling the method inside a servlet, so I guess this is thread safe?


I suspect so, but the fact is that if you follow Jeff's suggestion, it doesn't matter. Databases have been creating sequence numbers for longer (I suspect) than you've been alive, so unless the time is critical to your application, forget it (or store it separately).

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeppe Sommer wrote:Hi.

I forgot to tell that I am calling the method inside a servlet, so I guess this is thread safe?



Only if you're running that servlet in the single thread model, which you shouldn't be doing.
 
Wanna see my flashlight? How about this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic