working with transaction

5 posts, 1 answers
  1. Chen
    Chen avatar
    18 posts
    Member since:
    Apr 2012

    Posted 20 Jun 2012 Link to this post

    Hi I need to use transaction to commit()/rollback() multiple database updates/inserts. I searched and found the following code:
                 IObjectScope scope = ObjectScopeProvider1.GetNewObjectScope();
                 ITransaction txn = scope.Transaction;

    But what is the ObjectScopeProvider1 object? Can someone give me a complete sample?

    Thanks in advance.
  2. Answer
    Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 21 Jun 2012 Link to this post

    Hi Chen,

    I guess you are working with the OpenAccessContext. The context manages transactions implicitly. Whenever you create an context, a transaction is started automatically. You can commit the transaction by calling SaveChanges() or rollback the transaction by calling ClearChanges(). Additionally you can find a FlushChanges() method on the context. You can find more information in our documentation.
    Our SDK provides a lot examples on how to work with the context. This can be downloaded here.
    The ObjectScopeProvider was used in older versions of the product. I recommend using the OpenAccessContext.

    I hope this information is useful for you.
    Do come back if you have any other question.

    Greetings,
    Ralph
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  3. DevCraft banner
  4. Chen
    Chen avatar
    18 posts
    Member since:
    Apr 2012

    Posted 21 Jun 2012 Link to this post

    Hi Ralph,

    Thanks a lot for your help.

    Per your reply, my understanding is that if I call SaveChanges() for a few times, then call  ClearChanges(), it will roll back the changes done by the previouse SaveChanges() calls.

    Am I right?

    Chen
  5. Ralph Waldenmaier
    Admin
    Ralph Waldenmaier avatar
    202 posts

    Posted 22 Jun 2012 Link to this post

    Hi Chen,
    Calling SaveChanges() will do a commit on the database. So your assumption here is wrong. In order to send data to the server but not commit the data, you can use the FlushChanges() method. When you then call ClearChanges(), a rollback is executed. See the following snippet as example.

    using (var ctx = new EntitiesModel())
    {
        var obj = new FlushClass(){ SomeValue = "1"};
        ctx.Add(obj);
        ctx.FlushChanges(); // data is sent to the database
      
        var obj2 = new FlushClass(){ SomeValue = "2"};
        ctx.Add(obj2);
        ctx.FlushChanges(); // data is sent to the database
              
        ctx.ClearChanges(); // a rollback is executed
      
        var obj3 = new FlushClass() { SomeValue = "3" };
        ctx.Add(obj3);
        ctx.FlushChanges();
      
        ctx.SaveChanges(); // only object 3 is stored
    }

    As you can see, in the end only obj3 is stored to the database. If you would replace FlushChanges() with SaveChanges() then all objects would be saved to the database.

    Hope that helps.

    All the best,
    Ralph
    the Telerik team
    OpenAccess ORM Q2'12 Now Available! Get your hands on all the new stuff.
  6. Chen
    Chen avatar
    18 posts
    Member since:
    Apr 2012

    Posted 22 Jun 2012 Link to this post

    Ralph,

    Thanks a lot for your clear sample. Now I think I fully understood it.

    Chen
Back to Top
DevCraft banner