Solved

SQL Server 2008R2 Concurrency

Posted on 2010-11-18
3
465 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

910 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now