• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Inserting Duplicate rows

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Ours is a WebServices application. Whenever a request comes it is first checked for duplicate transaction from the table.
If a duplication transaction ID exists the request is rejected else it is processed and at the end...the record is inserted in the database.

Now the problem is if 2 or more requests having same id comes at same time...the application process them paralelly and unable to identify duplicate id and finally getting inserted in database after completion of process.

Unique Constraint will only help in not inserting duplicate row...but before that the actual processing has already taken place.

Is there any way in JDBC to check for duplicate records at the same time.

Regards
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a reason why a unique constraint won't solve the problem. Two requests can never be precisely at the same time.

However, you could consider having some transaction handling and table locking.
 
author & internet detective
Posts: 40211
816
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jumbalika",
We're pleased to have you here with us in the JDBC forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks,
Jeanne
Forum Bartender
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jumbalika", you had already been asked before (in this topic) to adjust your display name to confirm to JavaRanch standards. This is not optional. Be aware that accounts with invalid display names are subject to removal.

bear
Forum bartender
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be storing the request in the database before processing it. This helps in 2 ways

a) you will be able to detect duplicate requests
b) In case your application stops because of critical error or the server is rebooted, you will hae the request saved in the database. So, when you start your application back again, you can process the rquest again
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jumbalika,
What about using a stored procedure ? Let the Sp do the work and use a sequence, so that the unique constraint won't get voilated.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try not to use "distinct" restrictions in your SQL clauses
Good luck !!
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jumbalika,

I think unique constraint should work for avoiding duplication, and Mr. Annie there might be occurs situation where 2 or 3 requests can come at the same time, mostly in web applications.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic