This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Locking required in reading

 
Animesh Saxena
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
    • 0
    • Mark post as helpful
    • send pies
    • 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..!!

     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic