This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock(int recNo) throws RecordNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock(int recNo) throws RecordNotFoundException" Watch "lock(int recNo) throws RecordNotFoundException" New topic
Author

lock(int recNo) throws RecordNotFoundException

josine wilms
Ranch Hand

Joined: Jan 23, 2006
Posts: 45
Hi all,

About the have-to-use method signature lock(int recNo) throws RecordNotFoundException.

I'm very happy when the wanted recordnumber is not found, it means it's not locked allready and I can lock it.

In what case you catch this RecordNotFoundException in this method? Are there relevant cases?



Thanks,
Josine


SCJP 1.4<br />SCWCD 1.4<br />SCJD <br />SCEA (new version) Work in progress...
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,

Have you considered that "RecordNotFoundException", might mean that "the record has not been found (at all)", rather than "the record has not been found to be locked"?

Si


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
josine wilms
Ranch Hand

Joined: Jan 23, 2006
Posts: 45
Thanks for your reply, but it still seems not useful (for me!) :

I just have an empty HashMap in which a locked recordnumber with it's cookie is saved for the locking period, and taken out when the record is unlocked through the unlock method.

If the recordnumber is not found : that's a grace, I don't want to throw an exeption for it! . It means I can use the wanted recordnumber to store the cookie with it...

When would I want to throw this RecordNotFoundException within the lock method?

Maybe other people have other kind of implementations in which this Exception is usefull?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
I throw RecordNotFoundException in the lockRecord() only if the thread in waiting state gets interrupted:



Now, I also check that the record number is a valid positive integer, and if not I throw IllegalArgumentException, but I guess it could also be valid to throw RecordNotFoundException.



Now, another possible use that I did not implement, is to check that the record number actually exists in the data file. That is, if the record number is equal to or smaller than the number of records in the database. If not, throw RecordNotFoundException.



In my case I did not implemented this. The lock manager could lock a record that does not even exist in the data file. Now that I think it over, maybe I should work on this.
[ October 22, 2006: Message edited by: Edwin Dalorzo ]
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi Josine,

I certainly don't want to tell you how to interpret the spec, since it is a personal choice and part of the assigment.

But, I would suggest that you should think about the perspective from an interface point of view, rather than an implementaiton point of view.

That is to say, what does (what should) "Record Not Found" mean to someone who is trying to use the lock method on a record?

Cetainly an exception means that something has gone awry.

Simon
josine wilms
Ranch Hand

Joined: Jan 23, 2006
Posts: 45
Thank you both.

I have taken it as:

Also check if the recordnumber is still existing and being undeleted in the physical datafile.



Chulwoo Choi
Ranch Hand

Joined: Apr 11, 2005
Posts: 65
Hi,

RecordNotFoundException is thrown not only in the lock() method. (for e.g., readRecord method also throws it.)
So I think, when you are locking a record, if the record doesn't exist in the database, the exception is thrown. This is a multi-user system (in the networked mode), so someone else may have deleted the record when you're about to lock it; then, you throw RecordNotFoundException without even trying to lock the record that doesn't exist.
If a thread can't lock an "existing" record because another thread has locked it, the thread just waits until the lock-holder release the lock (intead of throwing RecordNotFoundException).

Regards,
Chulwoo

p.s.: My project is B&S.
Martin O'Hara
Greenhorn

Joined: Sep 25, 2006
Posts: 3
In my lock method (I have a seperate class for Lock Management) lock the record first, then i call the read method which throws a RecordNotFoundException if the EOF is encountered or if the record has been deleted. If an exception is thrown from the read method I unlockit right away.

In other words, get a lock regardless of the record number. The lockmanager will wait() if the record that is to be locked is currenlty locked and wil return a lock cookie. Then i read the record (via read method) if the read method throws RNFE (for either not found or deleted), i remove the lock on the record.

This technique will handle if the record that you are trying to access has been deleted while you are trying to act on it.

Does anyone see any problems with this implementation?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lock(int recNo) throws RecordNotFoundException
 
Similar Threads
RecordNotFoundException
Locking for URLyBird 1.1.1
implementing interface methods within another interface method
B&S cookie value
Tests for the Data class/locking mechanism