开发者

Importance of using TransactionScope

开发者 https://www.devze.com 2023-01-16 04:50 出处:网络
What is the difference between using using (var scope = ne开发者_开发技巧w TransactionScope(TransactionScopeOption.Required))

What is the difference between using

using (var scope = ne开发者_开发技巧w TransactionScope(TransactionScopeOption.Required))
        {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(entity);
                transaction.Commit();
            }
        }
        scope.Complete();
        } 

and simply using ?

using (ISession session = NHibernateHelper.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(entity);
                transaction.Commit();
            }
        }

What are the advantages and disadvanteges and when they are appropriate to use?


Since you are using both TransactonScope and NHibernate transaction you have some kind of duplicated logic. If you want to operate with simple SQL Transaction than you should use NHibernate transaction.

TransactionScope class is designed to use in any general transaction scenario. For example it is used in EntityFramework. While using NHibernate you don't need it, but it becames very useful when you need to implement custom transaction mechanism.

0

精彩评论

暂无评论...
验证码 换一张
取 消