aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes [ B&S ] - Locking Mechanism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "[ B&S ] - Locking Mechanism" Watch "[ B&S ] - Locking Mechanism" New topic
Author

[ B&S ] - Locking Mechanism

Laura Pecoraro
Ranch Hand

Joined: Mar 31, 2009
Posts: 45
Hi all

I try to fix my locking mechanism, but, I have some dubious

So, see follow esample:

#1
T1 -> Lock(1)
T2 -> Lock(1) -> Wait...
T1 -> Unlock(1)
T2 -> Restart... -> Lock(1)
This is easy (finally I understand the specification)

#2 (first dubs)
T1 -> Lock(1)
T2 -> Unlock(1) -> I can throws an Exception???

#3 (second dubs)
T1 -> Lock(1)
T2 -> Lock(1) -> Wait...
T1 -> Loop... ... ...
The thread n.2 (T2) wait until the T1 finished (possibility of death-lock :thumbdown, or, I can implements a time-out waiting??? For example 5 second, and if resource (record) is alredy locked by someone I throws an Exception???

#4 (third dubs)
T1 -> Lock(1)
T1 -> Lock(1) -> The thread n.1 (T1) try to lock some record... what happen??? I throws an Exception, or, I continued and to do nothing???

Hi Laura and Thanks for helps




Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4910
    
  10

Hi Laura,

This is what I did (and as you can see, I'm not a SCJD yet, just waiting for the results and it seems that i won't know my result before i go on holiday, June 7th) and should clear your doubts.
- when a record is updated, deleted or unlocked by a thread that is not owning the lock by the record, i throw an IllegalStateException
- when a thread has already locked a record and is trying to lock another record or the same record again, i throw an IllegalStateException
- I didn't implement a time-out mechanism, but you could do that. because i used thin clients, it's very unlikely that a thread won't unlock the record it locked.

Good luck!
Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Howdy, Laura. I'm very glad to see that you didn't give up! As soon as we fix this one, you'll be a SCJD too!

Just some comments on my buddy Roel's comments.

- when a record is updated, deleted or unlocked by a thread that is not owning the lock by the record, i throw an IllegalStateException


I did that as well. Just check if your unlock method throws a SecurityException; if so, then you should be throwing it in case the client trying to unlock a record is not the same one that previously locked it;

- when a thread has already locked a record and is trying to lock another record or the same record again, i throw an IllegalStateException


I did that too.

- I didn't implement a time-out mechanism, but you could do that. because i used thin clients, it's very unlikely that a thread won't unlock the record it locked.


Me neither. Some people have implemented it with success, though. But that isn't really required.

One thing to remember is, your locking mechanism needs to work properly (with the wait/notifyAll schema) in both networked and non-networked modes, even though you would only have one Thread in non-networked mode.
Another thing I'd kindly like to ask you, when you feel that you fixed your locking mechanism, please use the tests I created for it (you can find it here) in order to see if you still have problems with it.
That's it. Good luck and keep it up!!!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Laura Pecoraro
Ranch Hand

Joined: Mar 31, 2009
Posts: 45
Hi Roberto...

I try to use Tests-Data-Class... I like its

But I have a questions... my update methods "Lock" and "Unlock" automatically the record... This is a problem???

And at last... My score in "per-section" is good ( except by locking mechanism ) then I modify/fix ONLY lock & unlock methods or modify all my Data class implemetation??? (my data score is 40 (max 40))

Thanks Laura...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4910
    
  10

Hi Laura,

Your update/delete method should not lock/unlock the record in my opinion. to update a record you have to make 3 calls (without the use of a lockCookie):


your update method should first check if the record is correctly locked (that is: locked by the thread that wants to do the update) and then update the actual values (or when thread does not own lock on the record throw an IllegalStateException or like Roberto already said a SecurityException if your interface has such an exception).

So you will have to change your Data class implementation a bit (and probably your server and your client also a bit, because you need 3 calls to update a record and according to your explanation you have just 1 call, a call to update). Don't think you have to do a complete make-over of your data class or your server or ... because the per-section scores were more than good enough

Kind regards,
Roel
Laura Pecoraro
Ranch Hand

Joined: Mar 31, 2009
Posts: 45
Roel De Nijs wrote:Hi Laura,

Your update/delete method should not lock/unlock the record in my opinion. to update a record you have to make 3 calls (without the use of a lockCookie):


your update method should first check if the record is correctly locked (that is: locked by the thread that wants to do the update) and then update the actual values (or when thread does not own lock on the record throw an IllegalStateException or like Roberto already said a SecurityException if your interface has such an exception).

So you will have to change your Data class implementation a bit (and probably your server and your client also a bit, because you need 3 calls to update a record and according to your explanation you have just 1 call, a call to update). Don't think you have to do a complete make-over of your data class or your server or ... because the per-section scores were more than good enough

Kind regards,
Roel


Hi Roel...

Your observation is correct , but, in the "Assessor_1 report" I loss points only into record-locking mechanism... then, I thought about modifying only this section to don't change the previous score... but, I am very doubtful I don't know what to do




Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Laura Pecoraro wrote:but, I am very doubtful I don't know what to do


Alright, first things first

Indeed, you should not call lock/unlock within update/delete. These are supposed to be used by the services you offer to the users. So, booking a room means lock/update/unlock a record, performed by a method you offer in your services layer.

Also, it is pretty likely that you only need to fix the locking mechanism to be a SCJD without changing the other scores. But I think that the grade might not be exactly the same if another assessor looks at your assignment this time. So, you could just quickly review the other stuff, just to make sure (there are some little things I would do differently today, if I had to do it again).
Laura Pecoraro
Ranch Hand

Joined: Mar 31, 2009
Posts: 45
Roberto Perillo wrote:
Laura Pecoraro wrote:but, I am very doubtful I don't know what to do


Alright, first things first

Indeed, you should not call lock/unlock within update/delete. These are supposed to be used by the services you offer to the users. So, booking a room means lock/update/unlock a record, performed by a method you offer in your services layer.

Also, it is pretty likely that you only need to fix the locking mechanism to be a SCJD without changing the other scores. But I think that the grade might not be exactly the same if another assessor looks at your assignment this time. So, you could just quickly review the other stuff, just to make sure (there are some little things I would do differently today, if I had to do it again).


Hi Roberto... You have convinced me...

Thanks Laura
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [ B&S ] - Locking Mechanism
 
Similar Threads
Should delete() also remove the entry from the lock map?
Lock question!
ReentrantReadWriteLock's lock mechanism
record cache
Do I need notify()/wait() etc. when I just mark all Data-methods synchronized?