This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting Duplicate rows

 
Jumbalika
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
 
Annie Smith
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34837
369
Eclipse IDE Java VI Editor
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
 
Jayesh Lalwani
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
 
Srinivasa Raghavan
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.
 
Luis Canizares
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 !!
 
Nitin Jawarkar
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic