Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized in Database singleton class

 
lambertlee Li
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Database class, which is a singleton. It provide funtionality to access db file(read, write).

I knew that I absolutely need to synchronized those methods like delete(), update(), create() as I need to prevent file pointer in RAF from corrupted. That means only one client is accessing the db file at a time.
My question is do we need to synchronized read() and search() method also?
 
lambertlee Li
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
me again.

If I am going to synchronized search(), update(), delete(), create(), read(), I end up with double synchronized in search().

my search method:



As you could see that my read() is synchronized, and also my search(). Will this double synchronized pose issues? Even it does not pose issues, but do you think I will lose points by coding in this way? How you guys solve this kind of problem?
 
Thirumurugan Mylrajan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used one RAF per method call. But it has its own set of problems too..

I think many people prefer synchronization..
 
Cindy Rogers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does your search method need to be synchronized? The read method is accessing the data file, which is the main reason to require synchronization.

The 'double synchronization' should be okay; the JVM simply increases the lock count - so there is not an additional performance cost. I'm not sure how wait/notify may work with multiple synchronizations, though.
 
B Chen
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thirumurugan Mylrajan:
I used one RAF per method call. But it has its own set of problems too..

I think many people prefer synchronization..


What are the benefits of using multiple RAFs? You would still have to synchronize. What happens if a write operation occurs in one RAF at the same time a read operation occurs in another RAF, affecting the same record?
 
Thirumurugan Mylrajan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by B Chen:


What are the benefits of using multiple RAFs? You would still have to synchronize. What happens if a write operation occurs in one RAF at the same time a read operation occurs in another RAF, affecting the same record?


I am not trying to solve the dirty read problem in the database. This also exists in databases with a read called "Uncomitted Read". So I can say my DB is a similar one. Rather I check that the data has not changed before doing any update in the client.

For updates though, the locking mechanism should take care of that. The simplest locking mechanism is where you synchronize all the data methods and in that case there is no need for locking!..

The only benefit is that no synchronization is needed (if you carefully code the data class to consider thread safety).

The drwaback is that file descriptors are limited..

More comments are welcome.
[ July 20, 2006: Message edited by: Thirumurugan Mylrajan ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Thirumurugan

The lock manager only "locks" a records - for concurrent access.
That means if user A modified record 1 and user B tries to do the same stuff one from both must wait until the other one is ready. But if two clients are modify ing two different records you get two threads modifying the same file, and this is a race condition.
You can make a simple experiment - build a simple program w here two threads are writing in the same file (different sequences) - let the pr ogram run a few times and compare the result.

Regards M.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic