Transaction Management

2 posts, 0 answers
  1. Hasan Oz
    Hasan Oz avatar
    11 posts
    Member since:
    Apr 2010

    Posted 25 Aug 2010 Link to this post

    I have relational tables. After add a record to one table before scope.Commit() I must get the record Id.
    For consistency after some add, delete or update operations I can Commit or Roolback  all operations.
    Is there a way for this transaction management ?
    Can I get  autoincrement Id before commit?
    Is rollback after commit is possible?
  2. TSE
    TSE avatar
    381 posts
    Member since:
    Sep 2008

    Posted 25 Aug 2010 Link to this post

    Hi Hasan,

    I think what you need is a call to Flush() .

    The Flush method will give you almost "Commit behavior". For instance:

    1) Flush() will apply the changes on the database level, but not commit them at the database level
    2) After Flush() you can still do a Rollback()
    3) After Flush() the Id of a new persistent instance will be available in the instance.

    Pseudo code:

    // Scope level transaction starts

    // Do something to create a new persistent object - in this example an Order
    Order order = new Order();
    order.IssueDate = DateTime.Now

    // Flush the changes recorded in the ObjectScope (since scope level transaction start) to the database level.

    // Now you can read the Id of the newly flushed Order instance.
    int orderId = order.Id;

    // And you can still do a rollback (or commit in non-error cases)

    So Flush() is like a "soft-Commit" at the database level.

    So what is the drawback of using Flush()?

    At the database level you will have a running transaction until you either do a Commit or a Rollback..  Usually that is not a problem with short lived scope transactions..



  3. DevCraft banner
Back to Top