• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DuplicateKeyException

 
bao do
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Would anyone know when DuplicateKeyException is supposed to be thrown? The requirements only says that it's thrown in DBAccess.createRecord(String[] data) but didn't say when it's thrown. From the name, I assume that it's thrown when there's an existing record(in database) that matches exactly with the record to be inserted by client. Is this right ?
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Originally posted by bao do:
Hello,
Would anyone know when DuplicateKeyException is supposed to be thrown? The requirements only says that it's thrown in DBAccess.createRecord(String[] data) but didn't say when it's thrown.


In your creation logic, instead of adding a record that would create a duplicate key, you should/must throw this exception to tell the user.


From the name, I assume that it's thrown when there's an existing record(in database) that matches exactly with the record to be inserted by client. Is this right ?


For most assignment (most probably all of them, but check your own requirements), you are free to decide what is your unique key in your database. Could be all the fields of a record, or name+location, or the record number (my choice). If you choose "name+location", it is quite possible that the data provided would create a duplicate key in your database. Using the record number makes it virtually impossible, so I don't throw it but documented it.

Regards,
Alex
[ January 07, 2008: Message edited by: Alex Belisle Turcot ]
 
bao do
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

By doing your way, wouldn't we still have duplicate records if user decides to update an existing record with the same info as another existing record ? This is why I'm confused that updateRecord does not throw duplicate key exception also.

Bao
 
Oguz Ozun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bao do:
Hi,

By doing your way, wouldn't we still have duplicate records if user decides to update an existing record with the same info as another existing record ? This is why I'm confused that updateRecord does not throw duplicate key exception also.

Bao


Hi,

Duplication is based on your assumption about the key. If your key is record number, then 2 records having the same data could be different because of their different record numbers.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic