aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is Locking required in reading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Is Locking required in reading" Watch "Is Locking required in reading" New topic
Author

Is Locking required in reading

Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
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 ]

Animesh Saxena<br /> <br />Open Source Developer
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11423
    
  85

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 ]

    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Animesh Saxena
    Ranch Hand

    Joined: Jan 15, 2006
    Posts: 62
    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
     
    subject: Is Locking required in reading