[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Developer Certification (SCJD)
 
RSS feed
 
New topic
Author

About locking and RandomAccessFile

Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Messages: 52

Hello everybody ... I am just starting getting involved with my SCJD exam and I already have implemented my data layer: A simple class reading and writing to the dataFile using a RandomAccessFile (RAF).

Now my question ...

why do you all use one RAF that you share between Threads ?

Isn´t it much easier to use individual RAF instances for each request?

best regards

daniel

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Messages: 1225

Howdy, Daniel!

Champion, I don't think this could be possible. The file is unique, so someone will have to be Singleton, so the file doesn't get messed up. My Data class is Singleton, but some people choose to create the Data class as a Facade, and divide the responsibilities in a class that only reads the file and another one that manages the locking mechanism. This class that deals with the file has only one RandomAccessFile object, and then there's the possibility to either synchronize on this object, or to synchronize on the object that deals with the file in the Facade class.

Cheers, Bob "John Lennon" Perillo
SCJP 5.0, SCWCD, SCJD - Daileon: A Tool for Enabling Domain Annotations
Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Messages: 52

Ok sorry, I still don´t get it.

When I´m using a ReadWriteLock approach that makes sure that only read requests can be run simultanously and not write requests - what can be messed up if I instantiate a RandomAccessFile everytime I need one ?

Daniel Breitner
Ranch Hand

Joined: Nov 13, 2008
Messages: 52

To be more precise:

How about doing it this way:

There is one SIngleton: The Lock-Manager. It handles all locks from all clients by the ReadWriteLock Pattern.

Is this sufficient for the locking part of scjd or did I forget sth ?



Roberto Perillo
Bartender

Joined: Dec 28, 2007
Messages: 1225

Howdy, Daniel!

Is this sufficient for the locking part of scjd or did I forget sth ?


Well, I think it is. Honestly, since I used the classical synchronization, I'd have to test to see if it works properly, but I think it can do the job. But the important thing is to make sure that, when a Thread tries to lock a record that is already locked by another Thread, then this Thread consumes no CPU cycles when waiting for it to be released.

Cheers, Bob "John Lennon" Perillo
SCJP 5.0, SCWCD, SCJD - Daileon: A Tool for Enabling Domain Annotations
sohrab khan
Greenhorn

Joined: Sep 10, 2007
Messages: 13

Daniel Breitner wrote:Ok sorry, I still don´t get it.

When I´m using a ReadWriteLock approach that makes sure that only read requests can be run simultanously and not write requests - what can be messed up if I instantiate a RandomAccessFile everytime I need one ?



You don't need to instantiate a RAF everytime you need to read or write a record. It is not a good design, you can instantiate a single RAF object, store it in memory and use it whenever you need it.

Sohrab Khan SCJP, SCJD, OCPDBA, MCSE
3tons Web Directory | EggDir Directory
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Developer Certification (SCJD)
 
RSS feed
 
New topic
MyEclipse Enterprise Workbench