Importance of using TransactionScope
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.
精彩评论