Does Nhibernate session.BeginTransaction auto rollback on exception within Using

匆匆过客 提交于 2019-12-30 07:56:23

问题


Ok sorry for the long subject name...

If I do the following:

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();
    }
}

回答1:


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

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.



来源:https://stackoverflow.com/questions/6377394/does-nhibernate-session-begintransaction-auto-rollback-on-exception-within-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!