• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Is Locking required in reading

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Locking is ofcourse required in both update and delete. But to be sure is it not required when Reading a record?

When I try to read a record...... read(recNo)....How do i know that while I am reading the record its not being updated by update method or deleted?

Please Clarify??
currently I am doing this


isValidRecord(recNo) is a helper function which just checks whether recNo is not greater than existing number of records...
retrieveRecord(recNo) is a private function which gets the record from the File. (Its not synchronized)

Please Clarify if I am doing this wrongly??

[Andrew: Put code between [code] and [/code] UBB tags]
[ March 25, 2006: Message edited by: Andrew Monkhouse ]
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hI Animesh,

When I try to read a record...... read(recNo)....How do i know that while I am reading the record its not being updated by update method or deleted?

From the client's perspective you have at least the following two choices:
  • Don't worry about whether a record has been updated - just verify at the time of booking that it is still available.
  • Have the server notify every connected client of every record that gets updated, and verify at the time of booking that the record is still available.


  • Note that in this case you would have to either have the server keep track of which records each client is interested in so that you only notify them of the subset that they want, or you are going to have to build logic in the client to determine whether they are interested in an update.The second option is, in my opinion, way outside of the specifications given, and it is much harder to do. So why do it?

    Was that your real code you posted? If so, there may be some comments on it. But I am unsure I want to spend time commenting on conceptual code.

    retrieveRecord(recNo) is a private function which gets the record from the File. (Its not synchronized)

    If it is not synchronized, what is to stop two threads interferring with each other:
  • Thread A seeks to the location in the file where record 5 starts
  • Thread B seeks to the location in the file where record 3 starts
  • Thread A reads record 3 - that client now has the wrong data
  • Thread B reads record 4 - that client now has the wrong data
  • Note that this scenario gets even worse depending on how you are doing your updates, since you could corrupt multiple records just by failing to synchronizing on the read!

    By the way: Putting code between [code] and [/code] UBB tags] helps keep the indenting, which usually makes the code easier to read and more likely for people to comment on it. If you want to see how I did this on your post, click the edit button () that appears in the top line of your post (you can try clicking the same button that appears in my post, but you will just see an error message ).

    Regards, Andrew

    [ March 25, 2006: Message edited by: Andrew Monkhouse ]
    [ March 25, 2006: Message edited by: Andrew Monkhouse ]
     
    Animesh Saxena
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Andrew, Thanks for the reply. I changed the code. In your book you have used writeLock().lock() for persist method. Here in my case I think its not required. dataSectionOffset is always same(It is set in the beginning only once when the server starts). dbInfo instance stores this kind of data, like dataSectionOffset and recordLength. synchronization of dbFile (RandomAccessFile) I guess is enough for this function. Let me know if I am wrong..!!

     
    The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic