获取当前的.net TransactionScope IsolationLevel [英] Get current .net TransactionScope IsolationLevel
问题描述
我有一个实用方法在我的应用程序中创建TransactionScope.
I have an utility method creating TransactionScope in my application.
我想进行单元测试,以验证返回的TransactionScope是否设置了正确的IsolationLevel,以确保没有人可以在不破坏测试的情况下修改代码.
I want to do a unit test to validate that the returned TransactionScope has the correct IsolationLevel set, to be sure that nobody can modify the code without breaking the tests.
System.Transactions.Transaction范围没有公开此类信息的公共属性.( http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx )
System.Transactions.Transaction scope does not have public properties exposing information like that. (http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx)
有什么办法获取此信息?
Any way to get this information?
推荐答案
是否可以运行SQL查询来检查隔离级别:
Can you run a SQL query to check the isolation level:
SELECT CASE transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'ReadUncomitted'
WHEN 2 THEN 'Readcomitted'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot' END AS IsolationLevel
FROM sys.dm_exec_sessions
where session_id = @@SPID
编辑
根据下面的评论,还有一种方法可以从代码中获取隔离级别.可能是这样的:
Based on the comments below, there's also a way to get the isolation level from the code. This could be something like that:
using (TransactionScope scope = new TransactionScope())
{
using (SqlConnection connection1 = new SqlConnection("Data Source=localhost;Integrated Security=True"))
{
Transaction trans = Transaction.Current;
System.Transactions.IsolationLevel level = trans.IsolationLevel;
}
}
您可以通过调用 Transaction.Current
获得当前交易.
You can get the current transaction by calling Transaction.Current
.
来源:使用事务实现隐式事务范围
这篇关于获取当前的.net TransactionScope IsolationLevel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!