Solved

SQL Server 2008R2 Concurrency

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

744 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

11 Experts available now in Live!

Get 1:1 Help Now