C# - System.Transactions.TransactionScope

て烟熏妆下的殇ゞ 提交于 2019-11-26 16:35:22

问题


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:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2273419/c-sharp-system-transactions-transactionscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!