Álvaro García
Álvaro García

Reputation: 19366

I need to do rollback or somenthing when I don't need to do anything more?

I have this method that return a bool:

using(Entities dbContext = new Entities())
{
    using (DbContextTransaction myTransaction = dbContext.Database.BeginTransaction())
    {
        //Get data from database

        //check data from database

        //if my data is the expected: return true;
            //???: rollback or something to do or is it enough with the return?


        //if not is expected:
            //update data;
            //dbContext.Savechages();
            //myTransaction.Commit();
            //return true;
    }
}

My doubt is, if I don't have to do more with the data, is it enough returning true or It would be recommended to do a rollback, or commit with no SaveChanges?

Thanks.

Upvotes: 0

Views: 123

Answers (1)

Vecchiasignora
Vecchiasignora

Reputation: 1315

You should check your data before begin transaction, you dont need begin trasnaction while you are reading data from DB, because first part in your logic there is no any update,insert... and you dont need any commit or rollback

Actually your code should be like this

using(Entities dbContext = new Entities())
{
        //Get data from database

        //check data from database

        //if my data is the expected: return true;

using (DbContextTransaction myTransaction = dbContext.Database.BeginTransaction())
    {
         //if not is expected:
            try 
             {
               //update data;
               //dbContext.Savechages();
               //myTransaction.Commit();
               //return true;
             }
           catch(Exception ex)
           {
            transaction.Rollback();
            ......
            return false;
        }
    }
}

Upvotes: 1

Related Questions