Transaction handling

8 posts, 1 answers
  1. Dave
    Dave avatar
    9 posts
    Member since:
    May 2010

    Posted 04 Aug 2011 Link to this post

    Hi Telerik Team,

    How can I achieve the following scenario in OpenAccess where I can easily achieve in Linq to Sql.
    I want to finish all changes in one transaction.
    What I need is I want to read back the changes I made to DB before actual commit.

    // Update some data.

    // Read Updated data

    // Delete or Insert to other tables

    // SaveChanges();

    In Linq to Sql, I can achieve by

    context.BeginTransaction()

    try
    {
    // Update some data.
    context.SubmitChanges();  (Already changed in DB)


    // Read Updated data 

    // Delete or Insert to other tables
    context.SubmitChanges(); (Already changed in DB)


    context.Transaction.Commit();
    }
    catch
    {
    context.Transaction.Rollback();
    }
    And it make sure ACID.

    Thanks
  2. Answer
    TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 05 Aug 2011 Link to this post

    Hi Kyaw,

    I am not sure this is the official way of doing so, but what you'll need is what is known as Flush method in the Classic API.

    To get a hand on the underlying ObjectScope (that is the Context equivalent in the Classic API), you can provide a property getter on your Context class  like:

    public MyContext : OpenAccessContext
    {

       public IObjectScope ObjectScope
       {
           get { return this.GetScope(); }
       }
    }

    and then in your method where you need to read the updated info you do:

    // "Soft Commit" the changes
    Context.ObjectScope.Flush();

    // Read the object again with the changes

    // Do a final commit
    Context.SaveChanges();

    This is a little cumbersome, but as far as I remember the Flush functionality (or a equivalent) is not available in the Context API yet..

    Let me know if it works out for you...

    Regards

    Henrik
  3. DevCraft banner
  4. Dave
    Dave avatar
    9 posts
    Member since:
    May 2010

    Posted 05 Aug 2011 Link to this post

    Hi Henrik,

    Thanks a million. You save my day.

    Finally I come out with this extended code.
    Why they hide such kind of useful functions inside?

    As I can write the extended code in another partial class, no need to worry of overwritten by auto generated codes where every time we make changes to Model.

     

     

     

     

    public partial class EntityModel
        {
            public IObjectScope ObjectScope
            {
                get { return this.GetScope(); }
            }
      
            public void Flush()
            {
                GetScope().Transaction.Flush();
            }
        }

    Thanks and Regards,
    Kyaw Myint Aung

  5. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 05 Aug 2011 Link to this post

    Hi Kyaw,

    Great it did work out for you.

    Maybe someone from Telerik can elaborate on if the Flush (or equivalent method or functionality) will be made available in the Context API or is already there..

    Regards
    Henrik
  6. Dave
    Dave avatar
    9 posts
    Member since:
    May 2010

    Posted 05 Aug 2011 Link to this post

    Hi Henrik,

    As I am using Q2 2011, the method is not exposed yet in Context API.

    Regards,
    Kyaw Myint Aung

  7. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 05 Aug 2011 Link to this post

    Hi,

    First let me thank Henrik for helping out on this issue. We can confirm that his advice is valid as per the Q2 2011 release of Telerik OpenAccess ORM.

    As for including the flush functionality in the context API, we will most certainly do it in one of the future releases.

    Let us know if we can be of any assistance.

    All the best,
    Ivailo
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  8. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 05 Aug 2011 Link to this post

    Hi Ivailo,

    Thanks for joining and elaborating on the issue. Looking forward to see this in the Context API
  9. Dave
    Dave avatar
    9 posts
    Member since:
    May 2010

    Posted 06 Aug 2011 Link to this post

    Hi Ivailo,

    Thanks for joining.

    Could you also see and acknowledge of my other two threads regarding to Stored Procedure and Transaction?
    link1 and link2

    It is important for us in development.

    Any help would be appriciated.

    Thanks.
Back to Top
DevCraft banner