• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why do i care about locking when i use singleton fileaccess

 
fei lin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have a class that is singleton and its responsible for reading and writting to the database file. why do i still care about locking?
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

so there are more than one client will access this class and use the
methods provide ex
bookRecord :
1- read record to check if exist.
2- check if the record available for book
3- book the record.

if two clients use this method at the same time may be cause logical error
in the database. also the locking is business issue !.

best regards.
Mohamed darim
 
fei lin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i synchronize this method. its 1 atomic action.
 
Mike Ottinger
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fei, synchronizing the entire method, while technically could work, you need to take into consideration performance. Imagine a scenario where many threads are vying for access to the methods of your object. Each of them having to wait for the entire method to execute before they could possibly have their turn, would invariably result in poor performance. The best approach with multi-threaded programming is to synchronize only the resources that's state can be affected by multiple threads. This could be the RandomAccessFile object in your data class, or some sort of map object in a locking class. This approach does require you to think carefully about your design, but the improvements in performance are well worth it. Hope this helps.
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

thank Mike Ottinger to your reply, another reason that the DB interface is
general interface and does not provide contract that any clients must
synchronized prior to use.

Best regards.
Mohamed Darim.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic