Locks a record so that it can only be updated or deleted by this client.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
I want the WeakHashMap to release the lock automatically when a client's connection broken after locking. But when I lock a record from a client, after the lock() finished, the WeakHashMap release the lock immediately
A locked record surely should not be updated by other client.
Besides, with the above interface, can you give me some suggestion to check the locker.
Should I make the Data class as a member of LockManager and let the Data do the lock and the LockManager produce and return cookie?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Should I make the Data class as a member of LockManager and let the Data do the lock and the LockManager produce and return cookie?
regards
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Client programs can reserve DVDs by using the reserveDVD(string upc) method and unreserve them by using releaseDVD(string upc). It is assumed that reservations are meant to be very short lived. Thus, an attempt to reserve a DVD blocks until an exclusive lock on that DVD is achieved. It is also important to notice that there is an implicit contract between all operations that modify the database. Then will first reserve the UPC in question, then modify it, then unreserve it. This is the protocol that will guarantee that different threads are not interfering with eachother as they attempt to modify potentially the same DVD record at the same time. Further it is assumed that no attempt will be made by any thread to release a DVD that was not locked by that thread.
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Originally posted by Bharat Ruparel:
Hello Andrew/Vlad,
I need your help in this. I am not sure whether this is right or wrong.
Thanks.
Bharat
p.s. It would be great if Max will answer these questions once in a while, afterall we are all referring to his book constantly.
Basically, you have three options. First, you can externally enforce an order on the clients: this is what the external lockmanager does. Second, you can only allow a single client to be active at any given time: by active, I mean, lock, modify, unlock. Third, you can use a static or class-global structure like a HashMap or a WeakHashMap to track, as a member variable of the Data class, which clients own which locks. An interal LockManager also works here, and is also an elegent solution. In principle, this is similar to the way the static Vector in the book works, but it uses a Map so it can track two coordinated obects: the client ref and the locked record number. Does that help?
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Do you mean I should have a single Data instance for all the connections (not one instance for one connection)?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
but if use alone mode, there are no lock() and unlock()
should I keep two diffrent version of update() and delete() ln local and remote mode?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
If this is still true and there is only one Data instance per table for all clients, is there another way in which we can do the locking in the Data class without changing the signature of the lock and unlock method's.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Hi Bharat,
Like Andrew pointed out above, you can have a unique Data instance per client. Then, you can lock using Data.this. Since the hashmap inside of Data is static, each Data instance will be awarded a unique lock. Alternately, you can use the cookie. Make sense?
M
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
it worries me that I may be oversimplifying things
it required so few lines to be modified to get it right
Is it OK if I post my modified code here to run it by you and Andrew?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Do you have any requirement to lock the entire database (with -1)? If so, are you handling it?
In your Data.lock(), why do you have the psuedocode 'In a while loop check if the WeakHashMap contains "this" key"'?
Are you checking whether a client has already got a record locked? If so, is the wait indicated on the next line appropriate? Under what conditions could it be woken up and have the situation change? Assuming you do have answers for those questions, why are you using a while loop to check for the "this" key? Is there a method in Map that will give you this information without the loop?
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
The only issue I would now raise is whether your use of RecordNotFoundException is correct. To me RecordNotFoundException indicates that the record cannot be found, therefore it cannot be locked. This might happen if the user specified a record number out of range, or if the record in question has been deleted. You do not appear to check for either condition.
The other issue is with the InterruptedException - should this be rethrown as a RecordNotFoundException? There have been arguments here both for and against that in the past, so as long as you are willing to justify why you are doing this you could be OK.
Personally I agree with Tony: InterruptedException should never occur and should not need to be supported. Using an assertion as Tony did makes it clear to the junior programmer that you don't believe that it needs to be supported.
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Regards, Richard
The lockRecords looks as static class member,I don't know the reason why it belongs to WeakHashMap object.
Why don't use other class to store records other than WeakHashMap?
Could you please introduce its main functions?
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
The only issue I would now raise is whether your use of RecordNotFoundException is correct. To me RecordNotFoundException indicates that the record cannot be found, therefore it cannot be locked. This might happen if the user specified a record number out of range, or if the record in question has been deleted. You do not appear to check for either condition.
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Also, I am not sure that I should be calling this [validateRecord(int recNo)] method from unlock and islocked methods? They both also throw RecordNotFoundException? For that matter, so do read, update, delete methods. I am a bit unsure of why Sun has those methods throwing RecordNotFoundExceptions?
I am a bit unsure of:
1. When to assert false?
2. When to throw a FatalSystemException as you are suggesting above? Are you hinting that I should be throwing a RuntimeException and chaining it upwards here?
3. You and Andrew both are saying that don't wrap this exception in RecordFoundException and rethrow it?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|