Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Order of Checks in Update & Delete methods

 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Roel De Nijs
Sheriff
Posts: 9933
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think yourself?
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question was what others thought, I know what I think as I've already implemented it
 
Roel De Nijs
Sheriff
Posts: 9933
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9933
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because if not executed correctly your application will deadlock
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9933
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9933
113
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic