unlock method question

I'm not very clear about unlock method. It says that unlock method throws RNFException when the record is deleted.

But consider this condition, I execute following method -- lock() -> delete() -> unlock(). My question is that after I use delete() method, then the record is marked deleted, in unlock() there is some checking code like this
if (isDelete(recNo)){throw new RNFException;} because isDelete(recNo) will always return true after I use delete() method, unlock() method will always throw RNFException。

How to solve this problem please?

otherwise, I want to know in what situation, will unlock() method actually throw RNFEception?

I'm a Chinese student that my expression may be not clear enough. If it is, I will be sorry about that.

By the way, thanks for this forum, I really learn a lot here. Thank you all guys!

after I use delete() method, then the record is marked deleted, in unlock() there is some checking code like this
if (isDelete(recNo)){throw new RNFException;} because isDelete(recNo) will always return true after I use delete() method, unlock() method will always throw RNFException。

You could modify your code:

You could also check if the record is not locked before throwing RNFException.

You should also check that the client attempting to unlock the record is the same client who locked it.

Alexander Guan
Greenhorn

Joined: Oct 29, 2008
Posts: 2

posted Apr 04, 2009 08:14:32

0

I wonder in which situation unlock method will throw RNFException. Can someone give me some examples?

It could be when you try to unlock a record that doesn't exist... obviously, this is not likely to happen in your application, but we have to think that we are creating these APIs for other people to use, you know... so if someone uses it wrongly, then we're prepared.

Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file

Examples of when Record not found Exception would occur:
1) Trying to read a deleted record.
2) Trying to update a deleted record.
3) Trying to delete an already deleted record.
4) Trying to lock a deleted record.
5) Trying to unlock a deleted record! (The only exception here would be: a client tries to unlock a method it locked after deleting it!)

I have also faced this problem and thought about it. The delete process will have to be lock() -> delete() -> unlock() , so which means that throwing RNFException on unlocking will always happen if the condition to throw RNFException is record not existing or deleted. This does not make sense.

Post 04 April 2009 16:27:58 Subject: unlock method question

It could be when you try to unlock a record that doesn't exist... obviously, this is not likely to happen in your application, but we have to think that we are creating these APIs for other people to use, you know... so if someone uses it wrongly, then we're prepared.

Even if someone else is trying to use this API, why would he/she unlock something that is not locked.

My solution to locking records in the database is using Map<Integer,Long> so every record that is locked is registered.

So the only condition I can consider of throwing RNFException is when the record number entered is not registered in the Map.

PS : the specification on the assignment about throwing RNFException is
Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.
It's not a must statement, so it should be safe if you dont fully follow it.

In my implementation:
- only a RNFE is thrown by read, lock and isLocked-method
- trying to delete, update or unlock a record that is not locked by the thread that's invoking the requested operation will result in an IllegalStateException being thrown (because the API is not used correctly)