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

unlock method question

 
Alexander Guan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder in which situation unlock method will throw RNFException. Can someone give me some examples?

 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
K. Tsang
Bartender
Pie
Posts: 3222
12
Android Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fola Fadairo
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Guan Guna", please check your private messages for an important administrative matter.
 
Dmytro Stasyuk
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 9150
76
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Bram Pramono
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic