| 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 ]
|
 |
 |
|
|
subject: Thread safety - where state is in the database
|
|
|