Detaillierte Erläuterung von Beispielen des Transaktionsdateimanagers

Y2J
Freigeben: 2017-04-25 09:12:30
Original
2291 Leute haben es durchsucht

推荐一个文件事物管理 Transactional File Manager

Projektbeschreibung

Transactional File Manager ist eine .NET-API, die das Einbeziehen von Dateisystemvorgängen wie Dateikopie, Verschieben, Löschen, Anhängen usw. in einer Transaktion. Es handelt sich um eine Implementierung von System.Transaction.IEnlistmentNotification (funktioniert mit System.Transactions.TransactionScope).

Mit dieser Bibliothek können Sie Dateisystemvorgänge in Transaktionen wie diese einschließen:

// 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();
}
Nach dem Login kopieren

Aktuell Funktionen

  • Unterstützt die folgenden Dateivorgänge in Transaktionen:

    • AppendAllText

    • Kopieren

    • Verzeichnis erstellen

    • Verzeichnis löschen

    • Datei löschen

    • Verschieben

    • Snapshot

    • WriteAllText

    • WriteAllBytes

Diese Bibliothek unterstützt jedes Dateisystem und ist kein Wrapper für transaktionales NTFS (siehe AlphaFS).

Beispiele

// 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.}
Nach dem Login kopieren

这是一个开源项目。原始项目网站是  事务文件管理器.

版权所有(c)2008-2013 Chinh Do

特此授予任何获得本软件和相关文档文件 („软件“)副本的人免费许可,无限制地处理本软件, 包括但不限于使用, 复制, 修改, 合并的权利, 发布, 分发, 再授权和/或出售本软件的副本,并允许提供本软件的人员遵守以下条件:

上述版权声明和本许可声明应包含在本软件的所有副本或主要部分.

该软件“按原样“提供, 不附带任何明示或暗示的保证, 包括但不限于适销性在任何情况下, 作者或版权所有者均不对任何索赔,损害或其他责任负责,无论是否因与本软件或本软件的使用或其他交易相关的任何合同,侵权行为或其他方面的行为软件.

Das obige ist der detaillierte Inhalt vonDetaillierte Erläuterung von Beispielen des Transaktionsdateimanagers. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!