質問

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?

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top