This is a migrated thread and some comments may be shown as answers.

working with transaction

4 Answers 154 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chen
Top achievements
Rank 1
Chen asked on 20 Jun 2012, 06:59 PM
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.

4 Answers, 1 is accepted

Sort by
0
Accepted
Ralph Waldenmaier
Telerik team
answered on 21 Jun 2012, 01:31 PM
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.
0
Chen
Top achievements
Rank 1
answered on 21 Jun 2012, 04:57 PM
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
0
Ralph Waldenmaier
Telerik team
answered on 22 Jun 2012, 09:10 AM
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.
0
Chen
Top achievements
Rank 1
answered on 22 Jun 2012, 07:16 PM
Ralph,

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

Chen
Tags
Getting Started
Asked by
Chen
Top achievements
Rank 1
Answers by
Ralph Waldenmaier
Telerik team
Chen
Top achievements
Rank 1
Share this question
or