SlideShare uma empresa Scribd logo
1 de 63
Session
SAD335

  Concurrency problems and
  locking techniques in SQL
   Server 2000 and VB.NET
          Fernando G. Guerrero
                SQL Server MVP
              .NET Technical Lead
                    QA plc

                 October 2002
Quick info about Fernando
                            (2 milliseconds)

                            •   MCSD, MCSE+Internet (W2K), MCDBA, MCT,




QA
                                SQL Server MVP

                            •   This is where I work: QA, The best learning
                                environment in Europe

                            •   Writing for SQL Sever Magazine and SQL
                                Server Professional

                            •   This is my main web site: www.callsql.com

                            •   This is my book (so far):
                                 –   Microsoft SQL Server 2000 Programming by
                                     Example (ISBN : 0789724499, co-authored with Carlos
                                     Eduardo Rojas)


                            •   Currently writing on ADO.NET and SQL Server
                                2000


SQL Server Magazine LIVE!
Agenda
•   Concurrency problems
•   Isolation levels
•   Locks
•   Transactions




SQL Server Magazine LIVE!            3
Concurrency problems
•   Lost Updates
•   Uncommitted Dependency
•   Inconsistent Analysis
•   Phantom Reads




SQL Server Magazine LIVE!        4
Lost Updates (1)
                          Peter            Paul
Peter              UnitPric         UnitPric
                                                  Paul
                   e                e
                   10.0             10.0




 SQL Server Magazine LIVE!
                                 Mary
                                                    5
                              (SQL Server)
Lost Updates (2)
                          Peter              Paul
Peter              UnitPric   @UP *   UnitPric
                                                    Paul
                   e          1.2     e
                   10.0       12.0    10.0
DECLARE @UP money

SELECT @UP = UnitPrice
FROM Products
WHERE ProductID = 25




 SQL Server Magazine LIVE!
                                 Mary
                                                      6
                              (SQL Server)
Lost Updates (3)
                                Peter             Paul
Peter                    UnitPric   @UP *   UnitPric   @UP *
                                                               Paul
                         e          1.2     e          1.1
                         10.0       12.0     10.0 11.0
DECLARE @UP money
                                            DECLARE @UP money
SELECT @UP = UnitPrice
FROM Products
WHERE ProductID = 25
                                            SELECT @UP = UnitPrice
                                            FROM Products
                                            WHERE ProductID = 25




   SQL Server Magazine LIVE!
                                       Mary
                                                                 7
                                    (SQL Server)
Lost Updates (4)
                                Peter              Paul
 Peter                   UnitPric   @UP *   UnitPric   @UP *
                                                                            Paul
                         e          1.2     e          1.1

DECLARE @UP money        12.0       12.0    12.0       11.0    DECLARE @UP money
SELECT @UP = UnitPrice                                         SELECT @UP = UnitPrice
FROM Products                                                  FROM Products
WHERE ProductID = 25                                           WHERE ProductID = 25


UPDATE Products
SET UnitPrice =
    @UP * 1.2
WHERE ProductID = 25


   SQL Server Magazine LIVE!
                                       Mary
                                                                                   8
                                    (SQL Server)
Lost Updates (5)
                                   Peter              Paul
 Peter                      UnitPric   @UP *   UnitPric   @UP *
                                                                              Paul
                            e          1.2     e          1.1

DECLARE @UP money           11.0       12.0    11.0       11.0    DECLARE @UP money
SELECT @UP = UnitPrice                                            SELECT @UP = UnitPrice
FROM Products                                                     FROM Products
WHERE ProductID = 25                                              WHERE ProductID = 25
UPDATE Products
SET UnitPrice = @UP * 1.2
WHERE ProductID = 25                             UPDATE Products
                                                 SET UnitPrice =
                                                     @UP * 1.1
                                                 WHERE ProductID = 25


   SQL Server Magazine LIVE!
                                          Mary
                                                                                 9
                                       (SQL Server)
Uncommitted Dependency (1)
                          Peter            Paul
Peter              UnitPric         UnitPric
                                                  Paul
                   e                e
                   10.0             10.0




 SQL Server Magazine LIVE!
                                 Mary
                                                    10
                              (SQL Server)
Uncommitted Dependency (2)
                          Peter            Paul
Peter              UnitPric         UnitPric
                                                  Paul
                   e                e
                   12.0             12.0
BEGIN TRANSACTION

UPDATE PRODUCTS
SET UnitPrice =
    UnitPrice * 1.2
WHERE ProductID = 25



 SQL Server Magazine LIVE!
                                 Mary
                                                    11
                              (SQL Server)
Uncommitted Dependency (3)
                                  Peter           Paul
 Peter                     UnitPric         UnitPric   @UP
                                                             Paul
                           e                e
                           12.0             12.0 12.0
BEGIN TRANSACTION
                                           DECLARE @UP money
UPDATE PRODUCTS
SET UnitPrice = UnitPrice * 1.2
WHERE ProductID = 25
                                           SELECT @UP = UnitPrice
                                           FROM Products (NOLOCK)
                                           WHERE ProductID = 25




   SQL Server Magazine LIVE!
                                         Mary
                                                               12
                                      (SQL Server)
Uncommitted Dependency (4)
                                  Peter            Paul
 Peter                     UnitPric         UnitPric   @UP
                                                                           Paul
                           e                e

BEGIN TRANSACTION          12.0
                           10.0             12.0
                                            10.0       12.0   DECLARE @UP money
UPDATE PRODUCTS                                               SELECT @UP = UnitPrice
SET UnitPrice = UnitPrice * 1.2                               FROM Products
WHERE ProductID = 25                                          WHERE ProductID = 25


ROLLBACK TRANSACTION




   SQL Server Magazine LIVE!
                                         Mary
                                                                                  13
                                      (SQL Server)
Uncommitted Dependency (5)
                                  Peter            Paul
 Peter                     UnitPric         UnitPric   @UP
                                                                            Paul
                           e                e

BEGIN TRANSACTION          10.0             10.0       12.0     DECLARE @UP money
UPDATE PRODUCTS                                                 SELECT @UP = UnitPrice
SET UnitPrice = UnitPrice * 1.2                                 FROM Products
WHERE ProductID = 25                                            WHERE ProductID = 25
ROLLBACK TRANSACTION
                                                   INSERT [Order details]
                                                   (
                                                         OrderID,
                                                         ProductID,
                                                         UnitPrice,
                                                         Quantity,
                                                         Discount)
                                                   VALUES (25365, 25,
   SQL Server Magazine LIVE!
                                         Mary                @UP,   10, 0.1)
                                                                           14
                                      (SQL Server)
Inconsistent Analysis (1)
                               Peter
Peter                                       Paul




 SQL Server Magazine LIVE!
                                Mary
                                              15
                             (SQL Server)
Inconsistent Analysis (2)
                            Peter
  Peter          @Count             830   Paul
                 @Total
                 @Average
                 @Total /
DECLARE @Count   int,
                 @Count

    @Total money,
    @Average money

SELECT @Count =
     COUNT(DISTINCT
              OrderID)
FROM [Order Details] Mary
   SQL Server Magazine LIVE!                16
                      (SQL Server)
Inconsistent Analysis (3)
                                        Peter
 Peter                       @Count             830        Paul
                             @Total
DECLARE @Count int,
   @Total money,             @Average
   @Average money
SELECT @Count =              @Total /
   COUNT(DISTINCT OrderID)   @Count
FROM [Order Details]
                                            UPDATE [Order details
                                            SET Quantity = 600
                                            WHERE OrderID = 10272
                                            AND ProductID = 20



   SQL Server Magazine LIVE!
                                     Mary
                                                             17
                                  (SQL Server)
Inconsistent Analysis (4)
                                        Peter
 Peter                       @Count             830                      Paul
                             @Total             1304284.2
DECLARE @Count int,                             4           UPDATE [Order details]
   @Total money,                                            SET Quantity = 600
   @Average money            @Average                       WHERE OrderID = 10272
SELECT @Count =                                             AND ProductID = 20
   COUNT(DISTINCT OrderID)   @Total /           1571.43
FROM [Order Details]
                             @Count
SELECT @Total =
   SUM(UnitPrice *
      Quantity *
      (1 – Discount))
FROM [Order Details]

   SQL Server Magazine LIVE!
                                     Mary
                                                                             18
                                  (SQL Server)
Inconsistent Analysis (5)
                                        Peter
 Peter                       @Count             830                     Paul
                             @Total             1304284.2
DECLARE @Count int,                             4           UPDATE [Order details]
   @Total money,                                            SET Quantity = 600
   @Average money            @Average                       WHERE OrderID = 10272
SELECT @Count =                                             AND ProductID = 20
   COUNT(DISTINCT OrderID)   @Total /           1571.43
FROM [Order Details]
                             @Count
SELECT @Total =
   SUM(UnitPrice *
      Quantity *
      (1 – Discount))
FROM [Order Details]                        UPDATE [Order
                                            details]
                                            SET Discount = 0.4
                                            WHERE ProductID =
   SQL Server Magazine LIVE!
                                        Mary20
                                                                           19
                                  (SQL Server)
Inconsistent Analysis (6)
                                         Peter
  Peter                       @Count             830                     Paul
                              @Total             1304284.2
 DECLARE @Count int,                             4           UPDATE [Order details]
    @Total money,                                            SET Quantity = 600
    @Average money            @Average           1542.78     WHERE OrderID = 10272
 SELECT @Count =                                             AND ProductID = 20
    COUNT(DISTINCT OrderID)   @Total /           1571.43
 FROM [Order Details]
                              @Count                         UPDATE [Order details]
                                                             SET Discount = 0.4
 SELECT @Total =                                             WHERE ProductID = 20
    SUM(UnitPrice *
       Quantity *
       (1 – Discount))
 FROM [Order Details]



SELECT @Average =
   AVG(TotalPrice)
FROM (…) AS TotOrders
    SQL Server Magazine LIVE!
                                      Mary
                                                                            20
                                   (SQL Server)
Phantom Reads (1)
                   OrderID ProductI UnitPrice
 Peter                     D                    Paul
                   10259      37      20.8
                   10337      37      20.8
                   10408      37      20.8
                   10523      37      26.0
                   10847      37      26.0
                   10966      37      26.0


SELECT OrderID,
   ProductID,
   UnitPrice
FROM [Order Details]
WHERE ProductID = 37
  SQL Server Magazine LIVE!
                                 Mary
                                                  21
                              (SQL Server)
Phantom Reads (2)
                        OrderID ProductI UnitPrice
Peter                           D                         Paul
                        10259   37       20.80
                        10337   37       20.80
SELECT OrderID,
   ProductID,           10408   37       20.80
   UnitPrice
FROM [Order Details]    10523   37       26.00
WHERE ProductID = 37
                        10847   37       26.00
                        10966   37       26.00
                        10615   37
                                       INSERT [Order
                                        31.54


                                       details]
                                             (OrderID,
                                       ProductID,
                                             UnitPrice,
                                   MaryQuantity,
                                (SQL Server) Discount)
   SQL Server Magazine LIVE!                                22
Phantom Reads (3)
                        OrderID ProductI UnitPrice
 Peter                          D                                Paul
                        10259   37       20.80
                        10337   37       20.80
SELECT OrderID,                                      INSERT [Order details]
   ProductID,           10408   37       20.80          (OrderID, ProductID,
   UnitPrice                                            UnitPrice, Quantity,
FROM [Order Details]    10523   37       26.00
                                                        Discount)
WHERE ProductID = 37
                        10847   37       26.00       VALUES (10615, 37,
                                                        31.54, 20, 0.1)
                        10966   37       26.00
                        10615   37       31.54
SELECT OrderID,
   ProductID,
   UnitPrice
FROM [Order Details]
WHERE ProductID = 37
   SQL Server Magazine LIVE!
                                   Mary
                                                                    23
                                (SQL Server)
Isolation levels
• Transact-SQL:
   – READ COMMITTED
   – READ UNCOMMITTED
   – REPEATABLE READ
   – SERIALIZABLE
• Extra .NET Isolation Levels:
   – Chaos (not valid for SQLClient)
   – Unspecified (not settable for SQLClient)

SQL Server Magazine LIVE!                       24
Isolation levels vs. Concurrency
             problems
            P   Problem
                                             Isolation
            S   Solution
                                               Level
            X




                                                                                  SERIALIZABLE
                solved by standard




                                           UNCOMMITTED




                                                                     REPEATABLE
                                                         COMMITTED
              exclusive locks




                                                            READ

                                                                        READ
                                               READ
            inside
                transactions
                Concurrency
                  Problem




                                Lost
                               Update
                                           X             X           X            X
                              Dirty Read   P             S           S            S
                          Inconsiste
SQL Server Magazine LIVE!     nt           P             P           S            S              25
                           Analysis
READ COMMITTED
• Default isolation level
• Avoids Dirty Reads
   – m yTr =
          an
     m yconn.Begi ans i Iol i
                 nTr acton( s atonLevelReadCom m it
                                         .            t
     ed)
   –S ELECT … FRO M … W I (
                         TH READCO M M I
                                       TTED)
   –S ELECT … FRO M … W I (
                         TH READPAS T)
   – S TRANS
      ET      ACTI N IO LATI N LEVEL READ CO M M I
                  O S      O                     TTED
• Requests Shared locks for the duration of the
  reading operation
• Requests Exclusive locks for each modification 26
SQL Server Magazine LIVE!
READ UNCOMMITTED
• Isolation level only suitable for “sneaking
  around”
   – m yTr =
          an
     m yconn.Begi ans i Iol i
                 nTr acton( s atonLevelReadUnCom m
                                         .
     it
     ted)
   –S ELECT … FRO M … W I (
                         TH READUNCO M M ITTED)
   –S ELECT … FRO M … W I ( LO CK)
                         TH NO
   – S TRANS
      ET      ACTI N IO LATI N LEVEL READ UNCO M M I
                  O S      O                       TTED
• Doesn’t request Shared locks at all
• Requests Exclusive locks for each modification
SQL Server Magazine LIVE!                            27
REPEATABLE READ
• Quite a restrictive isolation level
• Avoids all concurrency problems, except
  Phantom Reads
  – m yTr =
         an
    m yconn.Begi ans i Iol i
                nTr acton( s atonLevelRepeat eRe
                                        .      abl
    ad)
  –S ELECT … FRO M … W I (
                        TH REPEATABLEREAD)
  – S TRANS
     ET      ACTI N IO LATI N LEVEL REPEATABLE READ
                 O S      O
• Requests Shared locks for the duration of the
  transaction
• Requests Exclusive locks for each modification 28
SQL Server Magazine LIVE!
SERIALIZABLE
• The most restrictive isolation level
• Avoids all concurrency problems
   – m yTr =
          an
     m yconn.Begi ans i Iol i
                 nTr acton( s atonLevelS i i e)
                                         . eralzabl
   –S ELECT … FRO M … W I ( ERI ZABLE)
                         TH S ALI
   –S ELECT … FRO M … W I ( LDLO CK)
                         TH HO
   – S TRANS
      ET      ACTI N IO LATI N LEVEL S ALI
                  O S      O          ERI ZABLE
• Requests Shared locks for the duration of the
  transaction
• Requests Exclusive locks for each modification
SQL Server Magazine LIVE!                             29
Transactions
•   Transact-SQL transactions
•   Distributed transactions
•   .NET Manual transactions
•   .NET Automatic transactions




SQL Server Magazine LIVE!           30
Transact-SQL transactions
•   Transaction Statements
•   Nested transactions
•   Transactions and Stored Procedures
•   Transactions and Triggers




SQL Server Magazine LIVE!                31
Managing transactions with
      Transact-SQL Statements
• BEGIN TRAN
• COMMIT TRAN
• ROLLBACK TRAN




SQL Server Magazine LIVE!          32
Transaction Savepoints
• SAVE TRAN TranName
• ROLLBACK TRAN TranName




SQL Server Magazine LIVE!         33
Nested transactions
• @@TRANCOUNT tells you how many
  transaction levels you are in
• For SQL Server there is only one actual
  transaction
• Commit happens only when all nested
  transactions are ended
• Rollback cancels all nested transactions at
  once
SQL Server Magazine LIVE!                  34
Transactions and Stored
               Procedures
• After Rollback execution continues, but you are
  outside transaction boundaries
• If Rollback happens inside a procedure, the
  calling process receives error 266, Level 16:
   – Transaction count after EXECUTE indicates that a
     COMMIT or ROLLBACK TRANSACTION statement is
     missing. Previous count = 1, current count = 0.
• Good idea to use save points and inform the
  outer process using output parameters

SQL Server Magazine LIVE!                           35
Transactions and Triggers
• After Rollback execution continues inside
  the trigger, but you are outside transaction
  boundaries and the process terminates
  when the trigger does.
• Consider using INSTEAD OF triggers to
  minimize rollbacks
• Consider using cancelling operations
  instead of rollbacks

SQL Server Magazine LIVE!                    36
Distributed transactions




SQL Server Magazine LIVE!           37
.NET Manual transactions




SQL Server Magazine LIVE!         38
.NET Automatic transactions
• Apply the TransactionAttribute to your class.
• Derive your class from the
  ServicedComponent Class.
• Sign the assembly with a strong name.
  – To sign the assembly using attributes create a
    key pair using the Sn.exe utility.
  – sn -k MCTCon.snk


SQL Server Magazine LIVE!                        39
.NET Automatic transactions (2)
<Assembly: ApplicationName("MCTCON")>
<Assembly: AssemblyKeyFileAttribute("MCTCON.snk")>

<Transaction(TransactionOption.Required)> Public Class clsProduct
  Inherits ServicedComponent

  Dim myConnection As SqlConnection

  <AutoComplete()> Public Sub RaisePrice(ByVal ProductID As Integer, ByVal
   amount As Integer)
    OpenConnection()
    Updateproduct(ProductID, amount)
    CloseConnection()

  End Sub
 SQL Server Magazine LIVE!                                               40
.NET Automatic transactions (3)




SQL Server Magazine LIVE!     41
Locks
•   Dynamic locking strategy
•   Locking SQL Server resources
•   Types of locks
•   Hunting for locks




SQL Server Magazine LIVE!           42
Dynamic locking strategy
• SQL Server tries to minimize locking costs
  balancing:
   – Lock granularity
   – Lock maintenance cost
• SQL Server 2000 defaults to row lock
  when necessary
• Uses latches, lightweight synchronization
  objects, for internal operations, minimizing
  expensive locks
SQL Server Magazine LIVE!                    43
Locking SQL Server resources
• SQL Server 2000 can lock:
   – Data Row
   – Index Key
   – Any page
   – Extent
   – Table
   – Database


SQL Server Magazine LIVE!     44
Types of locks
•   Shared (S)
•   Update (U)
•   Exclusive (X)
•   Intent:
    – intent shared (IS)
    – intent exclusive (IX)
    – shared with intent exclusive (SIX)
• Schema:
    – schema modification (Sch-M)
    – schema stability (Sch-S).
• Bulk Update (BU)
SQL Server Magazine LIVE!                  45
A typical case of Deadlock
involving two connections (demo)




SQL Server Magazine LIVE!     46
A Deadlock situation involving
    more than two connections
             (demo)




SQL Server Magazine LIVE!       47
Binding connections (demo)




SQL Server Magazine LIVE!        48
Hunting for locks
• Profiler can detect locks
• Performance Monitor counts locks
• Transaction Log registers transactions. It
  doesn’t register locks
• Convert sp_lock into fn_lock
       SELECT *
       FROM ::fn_lock()
       WHERE Status = 'WAIT'

SQL Server Magazine LIVE!                      49
Using Profiler to detect locks
              (demo)




SQL Server Magazine LIVE!            50
Using Performance Monitor to
        count locks (demo)




SQL Server Magazine LIVE!     51
Detecting transactions in the
      Transaction Log (demo)




SQL Server Magazine LIVE!           52
Locking techniques from ADO.NET
• Optimistic concurrency
• Pessimistic concurrency
• User-defined concurrency




SQL Server Magazine LIVE!     53
Optimistic concurrency
• Default behavior from DataAdapter
• Based on sp_executesql
• SET clause with all new values:
   – Updated columns
   – Unchanged columns
• WHERE clause with all old values:
   – Updated columns
   – Unchanged columns

SQL Server Magazine LIVE!             54
Pessimistic Concurrency
• Implemented through SqlCommand
  objects and stored procedures
• Not scaleable:
   – Requires maintaining connection open
   – Open transaction
   – Too much locking for too much time
• Necessary in some scenarios


SQL Server Magazine LIVE!                   55
User-defined concurrency
•   Trace changes on individual columns
•   Avoid unnecessary trigger execution
•   Avoid unnecessary locks
•   Fewer conflicts
•   Requires careful design




SQL Server Magazine LIVE!                 56
User-defined concurrency from
        ADO.NET (demo)




SQL Server Magazine LIVE!     57
Application locks
• Give applications access to the SQL
  Server Lock Manager
• sp_getapplock 'resource_name',
  'lock_mode', 'lock_owner', 'lockTimeout‘
• sp_releaseapplock 'resource_name‘,
  'lock_owner' ]



SQL Server Magazine LIVE!                    58
Application locks (demo)




SQL Server Magazine LIVE!          59
Do you want to know more?
• “Inside SQL Server 2000” (Kalen Delaney, MSPress)
• “Advanced Transact-SQL for SQL Server 2000” (Itzik
  Ben-Gan & Tom Moreau, APress)
• “SQL Server 2000 Programming” (Robert Vieira, WROX)
• “Microsoft SQL Server 2000 Programming by Example”
  (Fernando G. Guerrero & Carlos Eduardo Rojas, QUE)
• SQL Server 2000 Resource Kit (MSPress & TechNet)
• Visit the Microsoft public newsgroups:
   – msnews.microsoft.com/microsoft.public.sqlserver.*
• Download the source code of this session from:
   – http://www.callsql.com/en/articles


 SQL Server Magazine LIVE!                               60
Do you want to know even
               more?
• Visit the Microsoft public newsgroups:
  – msnews.microsoft.com/microsoft.public.sql
    server.*
  – msnews.microsoft.com/microsoft.public.dot
    net.*




SQL Server Magazine LIVE!                  61
Thank you!
                      Questions?
• Download the source code of this
  session from:
   – http://www.callsql.com/en/articles
• You can contact me at:
   – fernan@guerrerog.org




SQL Server Magazine LIVE!
Thank you!
                            • Please drop off your
                              session evaluations in
                              the basket at the back
                              of the room!
                            • Your comments are
                              greatly appreciated!




SQL Server Magazine LIVE!

Mais conteĂşdo relacionado

Destaque

Solid q universidad empresa 2011 10 27
Solid q universidad empresa 2011 10 27Solid q universidad empresa 2011 10 27
Solid q universidad empresa 2011 10 27Fernando G. Guerrero
 
Dealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NETDealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NETFernando G. Guerrero
 
Achieve the Impossible: Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...
Achieve the Impossible:Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...Achieve the Impossible:Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...
Achieve the Impossible: Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...Fernando G. Guerrero
 
New gTLDs between two rounds: trade mark challenges
 New gTLDs between two rounds: trade mark challenges New gTLDs between two rounds: trade mark challenges
New gTLDs between two rounds: trade mark challengesFernando G. Guerrero
 
Itinerarios de Grado de Ingenieria Informatica EPS Alicante
Itinerarios de Grado de Ingenieria Informatica EPS AlicanteItinerarios de Grado de Ingenieria Informatica EPS Alicante
Itinerarios de Grado de Ingenieria Informatica EPS AlicanteFernando G. Guerrero
 
Arquitectura bioclimatica ing zulma cabrera
Arquitectura  bioclimatica  ing zulma cabreraArquitectura  bioclimatica  ing zulma cabrera
Arquitectura bioclimatica ing zulma cabreraEduardo Soracco
 

Destaque (7)

Solid q universidad empresa 2011 10 27
Solid q universidad empresa 2011 10 27Solid q universidad empresa 2011 10 27
Solid q universidad empresa 2011 10 27
 
Dealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NETDealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NET
 
Achieve the Impossible: Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...
Achieve the Impossible:Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...Achieve the Impossible:Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...
Achieve the Impossible: Use INSTEAD OF triggers in SQL Server 2000 to Deal Tr...
 
New gTLDs between two rounds: trade mark challenges
 New gTLDs between two rounds: trade mark challenges New gTLDs between two rounds: trade mark challenges
New gTLDs between two rounds: trade mark challenges
 
Itinerarios de Grado de Ingenieria Informatica EPS Alicante
Itinerarios de Grado de Ingenieria Informatica EPS AlicanteItinerarios de Grado de Ingenieria Informatica EPS Alicante
Itinerarios de Grado de Ingenieria Informatica EPS Alicante
 
Udf eficientes
Udf eficientesUdf eficientes
Udf eficientes
 
Arquitectura bioclimatica ing zulma cabrera
Arquitectura  bioclimatica  ing zulma cabreraArquitectura  bioclimatica  ing zulma cabrera
Arquitectura bioclimatica ing zulma cabrera
 

Mais de Fernando G. Guerrero

Dealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NETDealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NETFernando G. Guerrero
 
Datos GeomĂŠtricos y Espaciales en SQL Server 2008
Datos GeomĂŠtricos y Espaciales en SQL Server 2008Datos GeomĂŠtricos y Espaciales en SQL Server 2008
Datos GeomĂŠtricos y Espaciales en SQL Server 2008Fernando G. Guerrero
 
Microsoft Changed the Game Again and Gave New Wings to an Entire Industry
Microsoft Changed the Game Again and Gave New Wings to an Entire IndustryMicrosoft Changed the Game Again and Gave New Wings to an Entire Industry
Microsoft Changed the Game Again and Gave New Wings to an Entire IndustryFernando G. Guerrero
 
Making business sense of the continuous and anarchic flow of Social Media data
Making business sense of the continuous and anarchic flow of Social Media dataMaking business sense of the continuous and anarchic flow of Social Media data
Making business sense of the continuous and anarchic flow of Social Media dataFernando G. Guerrero
 
Designing Role-Based Database Systems to Achieve Unlimited Database Scalability
Designing Role-Based Database Systems to Achieve Unlimited Database ScalabilityDesigning Role-Based Database Systems to Achieve Unlimited Database Scalability
Designing Role-Based Database Systems to Achieve Unlimited Database ScalabilityFernando G. Guerrero
 
Data Mining for Moderation of Social Data
Data Mining for Moderation of Social DataData Mining for Moderation of Social Data
Data Mining for Moderation of Social DataFernando G. Guerrero
 

Mais de Fernando G. Guerrero (6)

Dealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NETDealing with SQL Security from ADO.NET
Dealing with SQL Security from ADO.NET
 
Datos GeomĂŠtricos y Espaciales en SQL Server 2008
Datos GeomĂŠtricos y Espaciales en SQL Server 2008Datos GeomĂŠtricos y Espaciales en SQL Server 2008
Datos GeomĂŠtricos y Espaciales en SQL Server 2008
 
Microsoft Changed the Game Again and Gave New Wings to an Entire Industry
Microsoft Changed the Game Again and Gave New Wings to an Entire IndustryMicrosoft Changed the Game Again and Gave New Wings to an Entire Industry
Microsoft Changed the Game Again and Gave New Wings to an Entire Industry
 
Making business sense of the continuous and anarchic flow of Social Media data
Making business sense of the continuous and anarchic flow of Social Media dataMaking business sense of the continuous and anarchic flow of Social Media data
Making business sense of the continuous and anarchic flow of Social Media data
 
Designing Role-Based Database Systems to Achieve Unlimited Database Scalability
Designing Role-Based Database Systems to Achieve Unlimited Database ScalabilityDesigning Role-Based Database Systems to Achieve Unlimited Database Scalability
Designing Role-Based Database Systems to Achieve Unlimited Database Scalability
 
Data Mining for Moderation of Social Data
Data Mining for Moderation of Social DataData Mining for Moderation of Social Data
Data Mining for Moderation of Social Data
 

Último

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 

Último (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Concurrency problems and locking techniques in SQL Server 2000 and VB.NET

  • 1. Session SAD335 Concurrency problems and locking techniques in SQL Server 2000 and VB.NET Fernando G. Guerrero SQL Server MVP .NET Technical Lead QA plc October 2002
  • 2. Quick info about Fernando (2 milliseconds) • MCSD, MCSE+Internet (W2K), MCDBA, MCT, QA SQL Server MVP • This is where I work: QA, The best learning environment in Europe • Writing for SQL Sever Magazine and SQL Server Professional • This is my main web site: www.callsql.com • This is my book (so far): – Microsoft SQL Server 2000 Programming by Example (ISBN : 0789724499, co-authored with Carlos Eduardo Rojas) • Currently writing on ADO.NET and SQL Server 2000 SQL Server Magazine LIVE!
  • 3. Agenda • Concurrency problems • Isolation levels • Locks • Transactions SQL Server Magazine LIVE! 3
  • 4. Concurrency problems • Lost Updates • Uncommitted Dependency • Inconsistent Analysis • Phantom Reads SQL Server Magazine LIVE! 4
  • 5. Lost Updates (1) Peter Paul Peter UnitPric UnitPric Paul e e 10.0 10.0 SQL Server Magazine LIVE! Mary 5 (SQL Server)
  • 6. Lost Updates (2) Peter Paul Peter UnitPric @UP * UnitPric Paul e 1.2 e 10.0 12.0 10.0 DECLARE @UP money SELECT @UP = UnitPrice FROM Products WHERE ProductID = 25 SQL Server Magazine LIVE! Mary 6 (SQL Server)
  • 7. Lost Updates (3) Peter Paul Peter UnitPric @UP * UnitPric @UP * Paul e 1.2 e 1.1 10.0 12.0 10.0 11.0 DECLARE @UP money DECLARE @UP money SELECT @UP = UnitPrice FROM Products WHERE ProductID = 25 SELECT @UP = UnitPrice FROM Products WHERE ProductID = 25 SQL Server Magazine LIVE! Mary 7 (SQL Server)
  • 8. Lost Updates (4) Peter Paul Peter UnitPric @UP * UnitPric @UP * Paul e 1.2 e 1.1 DECLARE @UP money 12.0 12.0 12.0 11.0 DECLARE @UP money SELECT @UP = UnitPrice SELECT @UP = UnitPrice FROM Products FROM Products WHERE ProductID = 25 WHERE ProductID = 25 UPDATE Products SET UnitPrice = @UP * 1.2 WHERE ProductID = 25 SQL Server Magazine LIVE! Mary 8 (SQL Server)
  • 9. Lost Updates (5) Peter Paul Peter UnitPric @UP * UnitPric @UP * Paul e 1.2 e 1.1 DECLARE @UP money 11.0 12.0 11.0 11.0 DECLARE @UP money SELECT @UP = UnitPrice SELECT @UP = UnitPrice FROM Products FROM Products WHERE ProductID = 25 WHERE ProductID = 25 UPDATE Products SET UnitPrice = @UP * 1.2 WHERE ProductID = 25 UPDATE Products SET UnitPrice = @UP * 1.1 WHERE ProductID = 25 SQL Server Magazine LIVE! Mary 9 (SQL Server)
  • 10. Uncommitted Dependency (1) Peter Paul Peter UnitPric UnitPric Paul e e 10.0 10.0 SQL Server Magazine LIVE! Mary 10 (SQL Server)
  • 11. Uncommitted Dependency (2) Peter Paul Peter UnitPric UnitPric Paul e e 12.0 12.0 BEGIN TRANSACTION UPDATE PRODUCTS SET UnitPrice = UnitPrice * 1.2 WHERE ProductID = 25 SQL Server Magazine LIVE! Mary 11 (SQL Server)
  • 12. Uncommitted Dependency (3) Peter Paul Peter UnitPric UnitPric @UP Paul e e 12.0 12.0 12.0 BEGIN TRANSACTION DECLARE @UP money UPDATE PRODUCTS SET UnitPrice = UnitPrice * 1.2 WHERE ProductID = 25 SELECT @UP = UnitPrice FROM Products (NOLOCK) WHERE ProductID = 25 SQL Server Magazine LIVE! Mary 12 (SQL Server)
  • 13. Uncommitted Dependency (4) Peter Paul Peter UnitPric UnitPric @UP Paul e e BEGIN TRANSACTION 12.0 10.0 12.0 10.0 12.0 DECLARE @UP money UPDATE PRODUCTS SELECT @UP = UnitPrice SET UnitPrice = UnitPrice * 1.2 FROM Products WHERE ProductID = 25 WHERE ProductID = 25 ROLLBACK TRANSACTION SQL Server Magazine LIVE! Mary 13 (SQL Server)
  • 14. Uncommitted Dependency (5) Peter Paul Peter UnitPric UnitPric @UP Paul e e BEGIN TRANSACTION 10.0 10.0 12.0 DECLARE @UP money UPDATE PRODUCTS SELECT @UP = UnitPrice SET UnitPrice = UnitPrice * 1.2 FROM Products WHERE ProductID = 25 WHERE ProductID = 25 ROLLBACK TRANSACTION INSERT [Order details] ( OrderID, ProductID, UnitPrice, Quantity, Discount) VALUES (25365, 25, SQL Server Magazine LIVE! Mary @UP, 10, 0.1) 14 (SQL Server)
  • 15. Inconsistent Analysis (1) Peter Peter Paul SQL Server Magazine LIVE! Mary 15 (SQL Server)
  • 16. Inconsistent Analysis (2) Peter Peter @Count 830 Paul @Total @Average @Total / DECLARE @Count int, @Count @Total money, @Average money SELECT @Count = COUNT(DISTINCT OrderID) FROM [Order Details] Mary SQL Server Magazine LIVE! 16 (SQL Server)
  • 17. Inconsistent Analysis (3) Peter Peter @Count 830 Paul @Total DECLARE @Count int, @Total money, @Average @Average money SELECT @Count = @Total / COUNT(DISTINCT OrderID) @Count FROM [Order Details] UPDATE [Order details SET Quantity = 600 WHERE OrderID = 10272 AND ProductID = 20 SQL Server Magazine LIVE! Mary 17 (SQL Server)
  • 18. Inconsistent Analysis (4) Peter Peter @Count 830 Paul @Total 1304284.2 DECLARE @Count int, 4 UPDATE [Order details] @Total money, SET Quantity = 600 @Average money @Average WHERE OrderID = 10272 SELECT @Count = AND ProductID = 20 COUNT(DISTINCT OrderID) @Total / 1571.43 FROM [Order Details] @Count SELECT @Total = SUM(UnitPrice * Quantity * (1 – Discount)) FROM [Order Details] SQL Server Magazine LIVE! Mary 18 (SQL Server)
  • 19. Inconsistent Analysis (5) Peter Peter @Count 830 Paul @Total 1304284.2 DECLARE @Count int, 4 UPDATE [Order details] @Total money, SET Quantity = 600 @Average money @Average WHERE OrderID = 10272 SELECT @Count = AND ProductID = 20 COUNT(DISTINCT OrderID) @Total / 1571.43 FROM [Order Details] @Count SELECT @Total = SUM(UnitPrice * Quantity * (1 – Discount)) FROM [Order Details] UPDATE [Order details] SET Discount = 0.4 WHERE ProductID = SQL Server Magazine LIVE! Mary20 19 (SQL Server)
  • 20. Inconsistent Analysis (6) Peter Peter @Count 830 Paul @Total 1304284.2 DECLARE @Count int, 4 UPDATE [Order details] @Total money, SET Quantity = 600 @Average money @Average 1542.78 WHERE OrderID = 10272 SELECT @Count = AND ProductID = 20 COUNT(DISTINCT OrderID) @Total / 1571.43 FROM [Order Details] @Count UPDATE [Order details] SET Discount = 0.4 SELECT @Total = WHERE ProductID = 20 SUM(UnitPrice * Quantity * (1 – Discount)) FROM [Order Details] SELECT @Average = AVG(TotalPrice) FROM (…) AS TotOrders SQL Server Magazine LIVE! Mary 20 (SQL Server)
  • 21. Phantom Reads (1) OrderID ProductI UnitPrice Peter D Paul 10259 37 20.8 10337 37 20.8 10408 37 20.8 10523 37 26.0 10847 37 26.0 10966 37 26.0 SELECT OrderID, ProductID, UnitPrice FROM [Order Details] WHERE ProductID = 37 SQL Server Magazine LIVE! Mary 21 (SQL Server)
  • 22. Phantom Reads (2) OrderID ProductI UnitPrice Peter D Paul 10259 37 20.80 10337 37 20.80 SELECT OrderID, ProductID, 10408 37 20.80 UnitPrice FROM [Order Details] 10523 37 26.00 WHERE ProductID = 37 10847 37 26.00 10966 37 26.00 10615 37 INSERT [Order 31.54 details] (OrderID, ProductID, UnitPrice, MaryQuantity, (SQL Server) Discount) SQL Server Magazine LIVE! 22
  • 23. Phantom Reads (3) OrderID ProductI UnitPrice Peter D Paul 10259 37 20.80 10337 37 20.80 SELECT OrderID, INSERT [Order details] ProductID, 10408 37 20.80 (OrderID, ProductID, UnitPrice UnitPrice, Quantity, FROM [Order Details] 10523 37 26.00 Discount) WHERE ProductID = 37 10847 37 26.00 VALUES (10615, 37, 31.54, 20, 0.1) 10966 37 26.00 10615 37 31.54 SELECT OrderID, ProductID, UnitPrice FROM [Order Details] WHERE ProductID = 37 SQL Server Magazine LIVE! Mary 23 (SQL Server)
  • 24. Isolation levels • Transact-SQL: – READ COMMITTED – READ UNCOMMITTED – REPEATABLE READ – SERIALIZABLE • Extra .NET Isolation Levels: – Chaos (not valid for SQLClient) – Unspecified (not settable for SQLClient) SQL Server Magazine LIVE! 24
  • 25. Isolation levels vs. Concurrency problems P Problem Isolation S Solution Level X SERIALIZABLE solved by standard UNCOMMITTED REPEATABLE COMMITTED exclusive locks READ READ READ inside transactions Concurrency Problem Lost Update X X X X Dirty Read P S S S Inconsiste SQL Server Magazine LIVE! nt P P S S 25 Analysis
  • 26. READ COMMITTED • Default isolation level • Avoids Dirty Reads – m yTr = an m yconn.Begi ans i Iol i nTr acton( s atonLevelReadCom m it . t ed) –S ELECT … FRO M … W I ( TH READCO M M I TTED) –S ELECT … FRO M … W I ( TH READPAS T) – S TRANS ET ACTI N IO LATI N LEVEL READ CO M M I O S O TTED • Requests Shared locks for the duration of the reading operation • Requests Exclusive locks for each modification 26 SQL Server Magazine LIVE!
  • 27. READ UNCOMMITTED • Isolation level only suitable for “sneaking around” – m yTr = an m yconn.Begi ans i Iol i nTr acton( s atonLevelReadUnCom m . it ted) –S ELECT … FRO M … W I ( TH READUNCO M M ITTED) –S ELECT … FRO M … W I ( LO CK) TH NO – S TRANS ET ACTI N IO LATI N LEVEL READ UNCO M M I O S O TTED • Doesn’t request Shared locks at all • Requests Exclusive locks for each modification SQL Server Magazine LIVE! 27
  • 28. REPEATABLE READ • Quite a restrictive isolation level • Avoids all concurrency problems, except Phantom Reads – m yTr = an m yconn.Begi ans i Iol i nTr acton( s atonLevelRepeat eRe . abl ad) –S ELECT … FRO M … W I ( TH REPEATABLEREAD) – S TRANS ET ACTI N IO LATI N LEVEL REPEATABLE READ O S O • Requests Shared locks for the duration of the transaction • Requests Exclusive locks for each modification 28 SQL Server Magazine LIVE!
  • 29. SERIALIZABLE • The most restrictive isolation level • Avoids all concurrency problems – m yTr = an m yconn.Begi ans i Iol i nTr acton( s atonLevelS i i e) . eralzabl –S ELECT … FRO M … W I ( ERI ZABLE) TH S ALI –S ELECT … FRO M … W I ( LDLO CK) TH HO – S TRANS ET ACTI N IO LATI N LEVEL S ALI O S O ERI ZABLE • Requests Shared locks for the duration of the transaction • Requests Exclusive locks for each modification SQL Server Magazine LIVE! 29
  • 30. Transactions • Transact-SQL transactions • Distributed transactions • .NET Manual transactions • .NET Automatic transactions SQL Server Magazine LIVE! 30
  • 31. Transact-SQL transactions • Transaction Statements • Nested transactions • Transactions and Stored Procedures • Transactions and Triggers SQL Server Magazine LIVE! 31
  • 32. Managing transactions with Transact-SQL Statements • BEGIN TRAN • COMMIT TRAN • ROLLBACK TRAN SQL Server Magazine LIVE! 32
  • 33. Transaction Savepoints • SAVE TRAN TranName • ROLLBACK TRAN TranName SQL Server Magazine LIVE! 33
  • 34. Nested transactions • @@TRANCOUNT tells you how many transaction levels you are in • For SQL Server there is only one actual transaction • Commit happens only when all nested transactions are ended • Rollback cancels all nested transactions at once SQL Server Magazine LIVE! 34
  • 35. Transactions and Stored Procedures • After Rollback execution continues, but you are outside transaction boundaries • If Rollback happens inside a procedure, the calling process receives error 266, Level 16: – Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 0. • Good idea to use save points and inform the outer process using output parameters SQL Server Magazine LIVE! 35
  • 36. Transactions and Triggers • After Rollback execution continues inside the trigger, but you are outside transaction boundaries and the process terminates when the trigger does. • Consider using INSTEAD OF triggers to minimize rollbacks • Consider using cancelling operations instead of rollbacks SQL Server Magazine LIVE! 36
  • 38. .NET Manual transactions SQL Server Magazine LIVE! 38
  • 39. .NET Automatic transactions • Apply the TransactionAttribute to your class. • Derive your class from the ServicedComponent Class. • Sign the assembly with a strong name. – To sign the assembly using attributes create a key pair using the Sn.exe utility. – sn -k MCTCon.snk SQL Server Magazine LIVE! 39
  • 40. .NET Automatic transactions (2) <Assembly: ApplicationName("MCTCON")> <Assembly: AssemblyKeyFileAttribute("MCTCON.snk")> <Transaction(TransactionOption.Required)> Public Class clsProduct Inherits ServicedComponent Dim myConnection As SqlConnection <AutoComplete()> Public Sub RaisePrice(ByVal ProductID As Integer, ByVal amount As Integer) OpenConnection() Updateproduct(ProductID, amount) CloseConnection() End Sub SQL Server Magazine LIVE! 40
  • 41. .NET Automatic transactions (3) SQL Server Magazine LIVE! 41
  • 42. Locks • Dynamic locking strategy • Locking SQL Server resources • Types of locks • Hunting for locks SQL Server Magazine LIVE! 42
  • 43. Dynamic locking strategy • SQL Server tries to minimize locking costs balancing: – Lock granularity – Lock maintenance cost • SQL Server 2000 defaults to row lock when necessary • Uses latches, lightweight synchronization objects, for internal operations, minimizing expensive locks SQL Server Magazine LIVE! 43
  • 44. Locking SQL Server resources • SQL Server 2000 can lock: – Data Row – Index Key – Any page – Extent – Table – Database SQL Server Magazine LIVE! 44
  • 45. Types of locks • Shared (S) • Update (U) • Exclusive (X) • Intent: – intent shared (IS) – intent exclusive (IX) – shared with intent exclusive (SIX) • Schema: – schema modification (Sch-M) – schema stability (Sch-S). • Bulk Update (BU) SQL Server Magazine LIVE! 45
  • 46. A typical case of Deadlock involving two connections (demo) SQL Server Magazine LIVE! 46
  • 47. A Deadlock situation involving more than two connections (demo) SQL Server Magazine LIVE! 47
  • 48. Binding connections (demo) SQL Server Magazine LIVE! 48
  • 49. Hunting for locks • Profiler can detect locks • Performance Monitor counts locks • Transaction Log registers transactions. It doesn’t register locks • Convert sp_lock into fn_lock SELECT * FROM ::fn_lock() WHERE Status = 'WAIT' SQL Server Magazine LIVE! 49
  • 50. Using Profiler to detect locks (demo) SQL Server Magazine LIVE! 50
  • 51. Using Performance Monitor to count locks (demo) SQL Server Magazine LIVE! 51
  • 52. Detecting transactions in the Transaction Log (demo) SQL Server Magazine LIVE! 52
  • 53. Locking techniques from ADO.NET • Optimistic concurrency • Pessimistic concurrency • User-defined concurrency SQL Server Magazine LIVE! 53
  • 54. Optimistic concurrency • Default behavior from DataAdapter • Based on sp_executesql • SET clause with all new values: – Updated columns – Unchanged columns • WHERE clause with all old values: – Updated columns – Unchanged columns SQL Server Magazine LIVE! 54
  • 55. Pessimistic Concurrency • Implemented through SqlCommand objects and stored procedures • Not scaleable: – Requires maintaining connection open – Open transaction – Too much locking for too much time • Necessary in some scenarios SQL Server Magazine LIVE! 55
  • 56. User-defined concurrency • Trace changes on individual columns • Avoid unnecessary trigger execution • Avoid unnecessary locks • Fewer conflicts • Requires careful design SQL Server Magazine LIVE! 56
  • 57. User-defined concurrency from ADO.NET (demo) SQL Server Magazine LIVE! 57
  • 58. Application locks • Give applications access to the SQL Server Lock Manager • sp_getapplock 'resource_name', 'lock_mode', 'lock_owner', 'lockTimeout‘ • sp_releaseapplock 'resource_name‘, 'lock_owner' ] SQL Server Magazine LIVE! 58
  • 59. Application locks (demo) SQL Server Magazine LIVE! 59
  • 60. Do you want to know more? • “Inside SQL Server 2000” (Kalen Delaney, MSPress) • “Advanced Transact-SQL for SQL Server 2000” (Itzik Ben-Gan & Tom Moreau, APress) • “SQL Server 2000 Programming” (Robert Vieira, WROX) • “Microsoft SQL Server 2000 Programming by Example” (Fernando G. Guerrero & Carlos Eduardo Rojas, QUE) • SQL Server 2000 Resource Kit (MSPress & TechNet) • Visit the Microsoft public newsgroups: – msnews.microsoft.com/microsoft.public.sqlserver.* • Download the source code of this session from: – http://www.callsql.com/en/articles SQL Server Magazine LIVE! 60
  • 61. Do you want to know even more? • Visit the Microsoft public newsgroups: – msnews.microsoft.com/microsoft.public.sql server.* – msnews.microsoft.com/microsoft.public.dot net.* SQL Server Magazine LIVE! 61
  • 62. Thank you! Questions? • Download the source code of this session from: – http://www.callsql.com/en/articles • You can contact me at: – fernan@guerrerog.org SQL Server Magazine LIVE!
  • 63. Thank you! • Please drop off your session evaluations in the basket at the back of the room! • Your comments are greatly appreciated! SQL Server Magazine LIVE!