What is the good / use of TransactionScope?

I've been using NHibernate for a while now and came across the code below that uses Transaction scope

.

using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
    using (var session = sessionFactory.OpenSession())
    {
       using (var transaction = session.BeginTransaction())
       {
         // do work
       }
     }
}

      

I generally do everything without wrapping the code before TransactionScope

. Am I doing something wrong or am I just missing some great functionality?

+3


source to share


4 answers


Usage: transactions. Whether this is a more complex advantage. There are more direct ways of making transactions - ADO.NET transactions. It's a little awkward to work with (you need to remember to set up a transaction for each command), but very efficient.

A transactional transaction takes precedence over an external transaction; this makes work easier. However, it works differently. In particular, the transaction scope supports multiple resource transactions, which can mean multiple databases, etc. This is usually done with DTC, but DTC has an overhead - it is more expensive (and requires a specific firewall configuration, etc.), In many cases with a single database, it can shrink and use LTM instead of the full DTC, but that's about it even more expensive than ADO.NET transactions ... just not as expensive as DTC.



Powerful feature, but make sure you're going to use it before you do: p

+3


source


You are missing some great functionality: with a transaction scope, the transaction scoped code will participate in an external transaction if it is called from within a piece of code running in its own transaction scope. Without a transaction scope, your code will have its own transaction (from the deepest nested block), which can fail without breaking the outer transaction.

Also, anything inside your block // do work

will have an easier time involved in your transaction if you deposit transaction volume outside of it. The code could break the transaction without having to propagate the error code down the chain or throw an exception that could be ignored by the code in the middle.



Note. Remember to call scope.Complete()

in the transaction scope until the end of your block using

.

+1


source


Unless you use TransactionScope explicitly, each statement you execute on the database will be executed in a separate transaction.

With TransactionScope, you can combine multiple statements into a large transaction and undo everything as a block.

This is necessary when updating multiple tables in multiple operations, but when doing almost one big thing it should work or not at all.

0


source


Are you using transactions at all. If you don't, then you should be.

I personally don't use TransactionScope in any of my NHibernate, but again I don't need it. In a web environment with a single database, this is not necessary. my unit of work is a web request. I open a connection to BeginRequest and close it on EndRequest. I am using a shared repository that will start a transaction if none are present and have defined a TransactionAttribute that decorates the controller actions so that all table updates are done in a single transaction.

TransactionScope is just Microsoft's general way of putting all transactional resources into one transaction. It could be multiple databases, a transactional file system, ... In these scenarios, you need to worry that a transaction is likely to be promoted to a DTC to coordinate all updates.

Also, I don't know, still a problem, but older versions of NHibernate had a memory leak related to using TransactionScope.

Here's a link with everything you need to know about TransactionScopes http://www.codeproject.com/Articles/690136/All-About-TransactionScope

0


source







All Articles