Transactional File Managerの例の詳細な説明

Y2J
リリース: 2017-04-25 09:12:30
オリジナル
2307 人が閲覧しました

ファイル トランザクション管理の推奨 Transactional File Manager

プロジェクトの説明

Transactional File Manager は、ファイルのコピー、移動、削除、追加などのファイル システム操作をトランザクションに含めることをサポートする .NET API です。 System.Transaction.IEnlistmentNotification の実装 (System.Transactions.TransactionScope で動作します)。

このライブラリを使用すると、次のようなトランザクションでファイル システム操作をラップできます:

// 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 機能

  • トランザクションで次のファイル操作をサポートします:

    • AppendAllText

    • Copy

    • CreateDirectory

    • DeleteDirectory

    • ファイルの削除

    • 移動

    • スナップショット

    • WriteAllText

    • WriteAllBytes

このライブラリはあらゆるファイル システムをサポートしており、トランザクション NTFS のラッパーではありません (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.}
ログイン後にコピー

これはオープン ソース プロジェクトです。元のプロジェクトの Web サイトは Transaction File Manager です。

Copyright (c) 2008-2013 Chinh Do

これにより、このソフトウェアおよび関連ドキュメント ファイル (以下「ソフトウェア」) のコピーを入手した人に、使用、複製を含む制限なくソフトウェアを扱うための無料ライセンスを付与します。 、ソフトウェアのコピーを変更、マージ、公開、配布、サブライセンスおよび/または販売する権利、および以下の条件に従ってソフトウェアを提供する者に与えられます:

上記の著作権表示とこのライセンス通知は、すべてのコピーに含まれるものとします。ソフトウェアまたは本体部分のこと。

このソフトウェアは、商品性、特定の目的への適合性、および非侵害の保証を含むがこれらに限定されない、明示的か黙示的かを問わず、いかなる種類の保証もなく「現状のまま」提供されます。いかなる場合においても、作者または著作権所有者は、契約行為、不法行為、またはその他の行為であるかどうかにかかわらず、ソフトウェアまたはソフトウェアの使用またはその他の取引に関連して、いかなる請求、損害賠償、またはその他の責任についても責任を負いません。

以上がTransactional File Managerの例の詳細な説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!