Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.
SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Should I need to check whether locking and unlocking an deleted/non-exists record??
If so, the locking code will be very complicated,
while some deleted records cannot be unlocked.
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
In your API documentation for the lock() method, do you have the instruction "Locks a record so that it can only be updated or deleted by this client."? If so, how are you ensuring that this is achieved?
Not necessarily: you could add a method to verify if a record is valid, and just call that common method from your lock() and unlock() methods. This will keep your code clean and easy to read, and is a good practice.
To use this, the client should only call "lock() ... delete()". The client should not call the unlock() method when doing a delete.
SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Even if the current thread checks again the validity of a record after it's wait() and before it's lock(), other thread can remove it in the mean time.
SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
In data.delete(recNo), since I synchronized the RandomAccessFile, that means, no other I/O can perform, then it first check the validity of the record. If the record is valid, then it will be deleted, otherwise, RecordNotFoundException thrown. The data.unlock(recNo) removes the lock from the locker.
If we do not synchronize RAF, the case that, the record is valid during the check, but is removed by other thread while the current thread is waiting for the lock. Even if the current thread checks again the validity of a record after it's wait() and before it's lock(), other thread can remove it in the mean time.
By the way, should I provided create(), delete() functions to my wrapper class DataServer? At first, I do not have these functions becos the client will not perform such functions. But I feel it is very inconvenient to test the functions in Data without these (Since my test class actually need to implement those methods for testing purposes).
Should I provided them in my submission, or I should remove them after the testing?
In addition, I wanna ask, what should the unlock() checks?
1. Check whether the record is valid (deleted) in the data file? OR
2. Check whether the record is valid (deleted) in the dataLocker?
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |