CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

<Transactional(TransactionalTypes.TransactionScope)> Question

Last post 05-12-2008, 7:14 AM by smiley riley. 3 replies.
Sort Posts: Previous Next
  •  05-09-2008, 7:50 PM 23476

    <Transactional(TransactionalTypes.TransactionScope)> Question

    I have the following piece of code.
     
     

    <Transactional(TransactionalTypes.TransactionScope)> _

    Protected Overrides Sub DataPortal_Insert()

    Using DataLinq = ContextManager(Of SystemDataLinqDataContext).GetManager("ConnectionString"
          DataLinq.DataContext.spAddFinishType( _
                         Description, _
                         Active, _
                         _Id)
    End Using

    End Sub

    I heard using the <Transactional(TransactionalTypes.TransactionScope)> attribute is a performance hog if you are hitting a "single" database. Pretty much overkill.

    Is there a way I can create a manual transaction with the code above?

    Or am I thinking this all wrong.

    Thanks,
    Mark

  •  05-10-2008, 12:30 PM 23484 in reply to 23476

    Re: <Transactional(TransactionalTypes.TransactionScope)> Question

    No, TransactionScope is designed for a single database. The challenge is that it is only fast if you only open exactly one database connection and reuse that connection.

    This is the purpose of Csla.Data.ContextManager - to make it very easy to reuse a single connection - thus making TransactionScope the right option.

    ContextManager opens a connection if it isn't open, or reuses a connection if it is already open. The trick is to nest all your Using blocks.

    In other words, the root object (where your DP_XYZ method is called by the data portal) should have a Using block for the context, and all related data access should be in that Using block.

    <Transactional(TransactionScope)> _
    Protected Overrides Sub DataPortal_Update()
      Using ctx = ContextManager(Of MyDbContext).GetManager("MyDbName")
        ' update this object
        ' update child objects
        ' interact with any other root or command objects
      End Using
    End Sub

    As long as everything happens within that Using block, all the Using blocks in those other objects will automatically reuse the same L2S data context object, and thus will reuse the same database connection.

    As long as every root object's DP_XYZ methods follow this pattern, you can freely invoke one root object from another one's DP_XYZ methods and they'll all automatically reuse the data context.

    The result is very clean and simple. No muss, no fuss Smile [:)]


    Rocky
  •  05-10-2008, 2:14 PM 23485 in reply to 23484

    Re: <Transactional(TransactionalTypes.TransactionScope)> Question

    THANKS A LOT ROCKY!!!!!!!!!

    I was somewhat nervious in thinking that I would have to change my code.
     
    Thanks again, and it was great meeting you at DirectBuy.
     
    Mark
     
     
  •  05-12-2008, 7:14 AM 23498 in reply to 23476

    Re: <Transactional(TransactionalTypes.TransactionScope)> Question

    If the set of data updates/inserts etc includes at any point retrieving data then use the flag to exclude these from the transaction otherwise you may lock records within these selects as they may become part of the transaction. I may be wrong but best be aware.

     

    Not that you should need to select in an update delete etc, but people do.

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems