您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關文件事物管理Transactional File Manager的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Transactional File Manager is a .NET API that supports including file system operations such as file copy, move, delete, append, etc. in a transaction. It's an implementation of System.Transaction.IEnlistmentNotification (works with System.Transactions.TransactionScope).
This library allows you to wrap file system operations in transactions like this:
// Wrap a file copy and a database insert in the same transactionTxFileManager fileMgr = new TxFileManager();using (TransactionScope scope1 = new TransactionScope()) {// Copy a file fileMgr.Copy(srcFileName, destFileName);// Insert a database record dbMgr.ExecuteNonQuery(insertSql);scope1.Complete(); }
Support the following file operations in transactions:
AppendAllText
Copy
CreateDirectory
DeleteDirectory
DeleteFile
Move
Snapshot
WriteAllText
WriteAllBytes
This library supports any file system and is not a wrapper over Transactional NTFS (see AlphaFS).
// Completely 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. //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); XslCompiledTransform xsl = new XslCompiledTransform(true); xsl.Load(uri); xsl.Transform(inFileName, outFileName); // write to database 1. This database op will get committed/rolled back along with the file operations we are doing in this transaction. 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 nested transaction. Since we specify RequiresNew here, this nested transaction // will be committed/rolled back separately from the main 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.}
這是一個開源項目。原始項目網站是 事務文件管理器。
關于“文件事物管理Transactional File Manager的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。