aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S Version 2.3.3 locking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S Version 2.3.3 locking" Watch "B&S Version 2.3.3 locking" New topic
Author

B&S Version 2.3.3 locking

Ulas Onder
Greenhorn

Joined: Mar 15, 2004
Posts: 4
Hi guys,
I read a lot about the locking problem from previous topics, but i could not find the answer to my problem.
Below is the DBMain interface from my instructons:

Below is my solution foor lock/isLocked/unlock and delete methods. (In delete method, i have bypassed the unlock method because once the record is deleted, unlock method throws an exception.)

I'm aware that my solution is a little bit strange, but i could not think of another way because of 'RecordNotFoundException' thrown from each method. I could have written the code like this in order to fit in this locking contract. Do you have any idea that my code is acceptable or not?
Have you thought of a similar approach?
Ulas Onder
SCJP, SCWCD
[Andrew: removed islocked() and unlock() methods]
[ March 15, 2004: Message edited by: Andrew Monkhouse ]
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Hi Ulas,
Welcome to this forum.
Originally posted by Ulas Onder:
I'm aware that my solution is a little bit strange, but i could not think of another way because of 'RecordNotFoundException' thrown from each method. I could have written the code like this in order to fit in this locking contract. Do you have any idea that my code is acceptable or not?
Have you thought of a similar approach?

I think you may be missing a requirement. All three methods are declared to throw RecordNotFoundException and according to the assignment instructions:

Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.

But the way these methods are written they would allow me to lock(-12345) even when there isn't a record number -12345. Also, as written they don't throw RecordNotFoundException under any circumstances. So I think you're not satisfying that requirement. For example, if I tried to delete an already deleted record your delete method (as written) would not throw a RecordNotFoundException as I think it should. Other than not handling the RecordNotFoundException the code looks good.


Regards, George
SCJP, SCJD, SCWCD, SCBCD
Mehmet Atlihan
Greenhorn

Joined: Mar 03, 2004
Posts: 20
Hi Ulas
I agree with George your delete method should throw RecordNotFoundException
if an attempt is made to delete an already deleted record.
What I did was to add a try catch block in my unlock method and listen
for RecordNotFoundExceptions.Then I search for the exceptions message ie
"Record Is Deleted". If that is the case the program keeps on execution
othwerwise throws RecordNotFoundException. You can take a similar approach and listen for your Deleted Record message whatever it is.
Logically speaking since its not possible to lock a deleted record (assuming your lock method validation works correctly)
that indicates an unlock call after a delete operation so it is safe to let the application run and remove the lock over the deleted record.
I hope this helps
Mehmet
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

Hi Ulas,
I have removed some of your posted code, as we do not allow major sections of the assignment to be posted. The locking code is worth 20% of the assignment, and this is far too much to post in total. I have left the lock() method and the delete() method as points for people to discuss. See the question "What is the policy on posting questions I saw on the exam / details of how to do the assignment?" in the JavaRanch SCJD FAQ for more details.
In your lock() method I strongly recommend you do not swallow the InterruptedException - you should as a bare minimum be logging it. You should also consider what it means if your thread does get interrupted, and what state it is in at that point - can you really continue processing (as you are doing) or should you do something else?
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Javini Javono
Ranch Hand

Joined: Dec 03, 2003
Posts: 286
Hi,
George said above:

But the way these methods are written they would allow me to lock(-12345) even
when there isn't a record number -12345.

Hi, George, and everyone,
My LockManager allows the client to lock any record number >= 0,
including record 200,000 (even if this record does not exist).
To lock record 200,000 when it does not exist is, of course, irresponsible
behavior on the client's part.
Is my design incorrect? Or will it be okay if I document that client's are not
allowed to lock arbitrary record numbers?
Thanks,
Javini Javono
George Marinkovich
Ranch Hand

Joined: Apr 15, 2003
Posts: 619
Hi Javini,
If your assignment instructions state:

// Locks a record so that it can only be updated or deleted by this client.
// If the specified record is already locked, the current thread gives up
// the CPU and consumes no CPU cycles until the record is unlocked.public void lock(int recNo) throws RecordNotFoundException;

And if they also state:

Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.


Then assuming that recNo = 12345 doesn't exist in your database file, don't you think that the examiner is going to be surprised if the following call doesn't throw a RecordNotFoundException:

Under what circumstances are you throwing RecordNotFoundException?
Now if my intention in writing the assignment instructions were to get you to throw the RecordNotFoundException when the user tries to lock a record number that doesn't exist, how could I have better communicated that intention than in the assignment instruction quotations above?
If that is the intention of the assignment instructions (and I don't see how another interpretation is possible) then it would be awfully easy for the examiner to determine whether I honored the contract specificed in the interface method comment.
[ March 15, 2004: Message edited by: George Marinkovich ]
Ulas Onder
Greenhorn

Joined: Mar 15, 2004
Posts: 4
Thanks guys,
I put only a part of the delete method, it throws a RNFE, but not in the section i've posted.
Ulas Onder
SCJP, SCWCD
Javini Javono
Ranch Hand

Joined: Dec 03, 2003
Posts: 286
Originally posted by George Marinkovich:
Hi Javini,
If your assignment instructions state:

Under what circumstances are you throwing RecordNotFoundException?
Now if my intention in writing the assignment instructions were to get you to throw the RecordNotFoundException when the user tries to lock a record number that doesn't exist, how could I have better communicated that intention than in the assignment instruction quotations above?
If that is the intention of the assignment instructions (and I don't see how another interpretation is possible) then it would be awfully easy for the examiner to determine whether I honored the contract specificed in the interface method comment.
[ March 15, 2004: Message edited by: George Marinkovich ]


Hi,
Thanks for your response, George, and thanks for unambiguously clarifying this point.
I will modify my LockManager so that the client is not allowed to lock and unlock
records which do not currently exist within the database (but, my LockManager will
allow the server to carry out this "magic").
Thanks,
Javini Javono
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: B&S Version 2.3.3 locking