• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: DuplicateKeyException

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My db Interface:
public long createRecord(String[] data)
throws DuplicateKeyException;
When or in what situation to throw duplicateKeyException?
Could someone give me help?
Thanks,
Peter
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you go to create a record and the key is already in the database, then this is a duplicate key. In real databases you have primary key which must be unique. So if you try to create a new record where the primary key already exists the database will throw back an error.
This is what Sun is looking for here.
If you have a primary key that is a number and the current values in the database are
1
3
4
5
and you try to insert a new record with a primary key of 2, then it will let you, if you try to insert a new record with a primary key of 4, then it will throw an error.
Hope that helps.
Mark
 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Spritzler:
If you go to create a record and the key is already in the database, then this is a duplicate key. In real databases you have primary key which must be unique. So if you try to create a new record where the primary key already exists the database will throw back an error.
This is what Sun is looking for here.
If you have a primary key that is a number and the current values in the database are
1
3
4
5
and you try to insert a new record with a primary key of 2, then it will let you, if you try to insert a new record with a primary key of 4, then it will throw an error.
Hope that helps.
Mark


---------------
Thanks Mark.
In the assignment, what field is the key? Because we cannot change the format of the datebase file, the only unique field is recNo and it is impossible to be duplicated.
Could you explain to me?
Thanks.
Peter
 
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
That depends on your own decision. Someone said the combine of name and location is key.
Best,
 
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Peter -
From everything I've read out here on the forum, I understood that most folks are using the record number as the key. It's kind of an implied thing that you calculate based on the data file specs - i.e. the actual record number is NOT stored anywhere on the file.
About your point of being impossible to get the duplicate....
The scenario I have been thinking about is this:
In my createRecord(String [] data) method, I determine whether or not I can add a record into a previously deleted record's spot, or if I have to add a new record at the end of the file. Say I determine which of these I will do - what if some other client/thread grabs the spot that I am planning to use for my add. This might be a potential case where when I actually get around to doing my add, I would find a Duplicate out there already.
I have not dealt with this problem yet though. I plan to think about it more when I get to the locking portion of the assignment.
TJ
 
Peter Yunguang Qiu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all of you. I am still not very clear.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have had a similar question about the DuplicateKeyException. I do not believe there is a unique key in the provided database. There could logically be multiple records with the exact same data (a hotel could have multiple rooms with the same specifications avaiable at the same time for the same price). If there was a room number field, then a unique combination could be established. I am planning on not throwing the DuplicateKeyException and explaining it in my designchoices.txt. My app adds the new row if there are no existing matching rows or there are matching rows that are vaild. If there is a matching row that is deleted, that row is validated and a new row is not added.
The other thing that bothers me about createRecord (and deleteRecord) is that they are not requirements for my GUI, so I am not including those funcitons in the GUI, even though I am writing the code and testing it.
I would appreciate any comments on my choices.
Thanks
 
Philippe Maquet
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Don,
Welcome to this forum !

I do not believe there is a unique key in the provided database. There could logically be multiple records with the exact same data (a hotel could have multiple rooms with the same specifications avaiable at the same time for the same price). If there was a room number field, then a unique combination could be established.


Exactly ! That's why I wrote somewhere that if you implement a primary key in URLyBird (and hence the DuplicateKeyException), it *must be* optional.

I am planning on not throwing the DuplicateKeyException and explaining it in my designchoices.txt.


Looks like reasonable.

My app adds the new row if there are no existing matching rows or there are matching rows that are vaild. If there is a matching row that is deleted, that row is validated and a new row is not added.


Oops ! Do you mean that you may transform a "create" in an "update" ? If you do, I think you are breaking the create requirements.
Best,
Phil.
 
Don Resnik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, my create requirements say that the method creates a new record or possibly reuses a deleted entry. I don't know if they used that wording to make it tricky, but I interpet reusing a deleted entry as changing the valid flag back to valid. While that does not "create" a new record, it does reuse the deleted entry.
Also, thanks for commenting so quickly. I have been working on this for a few months and I am almost ready to submit (at least I thought I was until I started reading some of these posts). My last big question is about RMI-client death (I just posted the question).
Thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic