aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Question of unlock() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


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

Question of unlock() method

FeiWen Cheng
Greenhorn

Joined: Dec 20, 2004
Posts: 21
Hi I have a stupid quesiton about my unlock method. My assignment is urlybird 1.3.2. The signature of my lock and unlock method are as follows:



The lock and the unlock methods in ordinary should be pair as follows.


My question is .

If someone invokes the unlock method and the record which is exist is not locked before. Is it need to throw the SecurityException from unlock method or do something else in this case? The scenario is as follows:

(The record is exist, and the cookie is fake.)
(I assume someone want to use the function, but he don't know how to use it correctly.)
[CODE]
cookie = 1;
unlock(recNo,cookie)
[CODE]

If someone invokes the unlock method and the record which is not exist is not locked before. How to deal with whis case?

(The record is invalid, and the cookie is fake.)
[CODE]
cookie = 1;
unlock(recNo,cookie)
[CODE]

I know all this case are came from the programming bug! But I am afraid of there is some trap I miss.

Thanks for any advice.


SCJP(1.4),SCWCD(1.3,1.4),SCBCD,SCJD(1.4),SCJD,SCMAD(1.0)
Frank Verbruggen
Ranch Hand

Joined: Feb 01, 2005
Posts: 37
Holy ..., man !

Write ur question in better english, cuz I dont understand what u are asking.
If I understand better, then I'll be able to help u.
But locking is a VERY delicate issue !
As long as u dont know for sure your locking scheme is perfect, think of it as a bad scheme.
Cya
[ February 07, 2005: Message edited by: Frank Verbruggen ]

----------------<br />SCJP (98%)<br />SCJD (81%)<br />SCEA (near future)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
My personal opinion is that an attempt to unlock a record which the process attempting to unlock it did not have a lock in the first place should not lead to an exceptional condition.
It should either fall through silently or at most generate a warning flag as a return value.
After all, unlocking a record is not a dangerous or highly involved process.


42
FeiWen Cheng
Greenhorn

Joined: Dec 20, 2004
Posts: 21
Sorry Frank. I will try to improve my English.

Thanks Jeroen. You know what I mean and I like your idea.

... an attempt to unlock a record which the process attempting to unlock it did not have a lock in the first place ...

This is my question!

Thanks for you help.
Frank Verbruggen
Ranch Hand

Joined: Feb 01, 2005
Posts: 37
If its about unlocking a record with the wrong cookie, my assignment states:

// Releases the lock on a record. Cookie must be the cookie
// returned when the record was locked; otherwise throws SecurityException.
public void unlock(long recNo, long cookie)
throws SecurityException;


So be sure to implement this correctly.
Lots of luck
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
The choice you have to make is whether a record that is not locked can be replaced, deleted and unlocked. All three of these methods require a cookie. The questions then become "what cookie should I use if the record isn't locked? Does any cookie match? What if another client has it locked?"

I chose to not permit these operations if the record isn't locked, and throw SecurityException, a fatal RuntimeException which indicates a bug in the client code.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Holy ..., man !

Write ur question in better english, cuz I dont understand what u are asking.
If I understand better, then I'll be able to help u.
But locking is a VERY delicate issue !
As long as u dont know for sure your locking scheme is perfect, think of it as a bad scheme.
Cya
Frank,

Please write your responses in better English, not web-speak, especially when commenting on another's language use. Words such as "ur", "cuz" and "u" may make sense to you, but they can make it very difficult for others to read - especially if English is not their first language. Also, many people with visual disabilities rely on text readers to comprehend web pages. Web-speak short cuts like this can also confuse the text readers.

Cheng,

I agree with Peter's response and solution to your question. Whatever solution you choose, make sure you document your decision.
[ February 07, 2005: Message edited by: Paul Bourdeaux ]

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Frank Verbruggen:
If its about unlocking a record with the wrong cookie, my assignment states:



So be sure to implement this correctly.
Lots of luck


Frank, but what if the cookie is null? (in other words there is no cookie)?
No cookie isn't the wrong cookie after all
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Jeroen Wenting:

Frank, but what if the cookie is null? (in other words there is no cookie)?
No cookie isn't the wrong cookie after all


If your cookie is an Object this would be a good choice for no cookie. Most of the newer assignments declare the cookie as the primitive type long, so you could choose -1 as a null equivalent. I considered that, but chose instead to not allow it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question of unlock() method