我很好奇在数据库上使用IsolationLevel.Snapshot并将READ_COMMITTED_SNAPSHOT设置为ON会有什么影响。IsolationLevel枚举的documentation记录了快照隔离的行为,而这并不是我们在我们的情况下想要的。
启用READ_COMMITTED_SNAPSHOT后,我们应该指定IsolationLevel.Unspecified,还是根本不提供此值?或者,如果我们指定了IsolationLevel.Snapshot,我们会实现启用READ_COMMITTED_SNAPSHOT时的预期行为吗?
谢谢!
发布于 2013-01-17 06:23:11
如果您在DB级别启用了read_committed_snapshot,那么除非进行修改,否则所有查询都将具有该默认隔离级别。
如果更改查询本身的隔离级别,则查询将使用修改时使用的隔离级别。
发布于 2013-10-22 11:45:11
我用SQL2008 R2和Entity Framework4做了下面的测试(数据库的READ_COMMITTED_SNAPSHOT选项是打开的)
我创建了以下存储过程来返回上下文隔离级别(最初来自here):
Create Procedure TempTestIsolation
AS
Begin
DECLARE @UserOptions TABLE(SetOption varchar(100), Value varchar(100))
INSERT @UserOptions
EXEC('DBCC USEROPTIONS WITH NO_INFOMSGS')
SELECT Value
FROM @UserOptions
WHERE SetOption = 'isolation level'
End然后我编写了以下测试代码:
static void Main(string[] args)
{
var entities = new MyEntities();
// Execute the SP to get the isolation level
string level = entities.TempTestIsolation().First().Value;
Console.WriteLine("Without a transaction: " + level);
var to = new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.Snapshot };
using (var ts = new TransactionScope(TransactionScopeOption.Required, to))
{
// Execute the SP to get the isolation level
level = entities.TempTestIsolation().First().Value;
Console.WriteLine("With IsolationLevel.Snapshot: " + level);
}
to = new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted };
using (var ts = new TransactionScope(TransactionScopeOption.Required, to))
{
// Execute the SP to get the isolation level
level = entities.TempTestIsolation().First().Value;
Console.WriteLine("With IsolationLevel.ReadCommitted: " + level);
}
Console.ReadKey();
}它的输出是:

正如您所看到的,当您在TransactionOptions中将IsolationLevel设置为Snapshot时,存储过程将在"Snapshot“隔离级别下执行,并且在"Read Committed”下执行NOT。
相反,如果您将IsolationLevel设置为ReadCommitted it ,则会在"Read Committed“下执行。
希望能有所帮助。
https://stackoverflow.com/questions/14365676
复制相似问题