views:

754

answers:

2

I was curious about the TransactionScope class.

For the most part, I assume it was intended for database connections (which is what I've used it for).

My question, is can you put any code in the using-block of a TransactionScope to make it transactional? MS documentation is not clear on this.

If it can be used to make code other than database connections transactional, which ones are supported? It would seem crazy to me if it could make System.IO.File operations transactional.

+1  A: 

No, you cannot make arbitrary code transactional by running it inside a TransactionScope.

As you noted in a comment, the System.Transactions namespace provides infrastructure classes to help make any resource transactional. By default, .NET provides resource manager support for several kinds of operations, listed in the namespace introduction you linked (in a comment): "SQL Server, ADO.NET, MSMQ, and the Microsoft Distributed Transaction Coordinator (MSDTC)."

It turns out, there is support for file system transactions - though I could only find it for NTFS (Enhance Your Apps With File System Transactions). For my money, that code could seriously use a façade, though. ;) Perhaps there are other, more generalized implementations out there (or perhaps not - making file IO transactional may require the extra infrastructure NTFS provides).

There's also a fair amount of ongoing research on making changes to in-memory state transactional, called software transactional memory. Microsoft DevLabs offers an implementation: STM.NET.

Jeff Sternal
That project seems to only support working with a transactional state in memory. This is far-off from support to making all code transactional (like System.IO I mention above). What *does* TransactionScope support besides database connections?
Jonathan.Peppers
TransactionScope and Transactions are for databases - what else would they be used for?
0A0D
This page mentions some other stuff: http://msdn.microsoft.com/en-us/library/system.transactions.aspx, it talks about using it for other things besides databases (MSMQ) and the ability to implement a "Resource Manager". So can you implement something to make other things transactional?
Jonathan.Peppers
@change for anything that may need to roll back. Ctrl-z, except triggered by one part of the transaction failing rather than user input.
Will
@jon transactions aren't magic. You have to code not only what happens within the transaction but also how to undo what you've done.
Will
I understand they are not magic, but how useful would transactional file operations be? If an "undo" operation can be figured out for some scenarios, this seems like a valid open source project. You would, of course have to write to temporary files and perform a rename to get the desired effect. The same idea could be applied to other things as well (CTRL+Z in a UI).
Jonathan.Peppers
@Jonathan, you're right that STM only concerns state in memory, and that you could implement a resource manager to make file IO transactional (for example). I've updated my answer so that it better suits your question (I hope), and added some additional info.
Jeff Sternal
+3  A: 

TransactionScope is not only for the databases. Every component that implements IEnlistmentNotification interface can participate in two-phase commit of the transaction scope.

Here is an example of transactional in-memory storage: http://www.codeproject.com/KB/dotnet/Transactional_Repository.aspx

Also, I'm not sure if there are components in .NET for transactional file IO, but it is pretty easy to implement such component - latest OS like Vista and Windows Server 2008 have support for transaction file IO.

Vitaliy Liptchinsky