CodeSmith Community
Your Code. Your Way. Faster!

Transactions and Nested FormViews

Latest post 02-02-2007 10:32 PM by velum. 2 replies.
  • 01-29-2007 2:03 PM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Transactions and Nested FormViews

    Hi!

    I am working on an ASP.NET projet where I am using Strongly Typed Datasources, and where many web pages are using nested FormViews. Does anyone know how to use transactions to rollback the INSERTS/UPDATES in case an error happens in a sub-FormView?

    For example, let's say, I have the complex object A containing object B (It could be a client (A), and its default address (B)). In order to insert new information (a new client with his address), I am using a FormView tied to object A's strongly typed data source and containing a sub-FormView tied to object B's strongly typed data source. When submitting the form, object A is inseretd first, and object B second. If an error occurs while inserting object B, then I want to be able to rool back the transaction because object A has already been inserted.

    Another thing I noticed is that if an error is raised by object B's processor (let's say that a check is done to see if the zip code exists, and it does not), then object A is inserted (assuming transactions are not implemented), and an error message is presented, but the web form is empty. I would expect it to remain filled with the information I entered so that I can resubmit the information.

    Cheers!

    JF
     

    • Post Points: 5
  • 01-30-2007 12:28 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: Transactions and Nested FormViews

    EntityTransactionModule looks like what I need:

                <!--
                    Enable transaction-per-request pattern;  This module allows a single TransactionManager to be shared by
                    all data access operations executed during a page request.  Used by the EntityDataSource control when
                    the EntityDataSource.EnableTransaction property is set to True.
                -->
                <add name="EntityTransactionModule" type="MyNSpace.MyDB.Web.Data.EntityTransactionModule, MyNSpace.MyDB.Web"/>

    I have a line similar to the above one in my web.config file, and my Datasources have EnableTransaction property is set to True. However, I am using Strongly Typed Datasources. Maybe that is why I don't seem to be able to get Page level transactions (I mean transactions that encompasses whole web pages).

    Is there sopmething like EntityTransactionModule for Strongly Typed Datasources?

    Cheers!

    JF 

    • Post Points: 5
  • 02-02-2007 10:32 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: Transactions and Nested FormViews

    Tracing the code, I discovered that the Component (or Service) Layer does use Transactions, and the Rollback or Commit is done by this method in EntityTransactionModule.cs:

            protected void OnEndRequest(Object sender, EventArgs e)
            {
                HttpApplication application = (HttpApplication) sender;
                HttpContext context = application.Context;
                TransactionManager mgr = ConnectionScope.Current.TransactionManager;

                if ( mgr != null && mgr.IsOpen )
                {
                    if ( context.Error != null )
                    {
                        mgr.Rollback();
                    }
                    else
                    {
                        mgr.Commit();
                    }
                }
            }

    So I found out that the problem is elsewhere. There are cases where a Rollback should be done, but context.Error is null because I used a delegate earlier to handle the exception. The error was taken car of, but I still would like to be able to do a Rollback. There are two scenarios to solve this, and I'm not sure which one I should be using:

    1) One solution would be to raise a flag when an exception is handled and to check that flag too in the above method. Something like:

                    if ( context.Error != null || flagIsRaised )... 

       With this scenario, I am not sure where the flag should be declared. Any suggestion?

    2) Another solution would be to do the Rollback in the Error Handling Delegate. In this case however, I have to make sure that no other INSERT/UPDATE is taking place after the Rollback is done. Let me explain. If submitting a web page triggers the insertion of entities A, B, C and D, and an error occurs while validating or inserting B, a Rollback should be done so that the insertion of A does not take place. On top of this, I have to make sure that the insertions of C and D won't take place. Here again, I could raise a flag when an exception is handled. I could then add code in the processors of all entities to check that flag and prevent an INSERT/UPDATE to take place if necessary. The code could also be added in the Insert()/Update() methods instead. Here again, I am not sure where the flag should be declared.

    Of the two scenarios, is there a best practice?

    Cheers!

    JF

     

    Filed under: , ,
    • Post Points: 5
Page 1 of 1 (3 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems