• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread safety - where state is in the database  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Robert Ziel
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!