aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Order of Checks in Update & Delete methods 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 "Order of Checks in Update & Delete methods" Watch "Order of Checks in Update & Delete methods" New topic
Author

Order of Checks in Update & Delete methods

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

As part of my Update & Delete methods I carry out a few checks. I have the assignment that contains a lockCookie in the DB interface. Two of these checks that I carry out are:

* Check if the record exists.
* Check if the record is locked.

What order do you guys think these checks should be carried out in?

I store my records in a map (key = record number, value = record). I store my locked records in a map (key = record number, value = lock cookie).


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

What do you think yourself?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

My question was what others thought, I know what I think as I've already implemented it
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

It's one of the most crucial parts of the assignment, so I'm not going to spoil the fun of all other ranchers pursuing this certification and leave this question unanswered (so they can find out themselves through logical thinking and/or testing)
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

I would say that carrying out these checks is crucial before executing the operation. But why do you think that the order of these checks is crucial?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Because if not executed correctly your application will deadlock
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

I don't see how the order can cause deadlock. Both checks are carried out inside a single synchronized block of code. Failure of either check should case the method to return (by throwing an exception). So where's the possibility for deadlock?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

Sean Keane wrote:where's the possibility for deadlock?

My mistake, I was thinking about the lock-method itself, but indeed no deadlock possibility in update/delete.

As for your question: I only have a check to see if the record is locked, no check for an existing record (as that one is performed by the lock-method).
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

I've three checks:

1) Check if the record exists (throws a RecordNotFoundException)
2) Check if the record is locked (throws a SecurityException - telling the user to lock the record before carrying out the operation)
3) Check if the record is locked by the current user (throws a SecurityException - telling the user that they are trying to carry out an operation on a record that is locked by another user)

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5408
    
  13

I just have 2 and 3. If 1 occurs your lock-method has a flaw, because you have locked a record which does not exist
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

I'm thinking of this in terms of validating the arguments to my lock method. I've two arguments - record number & lock cookie. If I do (1) first then that validates the record number argument i.e. if that fails, then there's no point in checking whether that record is locked, because it doesn't exist.
 
Consider Paul's rocket mass heater.
 
subject: Order of Checks in Update & Delete methods