事务隔离级别范围

发布于 2024-07-25 07:18:05 字数 484 浏览 5 评论 0原文

SQL Server 2005 中事务隔离级别的范围规则是什么? 我知道不同级别的含义,但不知道如何在手动运行脚本之外正确应用它们。 我找不到生产质量代码的实际使用指南。

显然,当您使用这样的命令时,范围就开始了:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 

但是它在哪里结束呢? 如果我在存储过程中设置 iso 级别,然后该过程调用另一个过程,嵌套过程是否会继承它? 更好的是,如果我升级嵌套过程中的 iso 级别,它是否会返回到调用过程中? BEGIN TRAN、ROLLBACK 和 COMMIT 等事务命令有什么区别吗?

当应用程序或代理作业调用存储过程时,隔离级别更改是否会以某种方式持续存在? 我是否总是必须在每个过程结束时恢复为默认的 READ COMMITTED?

我会在不同的情况下测试它,但我不知道如何读取当前隔离级别的设置。

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 manually run script. I can't find a guide for practical use in production-quality code.

Obviously, the scope begins when you use a command like this:

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 

But where does it end? If I set the iso level in a stored procedure and then that proc calls another, does the nested proc inherit it? Even better, if I escalate the iso level inside the nested proc is it going to carry back out into the calling proc? Do transaction commands like BEGIN TRAN, ROLLBACK, and COMMIT make any differences?

When a stored proc is called by an application or an agent job do the isolation level changes persist in some way? Do I always have to revert to the default READ COMMITTED at the end of each proc?

I would test it in different situations but I don't know how to read what the current isolation level is set to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

温柔一刀 2024-08-01 07:18:06

运行以下命令并亲自查看:

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;

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;
婴鹅 2024-08-01 07:18:06

来自 MSDN

如果您在存储过程中发出 SET TRANSACTION ISOLATION LEVEL
触发,当对象返回控制时,隔离级别重置
到调用对象时有效的级别
。 例如,如果
您在批处理中设置 REPEATABLE READ,然后该批处理调用存储的
将隔离级别设置为 SERIALIZABLE 的过程,隔离
当存储过程执行时,级别设置恢复为 REPEATABLE READ
将控制权返回给批处理。

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.

属性 2024-08-01 07:18:06

DBCC USEROPTIONS 将显示当前的隔离级别以及所有其他 SET 选项。

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

深府石板幽径 2024-08-01 07:18:06

摘自网上书籍

只有一个事务隔离
LEVEL选项可以一次设置,
并且它仍然为该连接设置
直到它被明确改变。 这
成为默认行为,除非
优化选项指定于
FROM 子句中的表级别
声明。

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.

罪#恶を代价 2024-08-01 07:18:06

隔离级别不会随事务回滚。

即使您调用过程和函数,隔离级别也保持最新状态。

Isolation level does not roll back with the transaction.

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

浅唱々樱花落 2024-08-01 07:18:06

请注意,当使用高性能 ADO.NET 连接池时,事务级别和 SqlServer 2012 或更早版本的范围存在问题,在连接关闭时该问题实际上会持续存在:

SQL Server:跨池连接的隔离级别泄漏

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文