C# - System.Transactions.TransactionScope

后端 未结 2 1920
暖寄归人
暖寄归人 2020-12-03 08:06

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 qu

相关标签:
2条回答
  • 2020-12-03 08:34

    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.

    0 讨论(0)
  • 2020-12-03 08:36

    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.

    0 讨论(0)
提交回复
热议问题