my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Synchronized method versus SELECT ... FOR UPDATE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized method versus SELECT ... FOR UPDATE" Watch "Synchronized method versus SELECT ... FOR UPDATE" New topic

Synchronized method versus SELECT ... FOR UPDATE

Gandhali Daundkar

Joined: Dec 01, 2005
Posts: 1
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

Joined: Jul 08, 2003
Posts: 24166


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.

[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
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

Joined: Oct 03, 2005
Posts: 118
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.

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

Joined: Aug 15, 2004
Posts: 2874
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.

I agree. Here's the link:
subject: Synchronized method versus SELECT ... FOR UPDATE
Similar Threads
prepareStatement returns 0?
Spring Transaction
prepareStatement returns 0?
Concurrency Issue
Database Thread Safety, pls review