Aracılığıyla paylaş


Satır sürüm oluşturma tabanlı yalıtım düzeyi örneği

Aşağıdaki örnekler, davranış anlık görüntü yalıtım hareketleri ve okunur kaydedilen işlem sayısı, satır sürüm oluşturma'yı kullanma arasındaki farklar

Not

Satır sürüm oluşturma tabanlı yalıtım düzeyleri aşağıdaki örnekleri gözden geçirme önce anlamak da önemlidir.See Satır sürüm oluşturma tabanlı yalıtım düzeyleri anlama and Satır sürüm oluşturma tabanlı yalıtım düzeyleri kullanma.

Örnekler

A.anlık görüntü görüntü yalıtım ile çalışma

Bu örnekte, anlık görüntü yalıtım altında çalışan bir işlem daha sonra başka bir hareket tarafından değiştirilen verileri okur.Anlık görüntü hareket yok blok update işlemi diğer hareket tarafından yürütülen ve veri değişikliği yok sayarak sürüm bilgisi satırdan veri okumaya devam eder.Ancak, anlık görüntü işlem başka bir işlem tarafından değiştirilmiş verileri değiştirme girişiminde bulunduğunda, anlık görüntü işlem bir hata üretir ve sona erdirilir.

Oturum 1:

USE AdventureWorks2008R2;
GO

-- Enable snapshot isolation on the database.
ALTER DATABASE AdventureWorks2008R2
    SET ALLOW_SNAPSHOT_ISOLATION ON;
GO

-- Start a snapshot transaction
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
GO

BEGIN TRANSACTION;
    -- This SELECT statement will return
    -- 48 vacation hours for the employee.
    SELECT BusinessEntityID, VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

Oturum 2:

USE AdventureWorks2008R2;
GO

-- Start a transaction.
BEGIN TRANSACTION;
    -- Subtract a vacation day from employee 4.
    -- Update is not blocked by session 1 since
    -- under snapshot isolation shared locks are
    -- not requested.
    UPDATE HumanResources.Employee
        SET VacationHours = VacationHours - 8
        WHERE BusinessEntityID = 4;

    -- Verify that the employee now has 40 vacation hours.
    SELECT VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

Oturum 1:

    -- Reissue the SELECT statement - this shows
    -- the employee having 48 vacation hours.  The
    -- snapshot transaction is still reading data from
    -- the versioned row.
    SELECT BusinessEntityID, VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

Oturum 2:

-- Commit the transaction; this commits the data
-- modification.
COMMIT TRANSACTION;
GO

Oturum 1:

    -- Reissue the SELECT statement - this still 
    -- shows the employee having 48 vacation hours
    -- even after the other transaction has committed
    -- the data modification.
    SELECT BusinessEntityID, VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

    -- Because the data has been modified outside of the
    -- snapshot transaction, any further data changes to 
    -- that data by the snapshot transaction will cause 
    -- the snapshot transaction to fail. This statement 
    -- will generate a 3960 error and the transaction will 
    -- terminate.
    UPDATE HumanResources.Employee
        SET SickLeaveHours = SickLeaveHours - 8
        WHERE BusinessEntityID = 4;

-- Undo the changes to the database from session 1. 
-- This will not undo the change from session 2.
ROLLBACK TRANSACTION
GO

B.Okuma kaydedilmiş çalışma satır sürüm oluşturma'yı kullanma

Bu örnekte, satır sürüm oluşturma'yı kullanarak okuma kaydedilmiş bir hareket başka bir işlem aynı anda çalışır.Okuma kaydedilmiş hareket anlık görüntü görüntü hareketi farklı şekilde davranır.Verileri bir hareket bile değiştirdi sonra anlık görüntü görüntü hareketi gibi okunur kaydedilen işlem sürüm bilgisi satır okuyun.Ancak, anlık görüntü görüntü hareketi okunur kaydedilmiş hareket eder:

  • Veri değişiklikleri diğer hareketi tamamlar sonra değiştirilmiş verileri okuyabilir.

  • Burada anlık görüntü hareket değil diğer hareket tarafından değiştirilen verileri güncelleştirmek mümkün olabilir.

Oturum 1:

USE AdventureWorks2008R2;
GO

-- Enable READ_COMMITTED_SNAPSHOT on the database.
-- For this statement to succeed, this session
-- must be the only connection to the AdventureWorks2008R2
-- database.
ALTER DATABASE AdventureWorks2008R2
    SET READ_COMMITTED_SNAPSHOT ON;
GO

-- Start a read-committed transaction
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
GO

BEGIN TRANSACTION;
    -- This SELECT statement will return
    -- 48 vacation hours for the employee.
    SELECT BusinessEntityID, VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

Oturum 2:

USE AdventureWorks2008R2;
GO

-- Start a transaction.
BEGIN TRANSACTION;
    -- Subtract a vacation day from employee 4.
    -- Update is not blocked by session 1 since
    -- under read-committed using row versioning shared locks are
    -- not requested.
    UPDATE HumanResources.Employee
        SET VacationHours = VacationHours - 8
        WHERE BusinessEntityID = 4;

    -- Verify that the employee now has 40 vacation hours.
    SELECT VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

Oturum 1:

    -- Reissue the SELECT statement - this still shows
    -- the employee having 48 vacation hours.  The
    -- read-committed transaction is still reading data 
    -- from the versioned row and the other transaction 
    -- has not committed the data changes yet.
    SELECT BusinessEntityID, VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

Oturum 2:

-- Commit the transaction.
COMMIT TRANSACTION;
GO

Oturum 1:

    -- Reissue the SELECT statement which now shows the 
    -- employee having 40 vacation hours.  Being 
    -- read-committed, this transaction is reading the 
    -- committed data. This is different from snapshot
    -- isolation which reads from the versioned row.
    SELECT BusinessEntityID, VacationHours
        FROM HumanResources.Employee
        WHERE BusinessEntityID = 4;

    -- This statement, which caused the snapshot transaction 
    -- to fail, will succeed with read-committed using row versioning.
    UPDATE HumanResources.Employee
        SET SickLeaveHours = SickLeaveHours - 8
        WHERE BusinessEntityID = 4;

-- Undo the changes to the database from session 1. 
-- This will not undo the change from session 2.
ROLLBACK TRANSACTION;
GO