wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock the the entire database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock the the entire database" Watch "lock the the entire database" New topic
Author

lock the the entire database

yi lu
Greenhorn

Joined: Dec 26, 2003
Posts: 18
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?
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
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


Regards, George
SCJP, SCJD, SCWCD, SCBCD
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
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.
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
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
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
George,
I am in the same boat. I was done 2 months ago. But I just keep making
change after change.
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
I had the same problem as Bill mentions of making change after change after change ...
I too spent probably 2 months more on this than I should of, mostly due to automatic failure paranoia.


kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11437
    
  87

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: lock the the entire database