2

Suppose I have a method named A(),B() and mainFunction() in Class C. In mainFunction() I have to run both methods A() and B() but both have beginTransaction() and commitTransaction(). If any error occur in B() then transactions in A() will still be committed or not? If yes, how can I get rid of this problem? Thanks in advance

  public class C
    {
        public void A()
        {
            //beginTransaction
            //functions
            //commitTransaction
        }
        public void B()
        {
            //beginTransaction
            //functions
            //commitTransaction
        }

        public void mainFunction()
        {
            A();
            B();
        }
    }
Dot Net developer
  • 436
  • 1
  • 5
  • 19
  • 1
    Possible duplicate of [How do I do nested transactions in NHibernate?](https://stackoverflow.com/questions/1192111/how-do-i-do-nested-transactions-in-nhibernate) – Najera Nov 02 '17 at 15:02

1 Answers1

2

You can do this:

    public void A(ISession objSession)
    {
        //functions
    }
    public void B(ISession objSession)
    {
        //functions
    }

    public void mainFunction()
    {  
        ISession objSession = base.GetCurrentSession;
        using (ITransaction transaction = objSession.BeginTransaction)
        {
         try 
         {
           A(objSession);
           B(objSession);

           //If successful for everything:
           objSession.Flush();
           objSession.Commit();
         }
         catch (Exception ex)
         {
           transaction.Rollback();
         }
    }

Basically I made the ISession as global variable for mainFunction. Then if it catches an error on either Function A or Function B, you can rollback the transaction without saving any changes on the database.

You can use of course ISession.Evict or ISession.Update inside Function A or Function B without saving any changes on the database unless you call the commit transaction.

The idea here is that you put the Transaction outside all the Function A and B so that you can call its transaction rollback outside it.

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • sorry sir but I must put begin and commit in every functions and perform nested transactions . I think I can override nHibernate's beginTransaction() and commitTransaction() to implement counter and check if it is last commitTransaction. If so, then only commit the whole transaction. But which class to override? I am confused – Dot Net developer Nov 02 '17 at 06:10
  • I understand. But you want to rollback it as well if Function B triggers an error? So why not make it global? But it's just my suggestion. – Willy David Jr Nov 02 '17 at 06:12
  • Can you please look at this dataAccessLayer once. There are many conventional errors but I want you to see at that three methods and suggest me ? **https://codereview.stackexchange.com/questions/179334/dataaccesslayer-of-nhibernate/179346#179346** – Dot Net developer Nov 02 '17 at 06:19
  • 2
    NHibernate does not support nested transactions. you need to understand the concept of unit of work with regards to nhibernate. transactions should not be started willy nilly inside of functions. transactions should be started inside a unit of work. – Fran Nov 02 '17 at 16:17