English 中文(简体)
1. 现行交易中流利
原标题:Doing Flush() inside active Transaction

I have a scenario that I commonly run into. It s simple to do with a standard ADO Transaction, but not so much with NH (that I know of).

I have 2 tables to update. The first contains profile information (Profile) and the other (Work) contains records changes that need to be made and the status of those changes. For each update to the Profile table, there will be an update to the status on the Work table.

  • If the update to the Profile table fails, I need to update the status on the Work table.
  • If the update to the Profile table succeeds, and the update to the Work table fails, I need to rollback the transaction.

The problem is that I don t know if the update to the Profile table failed until I commit the transaction. I tried to do a Flush on the Profile to catch the exception so I could write the status to the Work table, but then my Commit fails with the exception caused from the Profile update.

How can I handle this? In a typical ADO Transaction, my first call will throw, but I can catch and still update the other tables in the transaction.

Here s sort of what my code looks like - pretty standard. This is not my actual code, so please focus on the problem, not that I m not disposing my transaction or closing my session ;) :

try
{
    ITransaction trans = _session.BeginTransaction();

    var work = _repo.GetWork();
    var profile = _repo.GetProfile(work.ProfileId);

    try
    {
        profile.UpdateWithNewValues(work);
        _session.SaveOrUpdate(profile);
        _session.Flush();
        work.Status = "Success";

    }catch{
      work.Status = "Failure";
    }

    _session.SaveOrUpdate(work);
    trans.Commit();

}catch{

    trans.Rollback();

}

I realize that Flush() is not going to work, but I don t know how else to do this.

最佳回答

有人需要澄清你的要求。

(1) >> 如果简介表的更新成功,以及工作表的更新工作失败,我需要收回交易。

我想,工作就像更新审计线索一样,如果更新简介工作,不应失败。 如果是这种情况,那么你就不应收回交易。 然而,在谈到这一点之后,你的法典已经符合这一要求。

如果简介表的更新工作失败,我需要更新工作表的状况。

如果更新工作失败,那么你就会撤回交易。 除非有两种单独的交易(一种交易涉及简介和工作(目前),然后是单独交易,否则你将无法更新工作表。 这是否对你有意义?

问题回答

在流体之前,我看不出有“<条码>传输”的问题。 这里就是一个例子(稍作修改,像你一样):

Profile profile;
Work work;
ITransaction tx;

try
{
    session.SaveOrUpdate(profile);  

    work.Status = "Success";    
    session.SaveOrUpdate(work);

    tx.Commit();
}
catch (Exception)   // wroh oh...
{
    try
    {
        work.Status = "Failure";
        session.SaveOrUpdate(work);

        tx.Commit();
    }
    catch (Exception)
    {
        if (!tx.WasRolledBack)
        {
            tx.Rollback();
            session.Clear();
        }

        throw;
    }
}
finally
{
    if (session.IsOpen)
    {
        // Whatever happened, Flush/Persist at the end.
        session.Flush();
    }
}




相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签