Why is System.Transactions TransactionScope default Isolationlevel Serializable

后端 未结 3 1275
我在风中等你
我在风中等你 2020-11-28 22:22

I am just wondering what a good reason to use Serializable as the default Isolationlevel may be when creating a System.Transactions TransactionScope

3条回答
  •  时光取名叫无心
    2020-11-28 23:07

    A useful way to cut down writing boilerplate code is to wrap it in a builder class like so:

    public static class TransactionScopeBuilder
    {
        /// 
        /// Creates a transactionscope with ReadCommitted Isolation, the same level as sql server
        /// 
        /// A transaction scope
        public static TransactionScope CreateReadCommitted()
        {
            var options = new TransactionOptions
            {
                IsolationLevel = IsolationLevel.ReadCommitted,
                Timeout = TransactionManager.DefaultTimeout
            };
    
            return new TransactionScope(TransactionScopeOption.Required, options);
        } 
    }
    

    Then you can use it like this when creating a transaction scope:

    using (var scope = TransactionScopeBuilder.CreateReadCommitted())
    {
        //do work here
    }
    

    You can add other common transaction scope defaults to the builder class as you need them.

提交回复
热议问题