• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Question about reuse a deleted record

 
HaoZhe Xu
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does it mean when I add a new record, the program should check if there is any deleted record, if there is, the new record can overwrite the deleted one since the length of each record is exactly the same, if there is not, then add a new record at the end of the file?
But when to throw the DuplicateKeyException??? Maybe when a deleted record can be used? Or there is already a record which name & location are same to the new one?
 
Jacques Bosch
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there.

does it mean when I add a new record, the program should check if there is any deleted record, if there is, the new record can overwrite the deleted one since the length of each record is exactly the same, if there is not, then add a new record at the end of the file?

Yes. That is correct.

But when to throw the DuplicateKeyException??? Maybe when a deleted record can be used? Or there is already a record which name & location are same to the new one?

Good question. There are lots of threads discussing this issue. Do a search for DuplicateKeyException.
I still need to decide what I'm going to do with it.
 
Alison Carter
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have been thinking about these issues too.
Originally posted by HaoZhe XU:
does it mean when I add a new record, the program should check if there is any deleted record, if there is, the new record can overwrite the deleted one since the length of each record is exactly the same, if there is not, then add a new record at the end of the file?

I agree with the above.

But when to throw the DuplicateKeyException??? Maybe when a deleted record can be used? Or there is already a record which name & location are same to the new one?

IMO the DuplicateKeyException should be thrown when you try to create a new record that equals an existing one. My spec (B&S Contractors) doesn't specify what makes two records equal. I think it is up to you to decide and document your assumptions.
Alison Carter
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi HaoZhe,
I concur with Jacques and Alison.
Regarding the meaning of DuplicateKeyException, I have the following thoughts:
DuplicateKeyException is only meaningful in the context of the create method.
DuplicateKeyException is thrown when the user tries to create a duplicate of a record that already exists in the database.
So the $60,000 question is, what constitues a duplicate record?
One can analyze the provided database and draw some conclusions about what constitutes a key. The minimal candidate key seems to be: name and location. That is, the same contractor seems to be able to be located in multiple different locations. But given the data that exists in the provided file, there does not seem to be a case where the contractor name and the location name of two records are the same. So, based on the limited evidence of the supplied database file, it seems that name and location are the duplicate key fields. Other people have drawn different conclusions. Some people have argued that because the duplicate key definition is not clear in the specification that there is no known circumstance under which it is appropriate to throw the exception.
As in most things related to the exam, if you have a reasonable basis for drawing your conclusion, document related assumptions in the design choices document, and implement the resulting solution well, you should be OK.
Hope this helps,
George
 
Xie Ruchang
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi George,
Your consideration may be valid for Bodgitt and Scarper specification dealing with contractors, but with URLyBird specification dealing with accomodation (hotel room), even you take all the fields in the record together, it is still not unique.
So to me the DuplicateKeyException does not make "business" sense at all. Because it is given in the specification, I implement it by using it to wrap an IOException or whatever expcetions which may occur in the process of writing to the random access file.
Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic