文件事物管理TransactionalFileManager的示例分析-创新互联
这篇文章将为大家详细讲解有关文件事物管理Transactional File Manager的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联长期为上千多家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为夏河企业提供专业的做网站、成都网站制作,夏河网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。Project Description
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(); }
Current Features
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).
Examples
// 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的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
本文题目:文件事物管理TransactionalFileManager的示例分析-创新互联
链接分享:http://hbruida.cn/article/ceodse.html