CodeSmith Community
Your Code. Your Way. Faster!

Transactions - Help/Examples

Latest post 07-24-2007 3:43 PM by Robert Hinojosa. 7 replies.
  • 07-11-2007 12:01 PM

    • Shamil
    • Not Ranked
    • Joined on 04-26-2007
    • Posts 4
    • Points 140

    Transactions - Help/Examples

    Hi,

     I was wondering if anyone can point me in the direction of some examples of the best way to use transactions within the component layer. I have a number of entities I want to edit then same in a single transaction.  Where should this be done and how?

     

    Thanks,

     

     

    Filed under:
    • Post Points: 35
  • 07-11-2007 2:52 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    Re: Transactions - Help/Examples

    I use the pattern that is used with the service layer itself.  It looks something like this:

    OrderService orderService = new OrderService();
    OrderDetailsService orderDetailsService = new OrderDetailsService();
    TransactionManager transactionManager = null;
    try
    {
       bool isBorrowedTransaction = ConnectionScope.Current.HasTransaction;
       transactionManager =
    ConnectionScope.ValidateOrCreateTransaction(true);
      
       //Do your work with the services
       Order order = new Order();
       order.OrderId = 1;
       ....
       orderService.Save(order);

      OrderDetails orderDetails = new OrderDetails();
      orderDetails.OrderId = 1;
      ....
      orderDetailsService.Save(orderDetails);

       //if persisted and tran not borrowed, commit
       if (!isBorrowedTransaction && transactionManager != null && transactionManager.IsOpen)
          transactionManager.Commit();
    }
    catch (Exception ex)
    {
       //if open, rollback, it's possible this is part of a larger commit
       if (transactionManager != null && transactionManager.IsOpen)
          transactionManager.Rollback();
    }

       

    The service classes use the ConnectionScope class to determine whether or not to create a transaction.  By expliciting creating your own transaction, the service class will go ahead and use that transaction, and since it's "borrowed" it will not commit.  It's up to you to commit or rollback based on the results of your work.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 07-11-2007 5:08 PM In reply to

    • Shamil
    • Not Ranked
    • Joined on 04-26-2007
    • Posts 4
    • Points 140

    Re: Transactions - Help/Examples

    That last sentence is the part I was not 100% clear on.  This makes sense now!

    Thanks a lot.

    Sham 

    • Post Points: 35
  • 07-23-2007 1:46 AM In reply to

    Re: Transactions - Help/Examples

    what is  ConnectionScope   here

    • Post Points: 35
  • 07-24-2007 2:25 PM In reply to

    • Don
    • Top 100 Contributor
    • Joined on 11-03-2006
    • Posts 48
    • Points 1,000

    Re: Transactions - Help/Examples

    So is there no way to deal with transactions in the service level?  It seems like you have to dip into the data access layer, right? 

    • Post Points: 35
  • 07-24-2007 2:36 PM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    Re: Transactions - Help/Examples

    What do you mean by "no way to deal with"?  In my example above, I explicitly create a transaction and then call into the service layer.  The service layer will automatically detect this transaction and join it.  If I'm misunderstanding your question, please let me know.

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 35
  • 07-24-2007 2:53 PM In reply to

    • Don
    • Top 100 Contributor
    • Joined on 11-03-2006
    • Posts 48
    • Points 1,000

    Re: Transactions - Help/Examples

    Sorry, I meant without having to reference the DAL.  It's not a big deal, was just wondering.  Your sample seems fine and I've converted it over to VB.NET and it looks good...

     

    Dim franchiseSvc As New Services.FranchiseService

                Dim transactionManager As Data.TransactionManager = Nothing

     

                Try

     

                    Dim isBorrowedTransaction As Boolean = Services.ConnectionScope.Current.HasTransaction

                    transactionManager = Services.ConnectionScope.ValidateOrCreateTransaction(True)

     

                    franchiseSvc.DeepSave(oneFranchise, Data.DeepSaveType.IncludeChildren, FranchiseInfoDeepLoadTypes)

     

                    ' Call the helper methods to save the junction tables for our entitiy collections that need it

                    SaveAddressJunctionTables(oneFranchise.FranchiseId, oneFranchise.AddressCollection_From_FranchiseAddress)

                    SaveContactJunctionTables(oneFranchise.FranchiseId, oneFranchise.ContactCollection_From_FranchiseContact)

     

                    'if persisted and tran not borrowed, commit

                    If (Not isBorrowedTransaction) AndAlso (Not transactionManager Is Nothing) AndAlso transactionManager.IsOpen Then

                        transactionManager.Commit()

                    End If

     

                Catch ex As Exception

     

                    'if open, rollback, it's possible this is part of a larger commit

                    If Not transactionManager Is Nothing AndAlso transactionManager.IsOpen Then

                        transactionManager.Rollback()

                    End If

     

                End Try

    • Post Points: 35
  • 07-24-2007 3:43 PM In reply to

    Re: Transactions - Help/Examples

    You can avoid having to include a Data Reference but you have to know that it's not a transient borrowed transaction.  And you will not be able to reference the TransactionManager directly.  So Ben's way is more flexible and powerful, but this will get you what you want.  The transaction will be rolled back in the dispose method if Complete() is not yet called.

    using (ConnectionScope.CreateTransaction())
    {
       //Do your work with the services
       Order order = new Order();
       order.OrderId = 1;
       ....
       orderService.Save(order);

      OrderDetails orderDetails = new OrderDetails();
      orderDetails.OrderId = 1;
      ....
      orderDetailsService.Save(orderDetails);
     
     ConnectionScope.Complete();
    }


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 5
Page 1 of 1 (8 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems