aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes referenced and last confusion 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 "referenced and last confusion" Watch "referenced and last confusion" New topic
Author

referenced and last confusion

Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
Hi,
a. I still confuse with it.
That's what I do in my client doBooking():
1. read the record
2. user is thinking
3. lock()
4. re-read the record
5. has record been changed? (or is there still seat available?)
6. ? fail : update.
7. unlock()
After step 5,there may something happened:
traffic jam
computer dead
...
After a while ,System receive the lock from Client
A(Unreference).Then Client B got the lock. Now A is all ok. and A wants to do the step 6.(maybe B is doing step 6,so A wait)
Will that senario occur?
How to prevent it?
b.
in my unreferenced(),I will clear all the locks.
But I wonder,if a client is operating between lock() and unlock()(like step 5),then what will
happen? Am I right?
Thanks a lot
:roll:
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Ray
I handled this by checking if the record was locked in my modify() method, and throwing an exception if not. And then I documented this everywhere
I think clearing locks after unreferenced is called is a good idea - I also do this. Normally unreferenced doesnt get called until around 15 - 20 minutes after the client dies, so it would have to be a very patient user who waited 20 minutes for their deadlock to clear so that they could continue.
If you want to you can change this value (I would suggest only changing it for testing purposes - waiting for 20 minutes to see if your unreferenced code is working correctly for multiple threads is a real pain).

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
Hi,
Do you mean I should recheck the lock not only in the getLock() ,but in all the methods which will be used between lock() and unlock()?
Thanks a lot!!!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Ray,
I did, but that was my decision ... you have to decide for yourself if it is right for you or not. I felt that it was better to enforce the locking than to trust in user programs. But I am not sure if I have broken the spec as a result of this and other things (such as having very crude deadlock handling). I am not very happy with my decision, but I am at the point where I am about to submit (probably over the weekend & exam next week), so I am not going to change it now.
Anyone care to give an alternate view point?
I have worked with databases that have forced locks before allowing modifications. I have also worked with databases that assume the user knows what they are doing, and also with databases that hide the locking (if you have not locked the record when you do the modify they do the lock and unlock for you). So I think any of these could be justified.
By the way ... that last solution has also been used by people who have passed this exam.
Regards, Andrew
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
Hi,
Is it necessary to make all the methods between lock() and unlock() recheck the lock? Or is it required in this exam?
Should I use Unreference or TimeMasker?
Could I use both of them?
Thanks
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Ray,
Had some more thoughts about this last night.
I do not know but I suspect that once unreferenced has been called, the client will no longer have a valid connection. So in the scenario you were describing (lock, <freeze for 25 minutes>, modify ....) by the time it gets to the modify, the unreferenced will have been called, and the modify should cause an RemoteException to be thrown.
If I can, I will throw together some code to test this later today (not sure if I can duplicate it or not though) and let you know the results.
Regards, Andrew
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Ray,
What is TimeMasker?
Regards, Andrew
Ray Cheeny
Ranch Hand

Joined: Aug 21, 2002
Posts: 74
Hi,
Sorry,it's TimerTask. I find that 15 min is too long.So I'd like to make a LockTimer extends TimerTask.
How do you think about it?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Ray,
Regarding Is it necessary to make all the methods between lock() and unlock() recheck the lock?
I check (not recheck) for a lock on anything that can do a modification (add / change / delete). My reason is that if some bad code came along (written by someone else of course ) and if they could try and do a modification without doing the lock, then my whole effort of doing the lock, verify, modify, unlock is wasted - I cannot guarantee that I will get the seat. So every potential modification checks for a lock, and if it doesnt exist, then I throw an exception.
This is not asked for in the assignment, so you should be able to ignore this issue - but you might want to make a note in your design decisions document to indicate that you thought about it and rejected it.
Regarding TimerTask - I think it can be justified, so you should be OK with it.
For myself, I think that this is something we may have to think about in real life, but for the assignment, I think we can safely leave the standard timeout alone. Think about what you are doing to any other applications that are coding to the interface specified - if someone writes some very complicated code that takes two minutes to run between the lock and the modify, and you chose an arbitrary timeout of 30 seconds, then their code wont work, even though they may have a reasonable expectation that it should.
OK, managed to confirm my suspicions that once unreferenced is called, the network connection is no longer valid.
Here is my code:
(If you are not used to it - using any of the triginomic functions is a great way to eat up resources In my case, this running cos() repeatedly like that made my CPU jump from 1% usage to 87% usage )
I could not get the above to call unreferenced on its own, so then I started both applications, then killed the network itself, and after 15 seconds restarted the network. The results were:
The ConnectionException was on the client side. (not very obvious above).
After I copied and pasted all the above, I realised that when I killed the network, the server actually shut down (with no messages at all), so that didnt really prove my point. So I set it up so that the server was on a different computer, and it was the client that had it's network die. So the server stayed up and called unreferenced, and the client gave a ConnectionException when it tried to use its existing reference to the server.
Regards, Andrew
[ May 21, 2003: Message edited by: Andrew Monkhouse ]
[ May 21, 2003: Message edited by: Andrew Monkhouse ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: referenced and last confusion
 
Similar Threads
B&S: unlock() method
NX: URLyBird Database Record Number Question
NX: Using SwingUtilties.InvokeAndWait method in GUI
Data Class Locking Test Doublecheck
Topic: NX: Unique Client or Transaction ID