事务隔离级别范围
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
运行以下命令并亲自查看:
Run the following and see for yourself:
来自 MSDN
From MSDN
DBCC USEROPTIONS
将显示当前的隔离级别以及所有其他 SET 选项。DBCC USEROPTIONS
will display the current isolation level, along with all of the other SET options.摘自网上书籍
From books on line
隔离级别不会随事务回滚。
即使您调用过程和函数,隔离级别也保持最新状态。
Isolation level does not roll back with the transaction.
Isolation level stays current even if you call into procedures and functions.
请注意,当使用高性能 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