CodeSmith Community
Your Code. Your Way. Faster!

Transactions... How they fit in with Component Layer

Latest post 02-26-2007 12:51 PM by bloodhard. 6 replies.
  • 08-29-2006 12:23 PM

    • strazz
    • Top 100 Contributor
    • Joined on 03-16-2006
    • Chico, California
    • Posts 48
    • Points 1,363

    Transactions... How they fit in with Component Layer

    OK, I've searched the forums for this and I still can't come up with a satisfying answer.  In the old days, before component layer, If I had Orders and Customers, and I wanted to use a Transaction, I would do:

    TransactionManager tm = new TransactionManager()

    try
    {
    tm.open()
    Customer customer = new Customer();
    customer.firstName = bob;
    DAL.DataRepository.CustomerProvider.Insert(tm,customer);
    Order order1 = new Order()
    order1.customerID = customer.ID;
    order1.price = $500;
    Order order2 = new Order()
    order2.customerID = customer.ID;
    order2.price = $2000;
    DAL.DataRepository.OrderProvider.Insert(tm,order1);
    DAL.DataRepository.OrderProvider.Insert(tm,order2);
    tm.commit()
    }
    catch
    {
    tm.rollback()
    }

    (there might be some errors in that above code, its just for an example :- )

    OK, that was the old days.  I'm trying to use component layer now.  I've read that "transactions are automically handled."   I just dont see how if I used the component layer and called order.save() and cusomer.save() it would be able to make a transaction like the one above automatically.  With the "save()" method on the component layer (Im using the service model) not accepting a transaction manager, how do I do this?

    Thanks.

    • Post Points: 45
  • 08-29-2006 1:48 PM In reply to

    • GRAW
    • Top 25 Contributor
    • Joined on 06-23-2006
    • Posts 157
    • Points 4,560

    Re: Transactions... How they fit in with Component Layer

    You would something along the lines of:
    TransactionManager tm = null;
    try
    {
        tm = ConnectionScope.ValidateOrCreateTransaction(true);
        // or use ConnectionScope.CreateTransaction() if u prefer
        order.save();
        customer.save();
        tm.Commit();
    }
    catch (Exception ex)
    {
        tm.Rollback();
    }
    "Small is the number of them that see with their own eyes, and feel with their own hearts" Albert Einstein
    • Post Points: 65
  • 08-29-2006 1:56 PM In reply to

    • strazz
    • Top 100 Contributor
    • Joined on 03-16-2006
    • Chico, California
    • Posts 48
    • Points 1,363

    Re: Transactions... How they fit in with Component Layer

    Thanks for your help.

    OK, so just to clarify, the Component Layer is somehow able to detect that it is inside a "try" area and attach any save inside the try area to a transaction?
    • Post Points: 35
  • 08-29-2006 2:51 PM In reply to

    • GRAW
    • Top 25 Contributor
    • Joined on 06-23-2006
    • Posts 157
    • Points 4,560

    Re: Transactions... How they fit in with Component Layer

    No.  All the methods in the Component Layer classes make calls to ConnectionScope.ValidateOrCreateTransaction(), some (Save, Insert, Delete, Update) asking to create a transaction if none exists, others just enlisting in a transaction if one exists (the read operations).  The ConnectionScope class acts as a "context" and the protocol for the Component Layer is to always acquire a transaction through the "context".
    Using the previous example, if the call to ConnectionScope.ValidateOrCreateTransaction(true) was not made before calling the save methods on the 2 components, each one of the Save() methods would have created their own transaction, through the ConnectionScope "context", but each one of those transactions would have been limited to the scope of each specific method.
    This is a very elegant solution to a non-trivial problem, the .netTiers team has done a wonderful job on this, they deserve kudos for this.
    "Small is the number of them that see with their own eyes, and feel with their own hearts" Albert Einstein
    • Post Points: 35
  • 12-19-2006 1:23 AM In reply to

    • Pofo
    • Top 500 Contributor
    • Joined on 11-28-2006
    • Posts 10
    • Points 200

    Re: Transactions... How they fit in with Component Layer

    Sorry, this is not quite obvious to me, I can't find any reference to ConnectionScope.ValidateOrCreateTransaction() in the code. Do I need to make modifications to the templates? And would I still need to enable the httpmodule transaction manager?

    • Post Points: 35
  • 12-22-2006 8:02 AM In reply to

    • GRAW
    • Top 25 Contributor
    • Joined on 06-23-2006
    • Posts 157
    • Points 4,560

    Re: Transactions... How they fit in with Component Layer

    Pofo,

    ConnectionScope is in the Services namespace of the Component layer.  Also note, that the transactions will work ONLY if you're  using the Services for your data access, you cannot do your data access through the DataRepository if you want it to be enlisted in a transaction (not without some work on ur part).

    So instead of writing DataAccess.ArticleProvider.DeepSave(article) you'd be calling:

    new ArticleService.DeeepSave(article)

    As far as the httpmodule enabling is concerned, I don't know much about it, because I don't use it.  However, from what I've seen all it does is to start a transaction through the context, so this should work just fine with the module as it would enlist in the same transaction that the module started. 

    "Small is the number of them that see with their own eyes, and feel with their own hearts" Albert Einstein
    • Post Points: 5
  • 02-26-2007 12:51 PM In reply to

    Re: Transactions... How they fit in with Component Layer

    Hi,

    What happens when the first order.save throws an exception?

    will the tm.Rollback still work??

    will there be a "BeginTransaction" if the first operation fails?

     

     

    thank you
     

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