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

B&S 2.2.2. - Thread Safe Access to File

 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10385
132
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic