• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DuplicateKeyException

 
Keith Jones
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our create methods are supposed to throw this but there is no key parameter that the create method takes and furthermore the key is the record number and there is no way that we can have two of those since all we need to do is append a record to the end of the file and hey presto we have a new key number.

Anyone have any ideas about this?
 
Mark Smyth
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Keith Jones:
Our create methods are supposed to throw this but there is no key parameter that the create method takes and furthermore the key is the record number and there is no way that we can have two of those since all we need to do is append a record to the end of the file and hey presto we have a new key number.

Anyone have any ideas about this?


Yeah there is no key so this exception can never be thrown as the datafile currently stands. What I hav done is to drop the exception from my implementation of the DBMain interface.



You should use a reference of type DBMain instead of type Data in the client code that uses Data. You then make provision to handle the DuplicateKeyException as normal, even though in the current implementation of DBMain it can never be thrown. This ensures that the client code will not need to change should a later implementation of DBMain have reason to throw a DuplicateKeyException such as by adding in a recordID field.


[ November 22, 2006: Message edited by: Mark Smyth ]
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just because it's in the throws clause doesn't mean it must be thrown
The exception is declared for future use and other database implementations.

And you don't just need to append new records, you should (if you want to do a good job at least) reuse space in the file taken up by deleted records.
 
Keith Jones
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course I know that we can reuse deleted records but I thought it would be better to just append because then we have a history of deleted items for reporting purposes. Besides neither option is wrong.
 
Chulwoo Choi
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course I know that we can reuse deleted records but I thought it would be better to just append because then we have a history of deleted items for reporting purposes. Besides neither option is wrong.


I also chose to do so. And as Jeroen said (in another post):
Whatever you decide, as always, document it

to defend your choices.

Regards,
Chulwoo
 
Keith Jones
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic