wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes create method and DuplicateKeyException 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 "create method and DuplicateKeyException" Watch "create method and DuplicateKeyException" New topic
Author

create method and DuplicateKeyException

Colin Duggan
Ranch Hand

Joined: Feb 23, 2008
Posts: 41
My create method does not throw a DuplicateKeyException as it assigns new record number inside the create method and it does not enforce any primary key on the records fields. I know alot of people have taken this approach and just left the method signature as is and documented in their choices.txt.

For my update/delete and unlock methods i have overriden the signatures that were supplied in the DB interface due to issues i see as violations in how the API is used. So for example i have removed the RecordNotFoundException from the unlock method as it is possible that a thread who has the lock on a record may delete it.

Is there a case to do the same for the create method and override its signature so that it does not throw the DuplicateKeyException? The only issue i can see with this is that maybe in the future if the underlying database is changed then this method may need to throw that exception. Any thoughts? Thanks
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Colin Duggan wrote:The only issue i can see with this is that maybe in the future if the underlying database is changed then this method may need to throw that exception. Any thoughts?

And that's exactly the reason why I would not override this method. With update/delete/unlock methods it makes sense, because this exception will never be thrown from these methods. That's not guaranteed with the create-method. Just my 2 cents.


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

Joined: Feb 23, 2008
Posts: 41
Roman Burdakov
Greenhorn

Joined: Mar 20, 2011
Posts: 10

Hi folks,

I just registered for the exam last week, and did a little bit of work on past weekends. I was reading this forum to see how people solved their problem and I'm not quite sure why you shouldn't throw DuplicateKeyException? I wrote a unit test where I'm trying to add two valid records which have same name and specialties and test should expect DuplicateKeyException be thrown. If I understand task correct, we should enforce entity integrity by checking, if combination of fields which creates primary key already exist in database. So we have to prevent adding records with same primary keys. It's not a rocket science to do, and it makes perfect sense for me. Am I missing something?

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5131
    
  12

Roman Burdakov wrote:Am I missing something?

No When you have the contractors assignment it makes sense to throw a DuplicateKeyException, when you have the hotel rooms assignment it makes no sense at all.
Roman Burdakov
Greenhorn

Joined: Mar 20, 2011
Posts: 10

Ok. Thanks for the clarification.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: create method and DuplicateKeyException
 
Similar Threads
B&S: Primary Key
Unneeded exception?
Locking strategy URLyBird 1.1.1 with ReentrantLock
NX: URLyBird - lock-delete-unlock and RecordNotFoundException
No definition of what is a DuplicateKeyException