This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes unlock method question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "unlock method question" Watch "unlock method question" New topic
Author

unlock method question

Alexander Guan
Greenhorn

Joined: Oct 29, 2008
Posts: 2
Hello,

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!
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
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
I wonder in which situation unlock method will throw RNFException. Can someone give me some examples?

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2247

I wonder in which situation unlock method will throw RNFException.


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.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 1973
    
    7

When to throw RNFE in the locking process is depends on how you write your lock method.

The locking process is lock -> update/delete -> unlock. So once you lock your record, that record must exist for your unlock method to release.

For me, I did all the checking in my lock method. Update/delete and unlock methods don't "physically" throw RNFE.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7
Fola Fadairo
Ranch Hand

Joined: Feb 16, 2004
Posts: 35
The specs say:
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!)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

"Guan Guna", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dmytro Stasyuk
Greenhorn

Joined: Apr 02, 2009
Posts: 5
Have you thought about throwing a RNF exception if the record number supplied is invalid, such as if it is < 0 or if it is too high?

The client code application could easily have a bug that would supply such an illegal value.
Bram Pramono
Greenhorn

Joined: Feb 19, 2009
Posts: 6
Hi guys,

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.


SCJP 5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4925
    
  10

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)


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Bram Pramono
Greenhorn

Joined: Feb 19, 2009
Posts: 6
Wow Roel, you made me change my perspective. Of course... it should be possible to remove the RNFE and not using it instead of forcing the method to throw it. Thanks for the idea.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unlock method question
 
Similar Threads
Locking strategy with singleton
NX: How to unlock(int recNo) after delete(int recNo)
pls validate my locking strategy - all inputs are g8ly appreciated. (URLyBird)
Locking strategy URLyBird 1.1.1 with ReentrantLock
assert false or RuntimeException() for unreachable code