Nhibernate session.BeginTransaction是否在Using中异常自动回滚 [英] Does Nhibernate session.BeginTransaction auto rollback on exception within Using

查看:100
本文介绍了Nhibernate session.BeginTransaction是否在Using中异常自动回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,主题名称太长了...

Ok sorry for the long subject name...

如果我执行以下操作:

using (var transaction = session.BeginTransaction())
{
    // do something
    transaction.Commit();
}

如果我的做某事导致异常,它会自动回滚,还是我需要像下面这样显式检查此问题:

If my do something caused an exception, would it auto rollback, or do I need to explicitly check for this like below:

using (var transaction = session.BeginTransaction())
{
    try
    {
        // do something
        transaction.Commit();
    }
    catch (Exception)
    {
        transaction.Rollback();
    }
}

推荐答案

有一个安全的假设,如果在using块中提交失败,事务将回滚.

It's a safe assumption that the transaction will be rolled back if the commit fails in a using block.

ITransaction在后台包装ADO.NET事务,因此它取决于提供程序特定的实现.我检查过的源代码(2.1)中的Dispose方法假定在内部IDbTransaction上调用Dispose会将其回滚.

ITransaction wraps an ADO.NET transaction behind the scenes, so it depends on the provider specific implementation. The Dispose method in the source code I checked (2.1) assumes that calling Dispose on the internal IDbTransaction rolls it back.

这篇关于Nhibernate session.BeginTransaction是否在Using中异常自动回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆