Hi 2025 visitors :) The current version of this library is available as a Nuget package and is also on GitHub. If you have any issues with this code please open an issue on GitHub.
Would it be nice if we can do something like this in our applications?
// 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.CopyFile(srcFileName, destFileName); // Insert a database record dbMgr.ExecuteNonQuery(insertSql); scope1.Complete(); }
With the rich support currently available for transactional programming, one may find it rather surprising that the most basic type of program operation, file manipulation (copy file, move file, delete file, write to file, etc.), are typically not transactional in today’s applications.
I am sure the main reason for this situation is lack of support for transactions in the underlying file systems. While Microsoft is bringing us Transactional NTFS (TxF) in Vista and Windows Server 2008, most corporate IT applications are still deployed to Windows 2003 or earlier. While I can’t wait to be able to use TxF, I have applications that have to be completed today!
While searching for a solution, I came across several articles describing the use of IEnlistmentNotification to implement your own resource manager and participate in a System.Transactions.Transaction. However, a complete working code example was nowhere to be found. Well, I guess it’s my turn to contribute. I hereby present to you: Chinh Do’s Transactional File Manager.
Here are my basic requirements for a Transactional File Manager:
Implementing IEnlistmentNotification is harder that it looks… at least for me it was. It’s not enough to just store a list of file operations. Because transactions can be nested and started from different threads; when rolling back, we have to make sure to only include the correct operations for the current Transaction. At first glance, it looks like we should be able to use the LocalIdentifier property (Transaction.TransactionInformation.LocalIdentifier) to identify the current transaction. However, further investigation reveals that Transaction.Current is not available in our various IEnlistmentNotification methods.
As it turned out, the little known but very cool ThreadStatic attribute fits the bill very well. Since the scope of a TransactionScope spans all operations on the same thread inside the TransactionScope block (excluding nested, new Transactions), ThreadStatic gives us an easy way to track that data.
/// <summary>Dictionary of transaction participants for the current thread.</summary> [ThreadStatic] private static Dictionary<string, TxParticipant> _participants;
In the initial version of my Transactional File Manager class (TxFileManager), I made the mistake of trying to implement IEnlistmentNotification in the main TxFileManager class. I had all kinds of difficulty trying to sort out different transactions/threads. Once I started to split to IEnlistmentNotification implementation into its own nested class (TxParticipant), everything became much cleaner. In the main class, all I have to do is to maintain a Dictionary<T, T> of TxEnlistment objects, which implement IEnlistmentNotification. Each TxEnlistment object would be responsible for handling a separate Transaction. Once that is in place, everything else was like pretty much a walk through the park.
Since my Resource Manager always performs operations immediately, there is really nothing to commit, except to clean up temporary files:
public void Commit(Enlistment enlistment) { for (int i = 0; i < _journal.Count; i++) { _journal[i].CleanUp(); } _enlisted = false; _journal.Clear(); }
Rolling back is a little bit more complicated. To ensure consistency, we must roll back operations in reverse order.
Another gotcha I ran into is that Rollback is often (if not all the time) called from a different thread from the Transaction thread. Any unhandled exception that occurs in Rollback will cause an AppDomain.CurrentDomain.UnhandledException. To “handle” an UnhandledException, you can either set IgnoreExceptionsInRollback = True or implement an UnhandledExceptionEventHandler.
public void Rollback(Enlistment enlistment) { try { // Roll back journal items in reverse order for (int i = _journal.Count - 1; i >= 0; i--) { _journal[i].Rollback(); _journal[i].CleanUp(); } _enlisted = false; _journal.Clear(); } catch (Exception e) { if (IgnoreExceptionsInRollback) { EventLog.WriteEntry(GetType().FullName, "Failed to rollback." + Environment.NewLine + e.ToString(), EventLogEntryType.Warning); } else { throw new TransactionException("Failed to roll back.", e); } } finally { _enlisted = false; if (_journal != null) { _journal.Clear(); } } enlistment.Done(); }
What does TDD have to do with this? It just happens that if you do Test Driven Development, Transactional File Manager can make testing classes that perform file operations much more convenient. In conjunction with a mocking framework such as Rhino Mocks, you can easily test the class functionality without having to read/write to actual files.
MockRepository mocks = new MockRepository(); MyClass1 target = new MyClass1(); Target.FileManager = new TxFileManager(); using (mocks.Record()) { Expect.Call(target.FileManager.ReadAllText()).Return("abc"); } using (mocks.Playback()) { target.DoWork(); }
Here are the known shortcomings of my Transactional File Manager:
// Complete unrealistic example showing how various file operations, including operations done // by library/3rd party code, can participate in transactions. IFileManager fileManager = new TxFileManager(); using (TransactionScope scope1 = new TransactionScope()) { fileManager.WriteAllText(inFileName, xml); // Snapshot allows any file operation to be part of our transaction. // All we need to know is the file name. XslCompiledTransform xsl = new XslCompiledTransform(true); xsl.Load(uri); //The statement below tells the TxFileManager to remember the state of this file. // So even though XslCompiledTransform has no knowledge of our TxFileManager, the file it creates (outFileName) // will still be restored to this state in the event of a rollback. fileManager.Snapshot(outFileName); xsl.Transform(inFileName, outFileName); // write to database 1 myDb1.ExecuteNonQuery(sql1); // write to database 2. The transaction is promoted to a distributed transaction here. myDb2.ExecuteNonQuery(sql2); // let's delete some files for (string fileName in filesToDelete) { fileManager.Delete(fileName); } // Just for kicks, let's start a new transaction. // Note that we can still use the same fileManager instance. It knows how to sort things out correctly. using (TransactionScope scope2 = new TransactionScope(TransactionScopeOptions.RequiresNew)) { fileManager.MoveFile(anotherFile, anotherFileDest); } // move some files for (string fileName in filesToMove) { fileManager.Move(fileName, GetNewFileName(fileName)); } // Finally, let's create a few temporary files... // disk space has to be used for something. // The nice thing about FileManager.GetTempFileName is that // The temp file will be cleaned up automatically for you when the TransactionScope completes. // No more worries about temp files that get left behind. for (int i=0; i<10; i++) { fileManager.WriteAllText(fileManager.GetTempFileName(), "testing 1 2"); } scope1.Complete(); // In the event an exception occurs, everything done here will be rolled back including the output xsl file. }
To list available contexts: kubectl config get-contexts To show the current context: kubectl config current-context…
kubectl exec -it <podname> -- sh To get a list of running pods in the…
# Create a soft symbolic link from /mnt/original (file or folder) to ~/link ln -s…
git config --global user.name "<your name>" git config --global user.email "<youremail@somewhere.com>" Related Commands Show current…
TypeScript/JavaScript function getLastMonday(d: Date) { let d1 = new Date(d.getFullYear(), d.getMonth() + 1, 0); let…
I had to do some SMTP relay troubleshooting and it wasn't obvious how to view…
View Comments
Hi all,
where is the all complete code ?? Works in Windows XP sp2 and win 2003 r2 ??
thanks
All code is update ? the project will be in codeplex ?? any other projects using IEnlistementNOtification ?? thanks
Espinete: The latest code is in the download link in the article. You will want to download it, then incorporate the suggested changes by Mark. CodePlex is a good idea... I will look into that. Chinh
Thats cool, codeplex power !!! Thanks
I have published the code to Codeplex (http://transactionalfilemgr.codeplex.com/). Cheers.
Should I implement IDisposable interface for TxEnlistment class or is it not needed?
Hi,
how about working with network pathes?
For example following: Directory.Exists(pathTargetProjectFolder) vs fileMgr.DirectoryExists(pathTargetProjectFolder) former returns true, latter false or fileMgr.CreateDirectory(pathTargetProjectFolder) fails with System.ArgumentException: Path cannot be the empty string or all whitespace, but Directory.CreateDirectory(pathTargetProjectFolder) succeedes.
Application is impersonated.
Thank you!
Shukhrat: Which version of the code are you using? I tested with the latest code (you can download from CodePlex) and it works fine:
Assert.IsFalse(_target.DirectoryExists(@"\\server\share"));
Assert.IsTrue(_target.DirectoryExists(@"\\vpc01\test"));
While going through this article i also came across ISinglePhaseNotification Interface and also a link to "Optimization using Single Phase Commit and Promotable Single Phase Notification" http://msdn.microsoft.com/en-us/library/ms229980(v=vs.90).aspx
,
Can you let me know when should one go for a Implmenting
ISinglePhaseNotification Interface vs. IEnlistmentNotification Interface.
I am currently having a database delete and file delete operations to be performed within a single transaction.
Thanks for the reply in advance
Ashwin: Sorry I don't have any additional info on ISinglePhaseNotification. But if what you need to do is have database and file ops wrapped in the same transactions, you should be able to use the current release of .NET Transactional File Manager. Is there any issue that prevents you from using that?