473,651 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Duplicate key insertion error when concurrent users insert same data into table

6 New Member
Hi,
I have a java application which calls a stored procedure to insert data into a table.Multiple threads of java call the same procedure at the same time. Sometimes it happens that few threads send the same data as parameter to the procedure. In that case i get the error : "Cannot insert duplicate key row in object ...."

The functionality of the proc is - It checks if a given data (Based on Unique key) is present in the table or not. If present it doesnt insert.It simply updates. If no row present, it will insert a new row.

So when both the concurrent processes search for the existence of data, both of them dont find the data. Both go on to insert the same data which gives the problem.

I tried using WITH(HOLDLOCK) and WITH(NOLOCK), but still get the same error.

Can anyone please help in this.

Regards,
Subrat
May 23 '07 #1
3 5983
almaz
168 Recognized Expert New Member
I tried using WITH(HOLDLOCK) and WITH(NOLOCK), but still get the same error
Surround the checking for existence & insertion/update with BEGIN TRANSACTION/COMMIT TRANSACTION. Also it looks like you'll have to set SERIALIZABLE isolation level (SET ISOLATION LEVEL SERIALIZABLE) before BEGIN TRANSACTION.
May 23 '07 #2
Subrat Das
6 New Member
Surround the checking for existence & insertion/update with BEGIN TRANSACTION/COMMIT TRANSACTION. Also it looks like you'll have to set SERIALIZABLE isolation level (SET ISOLATION LEVEL SERIALIZABLE) before BEGIN TRANSACTION.
I am using IF NOT EXISTS(select.. .) then insert.Any idea about sp_getapplock. I saw in a site http://sqlblogcasts.co m/blogs/tonyrogerson/archive/2006/06/30/855.aspx
a solution for it, but unable to implement it.

If i change the way i am checking for the existence of row, will it help.Waiting for your valuable suggestions.
May 23 '07 #3
almaz
168 Recognized Expert New Member
Locks should be obtained using transactions. Your stored procedure should look like:
Expand|Select|Wrap|Line Numbers
  1. create procedure dbo.RefreshData(@ID int, @Data varchar(100))
  2. as
  3. set nocount on
  4. set isolation level serializable
  5. begin transaction
  6.   if not exists(select * from YourTable where ID = @ID)
  7.     insert YourTable (ID, Data) values (@ID, @Data)
  8.   else
  9.     update YourTable 
  10.     set Data=@Data 
  11.     where ID = @ID
  12. commit transaction
By the way, if your primary key is a single int field, it is recommended to make it IDENTITY field, and refactor your code so that primary key value is generated on SQL Server and than returned to client. This way will allow you to omit serializable isolation level.
May 23 '07 #4

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

Similar topics

4
2202
by: Doo-Dah Man | last post by:
I hope this is the right group for this question, if it's not please direct me. I have a data-driven website that allows users to enter records for sales leads. It all works perfectly. The only thing I want to do right now is prohibit users from entering the same lead twice. I have a PK field in the MS Access database that, obviously, disallows duplicate records. However, when such an attempt is made, the browser redirects to some...
1
2071
by: dbuchanan | last post by:
VB.NET 2003 / SQLS2K The Stored Procedure returns records within Query Analyzer. But when the Stored Procedure is called by ADO.NET ~ it produced the following error message. --------------------------- Exception Message: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints. ---------------------------
4
2281
by: KKramsch | last post by:
My code is generating this type of error: Security Error: Content at http://nonexistent.org/somepage.html may not load data from about:blank. The "about:blank" page mentioned in the error message is a pop-up window, whose content is 100% dynamically-generated, and which is in fact *owes its existence* to code in the referring page (nonexistent.org/somepage.html), along the lines of something like
4
12039
by: prasad | last post by:
I am getting sql error during binding a program which access a temporary table. The temporary table declaration and access methods are given below. EXEC SQL DECLARE GLOBAL TEMPORARY TABLE TEM88 LIKE SYSIBM.SYSDUMMY1 END-EXEC. EXEC SQL INSERT INTO TEM88 SELECT * FROM SYSIBM.SYSDUMMY1 END-EXEC.
0
1541
by: vsaraog | last post by:
Hi y'all, I am using a Created Global Temporary Table in DB2 OS/390 version 7.1.2. I am inserting some data in it and then updating the table using ODBC but while I try to update it, I am getting following message. SQL0526N The requested function does not apply to declared temporary tables. SQLSTATE=42995 Anyone has any ideas why I getting the above error message. Following
0
1333
by: Alpha | last post by:
I have a dataset containing a table as a data source for a list box in the windows application depending on which radio button the user selects. The table is disposed when the user clicks on other radio button. I'm getting an error message that a column that I added previously alreday exists in the dataset. Each table that I add to the dataset according to which radio button is selected is disposed when it's de-selected. Why does the...
2
1359
by: Henrik | last post by:
im reciving an error when i tries to read multidimensional XML data, into my system. I'm receving the same errors discriped at: http://support.microsoft.com/default.aspx?scid=kb;en-us;325695 And http://support.microsoft.com/kb/325696/EN-US/ But I can't find a solution for the problem???
0
1411
by: babukgl | last post by:
Hi, We have a stored procedure which does a huge amount of transformation of data and it is running pretty slow. To avoid this we tried to create a cursor out of the SQL used in stored procedure & tried to load the data from that sql using Load from cursor statement. However, when we run this command i get an error message saying "NAME TOO LONG". Does this mean it has some limitation on the SQL query length used in cursor? Can i...
3
2195
by: James Watson | last post by:
'Microsoft VB 6.3, Access 2002 'Syntax error in the INSERT INTO Statement when the query runs 'How can I make this work? Private Sub Command52_Click() On Error GoTo Err_Command52_Click Dim rst As DAO.Recordset Dim dbs As DAO.Database Dim rsSQL As DAO.Recordset
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8457
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.