【问题标题】:Alternatives to using Transactional NTFS使用事务性 NTFS 的替代方案
【发布时间】:2012-11-16 16:34:34
【问题描述】:

鉴于微软有deprecated Transactional NTFS (TxF)

Microsoft 强烈建议开发人员使用替代方法来满足您的应用程序的需求。开发 TxF 的许多场景都可以通过更简单、更容易获得的技术来实现。此外,TxF 可能在 Microsoft Windows 的未来版本中不可用。

虽然 TxF 是一组功能强大的 API,但自 Windows Vista 以来,开发人员对该 API 平台的兴趣极为有限,这主要是由于其复杂性和开发人员在应用程序开发过程中需要考虑的各种细微差别。因此,Microsoft 正在考虑在未来版本的 Windows 中弃用 TxF API,以便将开发和维护工作重点放在对大多数客户更有价值的其他功能和 API 上。

这意味着我需要一个替代方案:

我的交易要求相当简单——移动两个文件:

tx = BeginTransaction();
{
   MoveFile(testResults, testResultsArchive); //throws if there's a problem
   MoveFile(cdcResponse, cdcResponseArchive); //throws if there's a problem

   CommitTransaction(tx);
}
finally
{
    CloseHandle(tx);
}

我考虑过将MoveFile 转为CopyFile + DeleteFile

CopyFile(testResults, testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse, cdcResponseArchive); //throws if there's a problem

DeleteFile(testResults);
DeleteFile(cdcResponse);

但我希望有一个好的解决方案,而不是一个有问题的解决方案。所以我再试一次:

CopyFile(testResults, testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse, cdcResponseArchive); //throws if there's a problem

try
{
    DeleteFile(testResults);
}
catch (Exception e)
{
   DeleteFile(testResultsArchive);
   throw e;
}
try
{
    DeleteFile(cdcResponse);
}
catch (Exception e)
{
   DeleteFile(cdcResponseArchive);
}

除了我希望有一个好的解决方案,而不是一个错误的解决方案。

【问题讨论】:

    标签: transactional txf


    【解决方案1】:

    来自链接:

    因此,Microsoft 正在考虑弃用 TxF API

    它还没有死!我不知道他们为什么要删除 Windows 的原子文件系统 API,即使它还没有得到很大的支持。应该有一个 .NET BCL 以便于使用 TxF 作为初学者。对网络副本的 TxF 样式支持也很棒。

    如果有的话,微软应该改进 API!

    【讨论】:

    【解决方案2】:

    试试.NET Transactional File Manager。安全使用它相当简单。页面中的以下示例显示了该方式。甚至看起来作者反应灵敏,并且能够使用新的有用功能扩展库。

    // Wrap a file copy and a database insert in the same transaction
    TxFileManager fileMgr = new TxFileManager();
    using (TransactionScope scope1 = new TransactionScope())
    {
        // Copy a file
        fileMgr.Copy(srcFileName, destFileName);
    
        // Insert a database record
        dbMgr.ExecuteNonQuery(insertSql);
    
        scope1.Complete();
    } 
    

    如果您对自己的事务管理器感兴趣,请务必查看this 文章。如果你仔细检查上面提到的库,你会发现它是这样实现的。

    【讨论】:

    • 我偷看了源头。 “回滚”仍然归结为File.Delete(path);;遇到问题可能无法删除文件。 “真正的”解决方案涉及使用CreateFile,并重新实现CopyFile,以便您可以retroactively signal the handle to "delete file when the handle is closed"。但是,如果与事务完整性相关的问题不是最重要的问题(而且我使用的是 .NET),我会使用类似的东西。接受。
    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 2014-03-31
    相关资源
    最近更新 更多