Transaction Isolation Level Scopes

后端 未结 6 1309
醉话见心
醉话见心 2020-12-24 05:37

What are the scoping rules for transaction isolation levels in SQL Server 2005? I know what the different levels mean, but not how to properly apply them outside of a manual

相关标签:
6条回答
  • 2020-12-24 05:53

    Please note there is an issue with scope of TRANSACTION LEVELS and SqlServer 2012 or earlier when high performance ADO.NET Connection Pooling is used where this can actually persist across closing of the connection:

    SQL Server: Isolation level leaks across pooled connections

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/916b3d8a-c464-4ad5-8901-6f845a2a3447/sql-server-2014-reseting-isolation-level?forum=sqldatabaseengine

    0 讨论(0)
  • 2020-12-24 05:54

    From books on line

    Only one of the TRANSACTION ISOLATION LEVEL options can be set at a time, and it remains set for that connection until it is explicitly changed. This becomes the default behavior unless an optimization option is specified at the table level in the FROM clause of the statement.

    0 讨论(0)
  • 2020-12-24 06:06

    Isolation level does not roll back with the transaction.

    Isolation level stays current even if you call into procedures and functions.

    0 讨论(0)
  • 2020-12-24 06:07

    DBCC USEROPTIONS will display the current isolation level, along with all of the other SET options.

    0 讨论(0)
  • 2020-12-24 06:10

    From MSDN

    If you issue SET TRANSACTION ISOLATION LEVEL in a stored procedure or trigger, when the object returns control the isolation level is reset to the level in effect when the object was invoked. For example, if you set REPEATABLE READ in a batch, and the batch then calls a stored procedure that sets the isolation level to SERIALIZABLE, the isolation level setting reverts to REPEATABLE READ when the stored procedure returns control to the batch.

    0 讨论(0)
  • 2020-12-24 06:14

    Run the following and see for yourself:

    CREATE PROCEDURE dbo.KeepsIsolation
    AS
    BEGIN
    PRINT 'Inside sproc that does not change isolation level';
    DBCC USEROPTIONS;
    END
    GO
    
    CREATE PROCEDURE dbo.ChangesIsolation
    AS
    BEGIN
    PRINT 'Inside sproc that changes isolation level';
    SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
    DBCC USEROPTIONS;
    END
    GO
    SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    DBCC USEROPTIONS;
    EXEC dbo.KeepsIsolation;
    DBCC USEROPTIONS;
    EXEC dbo.ChangesIsolation;
    -- demonstrates that isolation level restored to REPEATABLE READ after exiting the procedure
        DBCC USEROPTIONS;
    
    0 讨论(0)
提交回复
热议问题