Alternatives to using Transactional NTFS
Asked Answered
S

2

7

Given that Microsoft has deprecated Transactional NTFS (TxF):

Microsoft strongly recommends developers utilize alternative means to achieve your application’s needs. Many scenarios that TxF was developed for can be achieved through simpler and more readily available techniques. Furthermore, TxF may not be available in future versions of Microsoft Windows.

While TxF is a powerful set of APIs, there has been extremely limited developer interest in this API platform since Windows Vista primarily due to its complexity and various nuances which developers need to consider as part of application development. As a result, Microsoft is considering deprecating TxF APIs in a future version of Windows to focus development and maintenance efforts on other features and APIs which have more value to a larger majority of customers.

This means that i need an alternative to:

My transacted requirements are fairly simple - move two files:

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);
}

i've thought about turning MoveFile in to CopyFile + DeleteFile:

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

DeleteFile(testResults);
DeleteFile(cdcResponse);

But i was hoping for a good solution, not a buggy solution. So i try again:

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);
}

Except i was hoping for a good solution, not a buggy one.

Surbeck answered 16/11, 2012 at 16:34 Comment(0)
F
3

Give a try to .NET Transactional File Manager. It is fairly simple to use it safely. The following example from the page shows the way. It even looks like the author is responsive and is able to extend the library with new useful features.

// 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();
} 

In case you are interested in your own transactional manager, be sure you check out this article. If you carefully examine the above mentioned library, you will find that it is implemented right this way.

Formal answered 8/8, 2013 at 11:46 Comment(1)
i peeked at the source. The "rollback" still boils down to File.Delete(path);; which suffers the problem perhaps not being able to delete the file. The "real" solution involves using CreateFile, and re-implementing CopyFile, so that you can retroactively signal the handle to "delete file when the handle is closed". But if the issues associated with transactional integrity were not an overriding concern (and i were using .NET), i'd use something like this. Accepted.Surbeck
H
5

From the link:

As a result, Microsoft is considering deprecating TxF APIs

It isn't dead yet! I don't know why they would remove an atomic file system API for Windows, even if it isn't largely supported yet. There should be a .NET BCL for ease of use leveraging TxF for starters. TxF style support for network copies would also be awesome.

If anything, Microsoft should be improving the API!

Hogshead answered 8/7, 2014 at 1:7 Comment(2)
I agree. But we are where we are.Surbeck
We are currently using this to accomplish that goal. github.com/goldfix/Transactional-NTFS-TxF-.NET It works for File.Move That's all we use it forOutcaste
F
3

Give a try to .NET Transactional File Manager. It is fairly simple to use it safely. The following example from the page shows the way. It even looks like the author is responsive and is able to extend the library with new useful features.

// 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();
} 

In case you are interested in your own transactional manager, be sure you check out this article. If you carefully examine the above mentioned library, you will find that it is implemented right this way.

Formal answered 8/8, 2013 at 11:46 Comment(1)
i peeked at the source. The "rollback" still boils down to File.Delete(path);; which suffers the problem perhaps not being able to delete the file. The "real" solution involves using CreateFile, and re-implementing CopyFile, so that you can retroactively signal the handle to "delete file when the handle is closed". But if the issues associated with transactional integrity were not an overriding concern (and i were using .NET), i'd use something like this. Accepted.Surbeck

© 2022 - 2024 — McMap. All rights reserved.