隔离级别继承 [英] Isolation level inheritance

查看:96
本文介绍了隔离级别继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的代码定义了一个事务范围。但有一点我不了解事务范围的隔离级别。如果我有一行代码超出事务范围,隔离级别是否会继承到代码行?

以下是我的例子:

Hi, I have a transaction scope defined for my codes. But there is one thing I dont understand about the isolation level for transaction scope. If I have a line of code that is outside of the transaction scope, would the isolation level be inherited into the line of codes?
Below are my example:

using (TransactionScope t = new TransactionScope(TransactionScopeOption.RequiresNew, TransactionUtil.GetTransactionOptions(600)))
{
//do something here
//isolation level is ReadCommitted
}
//new line of codes here





任何帮助将不胜感激。谢谢。



我尝试了什么:



1.研究在线文章解释。



Any help would be appreciated. Thank you.

What I have tried:

1. Researched online for article explanation.

推荐答案

这个CodeProject文章所有关于TransactionScope [ ^ ]详细介绍了TransactionScope类,它可以为您提供帮助。



您还需要了解与SQL Server一起使用的类的问题 - 请参阅此文章使用新的TransactionScope()认为有害| David Browne&#0的Web日志 [ ^ ]



我的直觉是新的代码行不是继承隔离级别,就像在sql中的存储过程中设置隔离级别一样,隔离级别将恢复为SP终止时的状态。您可以通过创建新事务(不设置隔离级别)并查询Transaction.IsolationLevel属性来证明或反驳这一点。



如果这是用于SQL Server连接然后你可以通过查询连接上当前的隔离级别来确认或拒绝该行为(假设你使用相同的隔离级别) - 本文有一些C#代码用于执行此操作获取和设置SQL实体上的事务隔离级别连接 [ ^ ]



无论你是什么,都要尝试经验证据在您自己的代码中使用TransactionScope。



就个人而言,我绝不会认为该级别是继承的,并且会自己明确设置。
This CodeProject article All About TransactionScope[^] goes into some detail about the TransactionScope class, which may help you.

You also need to be aware of an issue with the class for use with SQL Server - see this article using new TransactionScope() Considered Harmful | David Browne's Web Log[^]

My gut feel would be that the new lines of code would not inherit the isolation level, in the same way that if isolation level is set within a stored procedure in sql, the isolation level reverts back to whatever it was when the SP terminates. You can prove or disprove this by creating a new transaction (without setting isolation level) and querying the Transaction.IsolationLevel property.

If this is being used for a SQL Server connection then you can confirm or deny the behaviour by querying the Isolation Level currently on the connection (assuming you are using the same one) - This article has some C# code for doing that Getting and setting the Transaction Isolation Level on a SQL Entity Connection[^]

Try for the empirical evidence in whatever you are using TransactionScope for in your own code.

Personally I would never assume that the level is inherited and would explicitly set it myself.


这篇关于隔离级别继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆