System.Transactions, not only for databases

September 29, 2010

In every WCF course I gave in the past couple of years, whenever I began teaching how to use transactions in WCF, the same question comes up again – can we use transactions on anything else other than a database when using the TransactionScope class?

The basic answer I give is as follows: every resource manager that implements the necessary interface and enlists itself to the transaction scope is supported, so in short – you can use transactions on anything, IF YOU IMPLEMENT IT.

To allow a resource manager to participate in a transaction scope, it needs to implement the System.Transactions.IEnlistmentNotification interface. You can find more information about this interface and some code samples here.

Fortunately, there are many people that already tried (and succeeded) in writing libraries for transactional resource managers, such as:

1. File system.Most students usually ask me about file system transactions, so this is a good time as any to answer this question thoroughly. Since Windows Vista, the NTFS file system supports transactions, referred to as TxF, but Microsoft hasn’t formally released a managed .NET library that wraps the Win32 API for those functions, only some samples in this MSDN sample page. You can also see an implementation of this sample in the following screencast in channel9. If you’re looking for open-source projects, try the AlphaFS project from CodePlex.

If your file system is older and doesn’t support TxF, but you still want to be able to use transactions on files, you can try the .NET Transactional File Manager project from CodePlex.

By the way, if you do use transactions on the file system, better leave your log file out of the transaction scope, otherwise you might not understand why your logs don’t have any information about failed db transactions that were rolled back.

2. Memory. I vaguely remember someone once told me that in .NET 4 we’ll have transactional memory capabilities (also referred to as Software Transactional Memory, or STM). Apparently, STM.NET was released in the beta 1 version of .NET 4, but was removed from the RTM version. If you’re interested in this concept, there are some white papers on this subject, and some scattered implementations, for example the NSTM (.NET Software Transactional Memory) project from CodePlex.

So as you can see, transactions are not limited to only databases, but can also span to other resources as well, and can also share transactions among resources (what is referred to as a Distributed transaction). If you are planning to use distributed transactions in your services, I advise that you to read the following blog post from Arnon Rotem-Gal-Oz regarding possible problems with this pattern.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



  1. Eran StillerSeptember 29, 2010 ב 1:26 pm

    And don’t forget Transactional Registry (TxR) which allows modifying the Windows Registry in a transactional manner (also from Windows Vista/2008).

  2. Ido FlatowSeptember 29, 2010 ב 1:35 pm

    Thanks Eran, I forgot about it.

    This is a small sample that shows how to use TxR: