Hi all,
The access to my RandomAccessFile is not synchronized. I'll try to explain why I think that is not necessary.
My Data class implements an extended Sun interface which has some additional methods. Let's call them readDatabase and writeDatabase. The Data class has some kind of worker class for handling the file I/O, working with the RandomAccessFile. (The Data class also has a worker class for logical record locking, which is not important here).
When the stand-alone client or the server instantiates the Data class (started from the business layer) this happens:
* At constuctor:
- create new worker for logical record locking
- add shutdownhook
thread which makes two call at shutdown: writeDatabase in the worker class and close the RandomAccessFile
After creating the Singleton Data instance a readDatabase will be called (with the by the client specified database location), which will read all the records in a Map<Integer,
String[]>. This content will be the record map in the Data class that is used as a record cache. Manipulation of this record cache (read, find, update, delete, etc) is handled by a ReentrantReadWriteLock.
When the stand-alone client or the server window closes, the shutdownhook thread will write all the records back to file and close the RandomAccessFile.
So I think in my current implementation both the readDatabase and writeDatabase don't have to be synchronized. But if the record cache implementation is replaced by directly working on the datafile then its access should be synchronized.
Maybe I just have to add the keyword synchronized to both my readDatabase and writeDatabase methods in the worker class handling file I/O?
Can anyone comment on this. I'm afraid to make a stupid mistake which will result in a failure (and then in a much more expensive exam...course and stuff..deadline coming up).