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
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: 754
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,<br /> <br />Pho<br /><a href="http://amour.zaadz.com/blog" target="_blank" rel="nofollow">http://amour.zaadz.com/blog</a>
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
 
Threads others viewed
44/80.....damn locking!
NX: Locking and Unlocking and Sun's Must Conditions
Locking strategy with singleton
SQL injection?
Synchronized Objects
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture