C# - System.Transactions.TransactionScope

发布于 2024-08-22 03:09:14 字数 232 浏览 6 评论 0原文

我对 TransactionScope 类很好奇。

在大多数情况下,我认为它是用于数据库连接(这就是我使用它的目的)。

我的问题是,您可以将任何代码放入 TransactionScope 的 using 块中以使其具有事务性吗? MS 文档对此并不清楚。

如果它可以用于使数据库连接以外的代码成为事务性的,那么哪些代码受支持?如果它能让 System.IO.File 操作成为事务性的,我会觉得很疯狂。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2024-08-29 03:09:14

TransactionScope 不仅仅适用于数据库。每个实现 IEnlistmentNotification 接口的组件都可以参与事务范围的两阶段提交。

以下是事务性内存存储的示例:
http://www.codeproject.com/KB/dotnet/Transactional_Repository.aspx

另外,我不确定 .NET 中是否有用于事务文件 IO 的组件,但实现此类组件非常容易 - Vista 和 Windows Server 2008 等最新操作系统都支持事务文件 IO。

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.

安静被遗忘 2024-08-29 03:09:14

不,您不能通过在 TransactionScope 内运行任意代码来使其具有事务性。

正如您在评论中指出的, System.Transactions 命名空间提供 帮助使任何资源具有事务性的基础设施类。默认情况下,.NET 为多种操作提供资源管理器支持,在您链接的命名空间介绍中列出(在注释中):“SQL Server、ADO.NET、MSMQ 和 Microsoft 分布式事务协调器 (MSDTC)”。

事实证明,它支持文件系统事务 - 尽管我只能找到 NTFS 的支持(通过文件系统事务增强您的应用程序)。不过,就我的钱而言,该代码可以认真使用外观。 ;) 也许还有其他更通用的实现(或者也许没有——使文件 IO 事务化可能需要 NTFS 提供的额外基础设施)。

还有大量正在进行的关于更改内存状态事务性的研究,称为软件事务性内存。 Microsoft DevLabs 提供了一个实现:STM.NET

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文