locked
Ambient Transactions?

    Question

  • Hi!

    First off, if I am posting this question in the wrong forum or if there is another forum that is more suited to provide an answer; then plz let me know.

    I have worked with databases and database programming within the .NET framework for years but only recently have I come across a concept that is new to me. What exactly is an Ambient Transaction?

    I have found a general description of it online but can't see how it would benefit over simply using a connection object provided by any of the available .NET database providers; or even what is the difference for that matter.

    Can someone plz enlighten me on this? The only thing that I've been able to find in terms of a definition is that they are "transactions that live in the current thread or object context that anybody interested can query for their existence and use them to do work as part of them." But what exactly does that mean???

    Any assistance would be greatly appreciated.

    Thx

    Thursday, May 27, 2010 11:10 PM

Answers

  • Hi,

    Ambient transaction roughly translates to current active transaction. SOme context would help here. Lets take a look at sql transaction.

    using (IDbTransaction tran = conn.BeginTransaction()) { 
      try { 
        // your code 
        tran.Commit(); 
      } catch { 
        tran.Rollback(); 
        throw; 
      } 
    } 

    In the above code you are specifically asking the sql server to get into a transaction(enlist). So if you want all the code inside the try block to execute and you have other method calls as well and you are expecting the other method calls to act in a transacted way then you need to pass the current transaction object to those method calls.

    With ambient transactions(TransactionScope class)

    using(TransactionScope tran = new TransactionScope()) { 
      CallAMethodThatDoesSomeWork(); 
      CallAMethodThatDoesSomeMoreWork(); 
      tran.Complete(); 
    }
    Your methods will understand that the current thread is transacted, and therefore it will use the transaction that is available already and we dont need to bring it inside a transaction explicitly.
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
    • Marked as answer by mindscape777 Friday, May 28, 2010 5:10 PM
    Friday, May 28, 2010 3:33 AM

All replies

  • Hi,

    Ambient transaction roughly translates to current active transaction. SOme context would help here. Lets take a look at sql transaction.

    using (IDbTransaction tran = conn.BeginTransaction()) { 
      try { 
        // your code 
        tran.Commit(); 
      } catch { 
        tran.Rollback(); 
        throw; 
      } 
    } 

    In the above code you are specifically asking the sql server to get into a transaction(enlist). So if you want all the code inside the try block to execute and you have other method calls as well and you are expecting the other method calls to act in a transacted way then you need to pass the current transaction object to those method calls.

    With ambient transactions(TransactionScope class)

    using(TransactionScope tran = new TransactionScope()) { 
      CallAMethodThatDoesSomeWork(); 
      CallAMethodThatDoesSomeMoreWork(); 
      tran.Complete(); 
    }
    Your methods will understand that the current thread is transacted, and therefore it will use the transaction that is available already and we dont need to bring it inside a transaction explicitly.
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
    • Marked as answer by mindscape777 Friday, May 28, 2010 5:10 PM
    Friday, May 28, 2010 3:33 AM
  • Thanks, that really helped!

    Friday, May 28, 2010 5:11 PM