• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Synchronized method versus SELECT ... FOR UPDATE

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am having a multithreaded application which involves access of one static method which would generate unique id against a particular variable. This id is selected from database table and an incremented id is stored in table for next thread. Not the problem is, when two threads are simultaneously accessing this mehthod this gives out same code for both threads hence the uniqueness of the code is not maintained.
Ithink of two solutions
One : To make the method synchronized

Two : To change the SELECT statement which is used to select the id from database table to SELECT .... FOR UPDATE

Which option would be more suitable?

Plz reply
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

If you need to do an atomic operation on a database, and you can do the operation atomically from SQL, then you should do that. Using a synchrionized method would only prevent one copy of your application from creating non-unique IDs, but if there were two copies running at the same time, the synchronized method wouldn't help. Doing it in the database would work for any number of copies of the application.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's your database? I think all the big ones have non-standard sequential number generators. We're using UDB with sequence numbers and syntax that fetches the next number. Wish I could remember the syntax right now, but it is not SELECT.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Oracle we use sequences to generate unique ids. And as far as I remember Oracle takecare of thread-safety by itself.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
There are lots of places where there is a need for generating sequence numbers even though there is a option of generating unique ids using DB. i can think of Account Numbers in bank Accounts.

Anyway
1) one easy way is to go for synchronizing the method. - This is probably more widely used.
2) U can cache some 1000 number from database, keep updating the same in the application and then u can do a batch update, provided u are maintaining the sequence numbers in a seperate tables.


Two : To change the SELECT statement which is used to select the id from database table to SELECT .... FOR UPDATE


What will this lead to?? There is no need.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whatever. If you are going to use synchronized method then its better to use synchronized block rather then method. For reason you can search Threads and Synchronization forum.

Thanks.
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic