How do I do nested transactions in NHibernate?

前端 未结 4 1114
一生所求
一生所求 2020-12-05 14:39

Can I do nested transactions in NHibernate, and how do I implement them? I\'m using SQL Server 2008, so support is definitely in the DBMS.

I find that if I try somet

4条回答
  •  独厮守ぢ
    2020-12-05 15:22

    I've been struggling with this for a while now. Am going to have another crack at it.

    I want to implement transactions in individual service containers - because that makes them self-contained - but then be able to nest a bunch of those service methods within a larger transaction and rollback the whole lot if necessary.

    Because I'm using Rhino Commons I'm now going to try refactoring using the With.Transaction method. Basically it allows us to write code as if transactions were nested, though in reality there is only one.

    For example:

    private Project CreateProject(string name)
    {
        var project = new Project(name);
        With.Transaction(delegate
        {
            UnitOfWork.CurrentSession.Save(project);
        });
        return project;
    }
    
    private Sample CreateSample(Project project, string code)
    {
        var sample = new Sample(project, code);
        With.Transaction(delegate
        {
            UnitOfWork.CurrentSession.Save(sample);
        });
        return sample;
    }
    
    private void Test_NoNestedTransaction()
    {
      var project = CreateProject("Project 1");
    }
    
    private void TestNestedTransaction()
    {
      using (var tx = UnitOfWork.Current.BeginTransaction())
      {
          try
          {
              var project = CreateProject("Project 6");
              var sample = CreateSample(project, "SAMPLE006", true);
          }
          catch
          {
              tx.Rollback();
              throw;
          }
          tx.Commit();
      }
    }
    

    In Test_NoNestedTransaction(), we are creating a project alone, without the context of a larger transaction. In this case, in CreateSample a new transaction will be created and committed, or rolled back if an exception occurs.

    In Test_NestedTransaction(), we are creating both a sample and a project. If anything goes wrong, we want both to be rolled back. In reality, the code in CreateSample and CreateProject will run just as if there were no transactions at all; it is entirely the outer transaction that decides whether to rollback or commit, and does so based on whether an exception is thrown. Really that's why I'm using a manually created transaction for the outer transaction; so we I have control over whether to commit or rollback, rather than just defaulting to on-exception-rollback-else-commit.

    You could achieve the same thing without Rhino.Commons by putting a whole lot of this sort of thing through your code:

    if (!UnitOfWork.Current.IsInActiveTransaction)
    {
      tx = UnitOfWork.Current.BeginTransaction();
    }
    
    _auditRepository.SaveNew(auditEvent);
    if (tx != null)
    {
      tx.Commit();
    }
    

    ... and so on. But With.Transaction, despite the clunkiness of needing to create anonymous delegates, does that quite conveniently.

    An advantage of this approach over using TransactionScopes (apart from the reliance on MSDTC) is that there ought to be just a single flush to the database in the final outer-transaction commit, regardless of how many methods have been called in-between. In other words, we don't need to write uncommitted data to the database as we go, we're always just writing it to the local NHibernate cache.

    In short, this solution doesn't offer ultimate control over your transactions, because it doesn't ever use more than one transaction. I guess I can accept that, since nested transactions are by no means universally supported in every DBMS anyway. But now perhaps I can at least write code without worrying about whether we're already in a transaction or not.

提交回复
热议问题