Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Avoid concurrency issue Expand / Collapse
Author
Message
Posted Thursday, April 24, 2014 10:08 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 2:27 PM
Points: 327, Visits: 853
I've a transaction table that has seqID column. Every time we process a new transaction through a scheudled job, I take the max(seqID) + 1 and put it in the table.
When a user process transaction manually through the web, it also takes the max(seqid) + 1 and put it in the table.
Often times, when both process runs simultaneously, they read the same seqID and increment that by 1 and ending up duplicates in the table.

Anyway to avoid this concurrency issue? Appreciate your help.

Post #1564734
Posted Thursday, April 24, 2014 10:11 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:46 PM
Points: 42,462, Visits: 35,525
Make the column an identity? Or use a sequence?


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1564740
Posted Thursday, April 24, 2014 10:13 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 23, 2014 2:27 PM
Points: 327, Visits: 853
Making column identity is an option that should have thought beforehand. If we go that route now, it can mess up reports and other files that we already sent to the client. Any other options you can think of?
Post #1564745
Posted Thursday, April 24, 2014 11:01 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:46 PM
Points: 42,462, Visits: 35,525
SQL_Surfer (4/24/2014)
If we go that route now, it can mess up reports and other files that we already sent to the client.


Why?

Any other options you can think of?


...
Or use a sequence?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1564786
Posted Thursday, April 24, 2014 3:29 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 1,294, Visits: 3,730
How about using the TABLOCK hint?
Post #1564906
Posted Thursday, April 24, 2014 3:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 13,110, Visits: 11,939
Eirikur Eiriksson (4/24/2014)
How about using the TABLOCK hint?


That seems to be dealing with the symptom rather than the cause.

Hints should be used as a last resort. The concurrency issue is caused by poor design, we shouldn't need to resort to query hints to deal with it. Just my 2¢.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1564910
Posted Thursday, April 24, 2014 3:43 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 9:16 AM
Points: 1,294, Visits: 3,730
Sean Lange (4/24/2014)
Eirikur Eiriksson (4/24/2014)
How about using the TABLOCK hint?


That seems to be dealing with the symptom rather than the cause.

Hints should be used as a last resort. The concurrency issue is caused by poor design, we shouldn't need to resort to query hints to deal with it. Just my 2¢.


I agree that one should not try to outsmart the query engine and the optimizer but I believe that not all hints are equal in this sense. If the problem is as described, more than one process reading the value at the same time, then force them to queue. An improved design would probably be a better solution though
Post #1564911
Posted Thursday, April 24, 2014 4:34 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 5:28 AM
Points: 1,783, Visits: 5,672
SQL_Surfer (4/24/2014)
Making column identity is an option that should have thought beforehand. If we go that route now, it can mess up reports and other files that we already sent to the client. Any other options you can think of?


Adding an identity column need not change existing values, you can create a new table with the column as an identity, then set identity_insert on for the new table, insert the existing rows, then RESEED the identity column, set identity_insert off, rename the old table, rename the new table to the old name.

Once you have done that, you will want to change your code to OUTPUT the identity value whenever you insert a new row, so that you can pass back to the calling code the new value.


MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1564920
    Posted Friday, April 25, 2014 1:32 AM


    SSC-Forever

    SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

    Group: General Forum Members
    Last Login: Yesterday @ 3:46 PM
    Points: 42,462, Visits: 35,525
    Eirikur Eiriksson (4/24/2014)
    How about using the TABLOCK hint?


    May or may not be sufficient, depending on the code (which we haven't seen), and not necessarily the best option.
    There are many ways to do a manual sequence wrong, but preventing any other access to what may be an important table isn't necessarily going to magically fix the problem and it may cause other problems.



    Gail Shaw
    Microsoft Certified Master: SQL Server 2008, MVP
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

    Post #1564982
    Posted Saturday, April 26, 2014 3:12 PM
    Forum Newbie

    Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

    Group: General Forum Members
    Last Login: Sunday, May 11, 2014 8:25 PM
    Points: 1, Visits: 29
    Since you are using sql server 2012 have you considered using the new feature for creating a sequence object? If in addition the application requires knowing whether this is the nth record, this of itself sounds more like something calculated/computed as needed and not something to store.

    I am aware that maybe in legacy databases there had been the practice of artificially restricting a sequence by using a varchar, like in our database using suffix fields to arbitrarily limit items to 999 for a given module and type code to represent different entities ...for ex. MasterName( agency, incident_nr, suffix1, suffix2, module, type, <data fields...> ). Then you'd need to use a stored procedure in order to check if the generated sequence value already exists and remains in range and if not to reset the sequence and continue generating until a gap exists. Again, the select would need to compute the seqno to the application having ordered the items by a record's created datetime (in high volume scenarios or when you have bulk insert job, without a surrogate primary key using identity, a datetime may not be sufficient to order deterministically).

    Now if your seqid is an int (number) then life's good, use the sequence object and let the application use a select or programing code to compute the item's order. If a varchar and you have no independent surrogate primary key to order by and you do not have an independent field retaining the timestamp for when the record was created, you need to make decisions for whats the best compromise for your situation.

    Hope this helps.
    Post #1565371
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse