473,383 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to prevent duplicate insertion without locking the entire table?

Hello,
I've a scenario with multiple threads that performs select/insert/update on the same table.
Basically, the flow is:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tablex where pk=?
if empty
Expand|Select|Wrap|Line Numbers
  1. INSERT into tablex values(...)
else
Expand|Select|Wrap|Line Numbers
  1. UPDATE tablex set ... where pk=?
where ph is the primary key ot tablex
To improve performance, I'd like that threads with different Pks will run without blocking, but threads with the same PK will be "serialized".
Using SELECT FOR UPDATE WITH RS is fine when the row already exists, but produce duplicate insert when the row doesn't exist yet, and two thread are trying to insert it.
Using TRANSACTION_SERIALIZABLE or SELECT FOR UPDATE WITH RR cause a table lock, and so threads with different Pks are blocked.
Is there a basic pattern to solve this problem?

Environment: Db2 V9.1 on Windows. Using a normal Java application
Sep 1 '08 #1
1 5293
docdiesel
297 Expert 100+
Hi,

did you try the MERGE statement (http://publib.boulder.ibm.com/infoce...c/r0010873.htm ) ? Something like the following could be working:
Expand|Select|Wrap|Line Numbers
  1. MERGE INTO mytable mta
  2. USING
  3.   (SELECT pk FROM mytable where pk=xxx) mtb
  4. ON (mta.pk=mtb.pk)
  5. WHEN MATCHED THEN
  6.   UPDATE
  7.   SET field = value
  8. WHEN NOT MATCHED THEN
  9.   INSERT
  10.     (fields)
  11.   VALUES
  12.     (values)
Regards,

Bernd
Sep 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Timo | last post by:
I'm trying to make a thread safe object cache without locking. The objects are cached by the id of the data dict given in __new__. Objects are removed from the cache as soon as they are no longer...
22
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4....
5
by: Manish | last post by:
The topic is related to MySQL database. Suppose a table "address" contains the following records ------------------------------------------------------- | name | address | phone |...
11
by: ariel81 | last post by:
i have created a function to input data from four textboxes into a table. how do i prevent duplicate records of the same mth/Yr into the table? editing of the data in the table with reference to the...
6
by: Arthur Dent | last post by:
Anyone know, in VB.NET (2005) how to prevent duplicate occurrences of the same event handler on an event? e.g... I have some object which raises an event, and some other class which consumes...
6
by: teser3 | last post by:
I have my PHP inserting into Oracle 9i. But how do I prevent duplicate record entries? I only have 3 fields in the insert in the action page: CODE <?php $c=OCILogon("scott", "tiger",...
1
by: chicago1985 | last post by:
I have a unique constraint in my Oracle table for 3 fields. If I enter duplicate info on the table using Oracle client I will get an Ora message error ORA-00001 that tells me it is a duplicate entry...
0
by: jehrich | last post by:
Hi Everyone, I am a bit of a hobby programmer (read newbie), and I have been searching for a solution to a SQL problem for a recent pet project. I discovered that there are a number of brilliant...
0
by: Himanshu85 | last post by:
Hi, There is an issue in our application where we have several duplciate entries present in our DB2 tables due to which the re-org is abending on daily basis. the DBA team has advised us to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.