• 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

Get or Insert

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a method that either gets and Id or inserts the a new row, what i have works some of the time but not all the time. sometimes i get a Duplicate Key message back. The table is keyed on the combination of identifierref and object_type_id, can anyone recommend a better more stable way, This is using the SQLServerDriver class for 2005 and Microsoft SQL Server 2000. Thank you in advance.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian,
Welcome to JavaRanch!

It looks like the duplicate key exception is thrown when two callers execute the code at the same time. This could happen if you have the following sequence of events:
1) You check if record exists and it doesn't
2) Another thread inserts a record with that key
3) You insert the record and get a duplicate key exception

Take a look at transactions to eliminate this scenario.
 
Brian Shultz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou,
That makes sense, so would the best way around this be a try catch that looks for duplicate key again, and then if caught try to select it and if not found try to insert it again. Or is there a good way around this at all?
Thanks
_Brian
 
reply
    Bookmark Topic Watch Topic
  • New Topic