>  기사  >  백엔드 개발  >  Transactional File Manager의 예에 대한 자세한 설명

Transactional File Manager의 예에 대한 자세한 설명

Y2J
Y2J원래의
2017-04-25 09:12:302292검색

推荐一个文件事물管리 Transactional File Manager

프로젝트 설명

Transactional File Manager는 파일 복사, 트랜잭션에서 이동, 삭제, 추가 등을 수행합니다. 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

    • 복사

    • 디렉터리 만들기

    • 디렉터리 삭제

    • 파일 삭제

    • 이동

    • 스냅샷

    • 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.}

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

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

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

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

该软件“按原样”,提供任不附带任何明示或暗示的保证,包括但不限于适销性,适用于特任何索赔,损害或其他责任负责,无论是否因与本软件或本软件件的使用或其他交易易关的任何何,侵权行为或其他方面은 行为软件。

위 내용은 Transactional File Manager의 예에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.