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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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
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.
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 ? codereview.stackexchange.com/questions/179334/…
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.