• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

unlock method question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder in which situation unlock method will throw RNFException. Can someone give me some examples?

 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!)
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Guan Guna", please check your private messages for an important administrative matter.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic