| Author |
B&S 2.2.2. - Thread Safe Access to File
|
Pete Palmer
Ranch Hand
Joined: Oct 21, 2008
Posts: 74
|
|
Hi,
My Data class uses the Facade Pattern to provide the access to the file ( DatabaseAccess class ) and record locking facility ( LockManger class ).
When the server is started a single instance of the Data class ( and of DatabaseAccess & LockManager) is created to support database access to the many clients.
The DatabaseAccess class provides following methods :-
readRecord()
deleteRecord()
findByCriteria()
createRecord()
updateRecord()
To ensure access to the database file is thread safe, I could synchronise all the above methods which would be easy and effective or I could introduce “ReadWriteLock” variables so that I could synchronise specific code blocks.
Any reason why I shouldn’t just synchronise all the methods in the DatabaseAccess class ?
Pete
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4349
|
|
Pete,
If you synchronize all methods you will loose a little bit of performance and concurrency. but according to the instructions that's not one of the main requirements.
I synchronized also all my methods in the Data-class and made of course a singleton of the Data-class (like you did)
Kind regards
Good luck
Roel
|
SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
|
 |
 |
|
|
subject: B&S 2.2.2. - Thread Safe Access to File
|
|
|