Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronized method versus SELECT ... FOR UPDATE

 
Gandhali Daundkar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • 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.
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic