File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread safety - where state is in the database Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Thread safety - where state is in the database" Watch "Thread safety - where state is in the database" New topic
Author

Thread safety - where state is in the database

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 757

In my singleton class, multiple clients
will get back the same reference to the
object.
Then in a "unsynchronized" method of the
singleton; they will check if a particular
record exists in a table. If it doesn't
exists; the method will attempt to create
it. Can I depend on the database's
locking mechanism for updates (which will
lock the row for update) or should I
synchronize the method ?
I have some doubts about this particular scenario.
Firstly the pseudocode of the method looks like
this:

Suppose I have two clients, A & B.
When they both enter method() simulataneously,
let's say A executes line //1 and finds that
the row doesn't exist. But b4 it could execute
line //2, it is preempted; and thread B executes
line //1. So now I will have a lost updates situation.
Please advise.
Thanks
Pho


Regards,

Pho
Robert Ziel
Greenhorn

Joined: Apr 22, 2002
Posts: 28
Hi pho,
i would say using the synchronized code is much
cleaner.. and performance is better i think
you do not have unnecessary database access ..
when using only database locking it should work also..
Robert
[ May 22, 2002: Message edited by: Robert Ziel ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread safety - where state is in the database
 
Similar Threads
SQL injection?
Synchronized Objects
44/80.....damn locking!
NX: Locking and Unlocking and Sun's Must Conditions
Locking strategy with singleton