Get current .net TransactionScope IsolationLevel

£可爱£侵袭症+ 提交于 2019-12-04 06:03:54

问题


I have an utility method creating TransactionScope in my application.

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 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?


回答1:


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

EDIT

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;
    }
}

You can get the current transaction by calling Transaction.Current.

Source: Implementing an Implicit Transaction using Transaction Scope



来源:https://stackoverflow.com/questions/19102726/get-current-net-transactionscope-isolationlevel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!