aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes synchronized in Database singleton class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "synchronized in Database singleton class" Watch "synchronized in Database singleton class" New topic
Author

synchronized in Database singleton class

lambertlee Li
Ranch Hand

Joined: Dec 15, 2005
Posts: 52
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

Joined: Dec 15, 2005
Posts: 52
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

Joined: Jan 26, 2006
Posts: 64
I used one RAF per method call. But it has its own set of problems too..

I think many people prefer synchronization..


SCJP , SCJD. (IBM 142 in progress).
Cindy Rogers
Ranch Hand

Joined: May 17, 2006
Posts: 31
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

Joined: Dec 27, 2005
Posts: 89
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?


SCJD<br />SCJP 1.4
Thirumurugan Mylrajan
Ranch Hand

Joined: Jan 26, 2006
Posts: 64
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

Joined: Sep 18, 2003
Posts: 916

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.


SCJP, SCJD, SCWCD, OCPJBCD
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: synchronized in Database singleton class