[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Server 2008R2 Concurrency

Posted on 2010-11-18
3
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
lcohan earned 2000 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 40

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

656 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