SCJP, SCJD
Question 1 : Now why do we have to pass the lock cookie into the update method? How the update method should handle the lock cookie inside its method definition?
5) Meanwhile, Client B wants to update record number 1 as well.
6) Client B checks the static hashmap, and find out that the record number is mapped . It means the record number is locked. (At this point, we don't need the lock cookie yet, since we can use hashmap.containsKey(Object key)).
7) Then client B has to determine which client is locking the record with recNum 1. Because if it's client B which is locking the record, then Client B will not need to wait until the record is unlocked. However if other client is locking the record, then I believe the lock cookie comes into use.
Question 2 : How client B knows that the lock cookie belongs to a Client others that Client B (in this case Client A). Based on my illustration, Client B hasn't generated a lock cookie yet. Even if it had,
then how client B store the cookie in its member so it can be compared with the cookie in the hashmap?
ps: using this approach means that in the unlock method, the entry with the associated record number must be removed from the static hashmap to indicate the record is now free.
ps2: from my illustration, I assume that a lock cookie is associated to a particular client. So if there are 5 clients trying to update a record, then 5 cookie values will be generated. Is my assumption correct?
But now I am confused with the SecurityException. The spec says that security exception is thrown if the record is locked with a cookie other than lock cookie. It means that in the process method, lock cookie must be compared with a value type long, and both must have same value.