in the instruction: The lock method should block until the requested lock can be applied. The integer argument indicates the record to be locked. If the method is called with an argument of -1, the entire database should be locked. in which situation should I lock the the entire database?
Originally posted by yi lu: in which situation should I lock the the entire database?
You might want to lock the entire database under the following circumstances: when calling the find method, or when calling the create method. Both of these methods operate in some sense on the entire database. Without locking the entire database when doing a find, it is possible that you will "find" a record that is in the process of being deleted, for example. In the case of a create, you may look through the entire database to find a record marked for deletion. Without locking the entire database when doing such a search, you may miss a potential deleted record (candidate record to be replaced by the new record) or pick the same deleted record that another create method is reusing at the same time. As you cannot tell which record in the database you may want to reuse, you cannot know a priori which record to lock; the entire database must be locked in this situation. I don't think you actually need to use the database lock in your client code, but I do think you should provide the mechanism in the Data class for completeness. If you don't use the database lock while calling the find method then you open up the possibility of dirty reads. You might come to the decision that dirty reads are acceptable in which case you should discuss why in the design choices document. Finally, you should be careful to think out all the implications of a database lock on your lock and unlock methods. For example, if there's a database lock, can any other record be locked? If any individual record is locked, can the database lock be obtained? A database lock complicates your locking scheme somewhat but you should do so if you think it is necessary. Hope this helps, George
I am not aware of anyone who locks the entire database file. Instead you can have Data hold a handle to your file manipulation class and this reference is a singleton. And the only way to get to the handle is through sycnchronized code. Thus you are guaranteed that one and only one client can manipulate or changes the file at once. I know many people who used this approach and passed easily.
Joined: Apr 15, 2003
Bill, I hadn't thought of the solution you offered and at first glance it looks good to me. It will take a little while for me to digest completely, but at present it appears to me to be better than my existing solution. Not being able to upload my assignment due to the certmanager problem is the worst kind of limbo. I'm done, but I'm not really done since I can still make changes. I'll be much happier when the blasted thing is uploaded and out of my hands. Anyway, thanks for the idea. Happy New Year, George
Joined: Mar 21, 2003
George, I am in the same boat. I was done 2 months ago. But I just keep making change after change.
Hi Yi The only time I locked the entire database was when I was shutting down - it did mean that shutting down could block until there were no individual locks allocated, but it did ensure that the database did not shut down in the middle of an update. Regards, Andrew