SCJD 1.5<br />SCJP 1.5
Originally posted by Alex Belisle Turcot:
Hi,
I'm not quite sure what is your question, could you rephrase for me ?
Regards,
Alex
SCJD 1.5<br />SCJP 1.5
SCBCD - SCWCD - SCJD - SCJP - OCA
Originally posted by Musab Al-Rawi:
Hi Conny,
As an advice start with the Database first and forget about the GUI.
now in the DVD Example he is identifying the locker using an instance of the fasade (that's why he is sending "this" with the record number to the reservation manager instance).
You can use the same example in the DVD example. But make sure that your interface doesn't require you to return a magical number or cookie.
now you can improve on the design of the locking that you have in the book and most probably you have to make changes to suit your design and data.
hope that helped.
[ November 15, 2007: Message edited by: Musab Al-Rawi ]
SCJD 1.5<br />SCJP 1.5
SCBCD - SCWCD - SCJD - SCJP - OCA
Originally posted by Musab Al-Rawi:
Sorry for the late reply,
I am not sure if I got your question right or not.
What I understood from my assignment is isLocked() should return true if the record has been locked by the invoking thread, if this method returns false then you shouldn't allow the thread to do update/delete kind of operations.
Another thing to mention is that when you want to lock a record all what you need to do is call the lock() if the record is locked by another thread (user) then your thread should wait until the locking thread unlock() the record.
You don't have to lock on a specific record (as it may be more complicated to implement) UNLESS it is required , i.e say thread 1 is locking record 1 then thread 2 should be able to lock and change recrod 2 if it is not locked. an easier approach is: if thread 1 is locking record 1 then thread 2 can't lock record 2 until thread 1 unlocks record 1.
final thing to mention is always try to follow the following style when locking:
to make sure that the record is unlocked whether the operation fails or succeeds.
SCJD 1.5<br />SCJP 1.5
SCBCD - SCWCD - SCJD - SCJP - OCA
Originally posted by Musab Al-Rawi:
Alex's way is better but in order for you to do it you have to provide a higher layer which is the business layer.
The business layer provide the main functionalities from the client point of view, so the client won't have to invoke lock, update then unlock to do the booking operation, instead he will invoke book() which does the locking updating then unlocking.
SCJD 1.5<br />SCJP 1.5
Originally posted by R van Vliet:
By the way, for those that delegated locking to a seperate class (say LockManager), does anyone guard against things such as :
Since most operations requiring record lock are atomic and are guaranteed to unlock the record before returning this isn't an implementation problem, but looking solely at the scope of LockManager wouldnt it be better to disallow a thread locking the same record twice? I havent noticed anyone doing or discussing this, hence asking.
SCJD 1.5<br />SCJP 1.5
Originally posted by conny pemfors:
what I believe or think in my mind, is that you always should check if the record is already locked, in that case, that kind of scenario would not be any problem, can't lock a locked record.
when you do the lock operation, check first it is not already locked.
so if you have many clients connected, it always checks before whether the recordNo is locked by another user.
Conny
I personally think you need to be aware of what is stated in your instructions, as well as what is reasonable - the 2 may conflict.Originally posted by R van Vliet:
[...] if you look at my little code sample, the same thread attempts to lock a record twice. If you then follow the "normal" locking approach, the following happens :
T1 : lockRecord(1);
T1 : record not locked, so acquire lock and return cookie
T1 : lockRecord(1);
T1 : record locked, so let T1 wait for unlock of record 1
The problem here ofcourse is that T1 is the thread that's supposed to do the unlocking, which it cant because it's not in WAITING[...]
Remember if you are using RMI and you are calling the lock() update() and unlock() methods all through RMI then you will not be able to use the thread to uniquely identify the client. (Not everyone (or even the majority) agrees with me that you should call all 3 methods from the client though ).Originally posted by R van Vliet:The only safeguard against this is keeping track of which thread owns a lock along with which client (represented in my assignment by a cookie). Then if one thread tries to lock a record twice the second time will simply be ignored (meaning it will return the same cookie as the first attempt and not block).
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by R van Vliet:
Hi Andrew,
Thanks for your reply and I agree, although I think in the case of the 2nd instructions the safer bet is to let the code follow the instructions to the letter (avoiding the woes of assessors that got a speeding ticket that morning) and document your reservations in choices.txt.
You make a good point about RMI. Although perhaps even in the case of RMI tracking the thread would be a viable option. I wouldnt use the thread to uniquely identify a client to begin with, but it's still a way to avoid the deadlock situation mentioned. All you'd basically care about is one thread trying to lock record X twice. After that check you can always seperately check if you're talking to the same client and deal with the request to lock appropriately, still avoiding the deadlock in the process.
I almost feel a need to apologise for the fact that I too belong to that majority that objects calling lock/unlock from the client
SCJD 1.5<br />SCJP 1.5
Heh - no problems. For what it's worth, I also object to calling lock/unlock from the client. However I believe that this is what is required by the instructions.Originally posted by R van Vliet:
I almost feel a need to apologise for the fact that I too belong to that majority that objects calling lock/unlock from the client
I deliberately commented on your responses out of order since your comment on using threads as identifiers in RMI only makes sense in the case where you have the entire booking method server side. In that case you probably don't need any other checks - the thread should be able to indicate ownership. However if you have the ability to call lock from client side, then you cannot use the thread as an identifier - thread reuse in RMI could result in different clients using the same thread.Originally posted by R van Vliet:
You make a good point about RMI. Although perhaps even in the case of RMI tracking the thread would be a viable option. I wouldnt use the thread to uniquely identify a client to begin with, but it's still a way to avoid the deadlock situation mentioned. All you'd basically care about is one thread trying to lock record X twice. After that check you can always seperately check if you're talking to the same client and deal with the request to lock appropriately, still avoiding the deadlock in the process.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by conny pemfors:
Anybody who know for sure how well documented choices.txt should be.
Somehow it must be explained in detail of why certain choices have been made regarding record locking for instance, or other issues.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by Andrew Monkhouse:
Heh - no problems. For what it's worth, I also object to calling lock/unlock from the client. However I believe that this is what is required by the instructions.
Originally posted by Andrew Monkhouse:
I deliberately commented on your responses out of order since your comment on using threads as identifiers in RMI only makes sense in the case where you have the entire booking method server side. In that case you probably don't need any other checks - the thread should be able to indicate ownership. However if you have the ability to call lock from client side, then you cannot use the thread as an identifier - thread reuse in RMI could result in different clients using the same thread.
Regards, Andrew
Yes - I feel that the interface you have been asked to implement should be accessible client side. This only applies to the newer assignments - the older (Fly By Night) assignments did not have an interface to implement. As for my logic as to why I believe this, take a look at this topic. It is rather lengthy, and I expanded on my ideas as I went, but I think I make a valid case for the instructions requiring the interface to be exposed to the client.Originally posted by Andrew Monkhouse:
Heh - no problems. For what it's worth, I also object to calling lock/unlock from the client. However I believe that this is what is required by the instructions.
Originally posted by R van Vliet:
Could you elaborate on this? Do you mean the instructions imply the data access interface (DBAccess in my case) should be the interface that is optionally remote? And do you feel this way regarding all current assignments or just the (i assume significantly older) one you implemented? I've always assumed the DBAccess interface simply defined an interface for the data layer rather than the business layer.
The RMI specification is quite explicit in saying that it refuses to define what anything to do with how threading is handled on the server. So your nasty scenario is allowed according to the specification.Originally posted by R van Vliet:
P.S. I'm assuming RMI works with a thread pool of some sort, so that two seperate remote method calls could be executed by the same RMI thread. I couldnt google anything useful on this on short notice, but if this isn't so perhaps my nasty scenario cant even happen.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by Andrew Monkhouse:
However, as that thread shows, there are opposing arguments. And it certainly appears that assessors are not failing people for only exposing business methods over the network. So the argument is kind of academic.
Originally posted by Andrew Monkhouse:
The RMI specification is quite explicit in saying that it refuses to define what anything to do with how threading is handled on the server. So your nasty scenario is allowed according to the specification.
I have tested this on multiple versions of the Sun JDK up to version 1.5, and I have been able to confirm the scenario you mention. I also just tested it with the Apple JDK, and with 2 clients connecting to the server 12 times each, I found that for my particular code (which I will admit is designed to prove this problem ) there were only 3 threads used. One thread got used 8 times by each of the 2 clients, another thread got used 3 times by each of the 2 clients, and another thread got used once by each of the 2 clients. If you are really interested in the proof, let me know and I will post the code, however at 67 lines long it may be a bit of overkill to post the code just to prove a problem that wont affect anyone only exposing business objects to the client.
Regards, Andrew
Originally posted by Alex Belisle Turcot:
Hi,
I'm not there yet.. in 2 or 3 weeks I believe..
This has been ask many times and there are few very good thread where people detail what they did...
I read some say it should not be much more than 1 page. Maybe 2-3 top.
Look it up yourself on the board, you'll find a lot of people shared their experience about their choices.txt
Regards,
Alex
[ November 20, 2007: Message edited by: Alex Belisle Turcot ]
SCJD 1.5<br />SCJP 1.5
Originally posted by R van Vliet:
Hi conny,
I was unable to extract a question from your post. Could you please be more specific as to what you are confused about?
Also, on a sidenote, it's much easier to read code if you use the appropriate code tag.
SCJD 1.5<br />SCJP 1.5
Originally posted by R van Vliet:
So would you say this means that all assignments that use RMI and expose the lock methods to the clients without guarding against this specific scenario are broken?
Originally posted by R van Vliet:
I dont have the code included with the book at hand, but does it guard against this issue?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Originally posted by conny pemfors:
it's the part where you use threads,
I might have another solution that looks like this, but I don't kn ow if it is correct but it seems to work.
used during update a record.
Now I use Reentrant lock as well in reservationsmanager.
I suppose it depends on the implemention of equals(), but if it checks if recNo and the data strings are equal and setData does nothing more than this.data = data I dont think this can actually happen.
The else clause then has the exact opposite problem. Provided your record locking system works it cannot happen that your record is being modified after the record was locked, so checking for this is a bit redundant. Ofcourse, ignore that comment if the else-log line is for debugging purposes only or if you allow other parts of the application to directly modify your value object (Record) without checking for a lock on that record. The latter would be something I'd avoid, the Record set..() methods should have package visibility only really.
Anyway, I hope this clears one or two things up. If you have a more specific question feel free to post it and I'll try to be more specific if I can
Originally posted by Andrew Monkhouse:
I don't have time to look into Terry's code at the moment (he wrote the Sockets and RMI chapters), but from memory we guard against this.
Mario Rodriguez Jaen
SCJP1.4, SCJD1.4
Unfortunately it does not handle the case where a single client calls the (reentrant) lock twice, but happens to use a different thread for the second call. This is very plausible with RMI.Originally posted by MarioAixel Rodriguez Jaen:
You guys should check the Lock interface in Java 6. It provides with a very useful object, ReentrantLock, that fixes all those problems [...]
Originally posted by MarioAixel Rodriguez Jaen:
and you could be forced to watch Titanic again =)
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog