Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am building an application that allows multi-instances to run at the same time and access to the same database.
Suppose that the instance 1 of the app is executing the SQL statement
SQL
"Insert into MyTable(ID,Value) values(1, 'A')"

Here "ID" is the primary key of MyTable and the Value 1 was calculated before based on the current values of ID column of MyTable (to avoid conflict), I wonder if unexpectedly another instance of that app has also calculated for the same ID value (1) because both of instances have the same algorithm to find out the next ID to insert into MyTable.
And then the second instance may execute a Sql statement like this
SQL
"Insert into MyTable(ID,Value) values (1, 'B')". 

That situation of course will lead to a primary key conflict.
Could you please help me solve this hard problem?
Thank you very much!
Posted
Updated 2-Nov-11 1:49am
v2

Use "Identity" on your Primary Key column. RDBMS will take care of rest.

http://msdn.microsoft.com/en-us/library/aa933196%28v=sql.80%29.aspx

Edit -
When you use "Identity" column, then your Insert statement should be as below. Need not to pass value for column having "Identity".
SQL
Insert into MyTable(Value) values('A')
 
Share this answer
 
v3
Comments
BobJanova 2-Nov-11 8:02am    
Good answer. In MySQL you set the column to be 'auto_increment' to do the same thing. One problem with this approach is that it can be difficult to find the ID of the row you just added.
[no name] 2-Nov-11 8:04am    
Thank you very much!
But unfortunately, I can't let the DBMS care for me about autoinserting values for ID column because the values of ID must be formatted and displayed as an important info, in fact it is not simply an integer, it may be preceded with some characters like "AAA".
Thank you very much!
RaisKazi 2-Nov-11 8:09am    
In your question you mentioned only integer values. That's the reason I suggested "Identity".
In this case, You may have to look for some "Locking" kind of logic while generating your next Id.
[no name] 2-Nov-11 8:54am    
I'm sorry, I should have cleared the things first. However your solution has introduced the term "Identity" and its usage to me that I have had little knowledge about it before. If there isn't any solution better than yours, I should accept your solution anyway :)
Thank you very much!
I had the same problem and transactions solved my problem. There are transaction commands in TSQL and ADO or ADO.NET.
In all of them, you have to do something like this :

start transaction
  get biggest id
  compute next id
  insert new record with new ID
commit transaction


And another point is that transactions have multiple isolation levels and the clearest one for this type of problems is Serializable one.

For more information please read :
http://msdn.microsoft.com/en-us/library/ms188929.aspx[^]

http://msdn.microsoft.com/en-us/library/ms173763.aspx[^]

http://msdn.microsoft.com/en-us/library/2k2hy99x%28v=vs.71%29.aspx[^]

If you say that in which of TSQL , ADO or ADO.NET you are interested, I'll provide the code for you. However I'm sure that you have got the point.
 
Share this answer
 
Comments
[no name] 2-Nov-11 17:17pm    
Wow! That's exactly what I need! Thank you so much for the keyword "transaction"!
With me, keywords are all to start and understand.
Thank you very much!
RaisKazi 3-Nov-11 0:06am    
My 5!
Amir Mahfoozi 3-Nov-11 0:56am    
Thank you ;)
You should change your ID generation algorithm.
You have to consider all the incremented values.
You can record all the issued ID's in another table and issue the id's from this table. By this way you will issue a single id only once.
And then insert them in main table only those id's for which all the fields are fulfilled successfully.
 
Share this answer
 
Comments
[no name] 2-Nov-11 8:16am    
I think the problem here is the time the two instances of the app execute the sql statement, if after the first inserted a new row into MyTable successfully, the second then will find out the next ID as 2 not 1 and no conflict happens. But my situation is the two instances seem to operate with the same sequence of commands (finding next ID, Inserting) nearly simultaneously.
I hope you figure out the situation here!
Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900