SQL lock row,table on insert and select -
I am trying to get some kind of lock on SQL. To make it clear what I am doing simple: a table with ID int automation as a PK, and a field data worker (max) non-cluster IX
Now I have some C # The code that checks the symlip if the item is not in DB, one is inserted like the SQL code I am using:
INSERT {0}. (TABLOCKX) with value (@ data ...) and select one:
Select ID from {0}. With {1} WHO (data = @ data) but I can see that many objects are inserted with the same value, which creates tab lock deadlock , And I do not want to use the unique index because it is very slow.
One way to achieve this with locking
I'm not sure You want the same, I hope this answer is helpful.
Private Zero Test (string aConnectionString, string adata) {use (SqlConnection sqlConnection = new SqlConnection (aConnectionString)) {sqlConnection.Open (); SqlCommand sqlCommand = sqlConnection.CreateCommand (); SqlTransaction sqlTransaction = sqlConnection.BeginTransaction (System.Data.IsolationLevel.ReadCommitted); SqlCommand.Connection = sqlConnection; sqlCommand.Transaction = sql transaction; Try {sqlCommand.CommandText = @ "if not (starting from the selection id {0}. {1} where data = @ data} {0} start in. {1} select @ data end"; SqlCommand.Parameters.Add ("@Data", System.Data.SqlDbType.VarChar) .Value = aata; sqlTransaction.Commit (); } Hold (exception before) {sqlTransaction.Rollback (); }}}
Comments
Post a Comment