Solved

SQL Server 2008R2 Concurrency

Posted on 2010-11-18
3
467 Views
Last Modified: 2012-08-13
Greetings,

      I’m using SQL Server 2008R2 in its default configuration. Below are three skeleton examples of stored procedures.

      Question: How do I ensure that each of these types of stored procedures will run as an atomic operation in a multi threaded environment?  Does the default locking ensure that the underlying table cannot change between testing if a value exists and updating that value? Should I always put a TRANSACTION BEGIN / COMMIT TRANSACTION around these types of stored procedures? What’s the best practice approach for handling this?

Thanks,

Karsten

############################################################
STORED PROCEDURE EXAMPLE 1:

IF EXISTS(SELECT * FROM MyTable WHERE MyId = @MyId)
BEGIN
      -- Update
      UPDATE MyTable
      SET
            MyData = @MyData
      WHERE MyId = @MyId
END
ELSE
BEGIN
      -- Insert
      INSERT MyTable
      (
          MyData,
          MyId
      )
      VALUES
      (
            @MyData,
            @MyId
      )
END

############################################################
STORED PROCEDURE EXAMPLE 2:

      DELETE FROM MyTable1 WHERE MyId = @MyId
      DELETE FROM MyTable2 WHERE MyId = @MyId

############################################################
STORED PROCEDURE EXAMPLE 3:

      DECLARE @Test1 bit
      IF EXISTS(SELECT * FROM MyTable MyId = @MyId)
            SET @Test1 = 1
      ELSE
            SET @Test1 = 0
      
      DECLARE @Test2 bit
      IF EXISTS(SELECT * FROM MyTable MyId = @MyId2)
            SET @Test2 = 1
      ELSE
            SET @Test2 = 0
      
      
      DECLARE @CombinedTest int
      SET @CombinedTest = 0
      IF @Test1 = 1 SET @CombinedTest = @CombinedTest + 1
      IF @Test2 = 1 SET @CombinedTest = @CombinedTest + 2

      RETURN @CombinedTest      

0
Comment
Question by:karstenweber
  • 2
3 Comments
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 34166661
SQL server default isolation level is read commited and in my opinion both of the above SP's do not need any explicit transactions. Only place where I used such thing in SQL was to generate a invoice number and use it to do some updaets before it was inserted into the actual invoice table but you don't do such things. Only thing to recommand is to use NOLOCK hint on all selects and maybe ROWLOCK or UPDLOCK on the updates if you have many rows to update in one statement.
0
 
LVL 39

Expert Comment

by:lcohan
ID: 34166787
By the way, I think you could re-write the second SP to something like below where you definitely won't need explicit transaction:

select case
      when EXISTS (SELECT * FROM MyTable where MyId = Id1) then 1
      when EXISTS (SELECT * FROM MyTable where MyId = Id2) then 2
      when EXISTS (SELECT * FROM MyTable where MyId = Id1) and EXISTS (SELECT * FROM MyTable where MyId = Id2) then 3
else 0 end
0
 

Author Comment

by:karstenweber
ID: 34169940
Thanks for the feedback Icohan.  I did suspect that the default read commited  isolation would suffice, but it wasn't exactly clear.  Thanks for the re-write. The original form was pretty brain dead, but did show the multiple select that I was concerned about.

Karsten
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question