I am reading "The Sun Certified Java Developer Exam with J2SE 1.4". It gives examples for a lock and unlock mechanism (reserveDVD, releaseDVD) which don't fulfill this part of my exam instructions: "if an attempt is made to unlock a record that has not been locked by this connection, then no action is to be taken." What am I missing here? (I am using RMI.) Thanks, Tom
Hi Tom, I don't know the book you are referring to, so I can answer your question only partially. However, the instructions say that only the connection that has locked a record may unlock it. This means in the unlock() method you have to check the connection identity. For example: Connection 1 locks record n. Connection 2 attempts to release record n. Result: Record n remains locked, because only connection 1 can release it. Secondly: Stick to your instructions. It seems that there are different assignments around, the instructions of which differ slightly from each other! Hope this helps.
How should we identify the clients such that we know who obtained the lock in the first place? Should each user be asked to enter an ID, or should the UI create some sore of UUID upon start up? Perhaps UUID=hostname + currentTime + randomNumber. Any thoughts? Thanks! --Dave.
Joined: Apr 07, 2002
Hi Dave, there are many solutions to this issue (and most of them have been discussed extensively in this forum). Here is how I did it: I had two sets of locking and unlocking methods. The first set required the record number plus an identifying object. The second set required only the record number and passed it on to the first set of methods, together with the current thread as identifying object. However, the RMI specification explicitely says that you cannot rely on getting the same thread in subsequent method calls. For this reason I specified that the methods requiring only the record number should only be used for locking and unlocking within a single remote call. The second set should be used when more than one remote call is involved. Hope this helps.
Originally posted by Tom Elix: I am reading "The Sun Certified Java Developer Exam with J2SE 1.4". It gives examples for a lock and unlock mechanism (reserveDVD, releaseDVD) which don't fulfill this part of my exam instructions: "if an attempt is made to unlock a record that has not been locked by this connection, then no action is to be taken." What am I missing here? (I am using RMI.) Thanks, Tom
Hi Tom, I may be able to help, since I wrote the book you're talking about. You're correct, the solution offered in the book is not an exact match for the solutions in the SCJD: we wanted to provide principles, not actual, literal answers. To be honest, we were afraid that we were too close as it was . That being said, I think you'll find that modifying that part of test will be trivial. Try using a map instead of a vector, and track your clients, either by assignning them a ticket number or keeping track of thier thread, and you'll be where you want to be. All best, M, author The Sun Certified Java Developer Exam with J2SE 1.4