How to “transaction” a IO operation and a database execution?

前端 未结 2 1282
温柔的废话
温柔的废话 2021-01-14 01:57

I have service that contains a processor running, and it do two things:

1- Create a file in a directory.
2- Set your own status to \"Processed\".


        
相关标签:
2条回答
  • 2021-01-14 02:17

    You didn't specify the database server, but Microsoft SQL Server 2008 R2 supports streaming file data as part of a transaction.

    See: https://technet.microsoft.com/en-us/library/bb933993%28v=sql.105%29.aspx

    Transactional Durability
    With FILESTREAM, upon transaction commit, the Database Engine ensures transaction durability for FILESTREAM BLOB data that is modified from the file system streaming access.
    

    For very large files, I wouldn't recommend it, because you often want the transaction to be as quick as possible when you have a lot of simultaneous transactions.

    I'd normally use a compensation behaviour, e.g. storing status in a database and when a service is restarted, get it to first check for operations which have started but not completed and finish them off.

    1. Operation started on Server x at datetime y
    2. Operation completed on Server x at datetime y
    0 讨论(0)
  • 2021-01-14 02:27

    You can use Transactional NTFS (TxF). This provides the ability to perform actions that are fully atomic, consistent, isolated, and durable for file operations.

    It can be intergrated to work with a large number of other transactional technologies. Because TxF uses the new Kernel Transaction Manager (KTM) features, and because the new KTM can work directly with the Microsoft® Distributed Transaction Coordinator (DTC).

    Any technology that can work with DTC as a transaction coordinator can use transacted file operations within a single transaction. This means that you can now enlist transacted file operations within the same transaction as SQL operations, Web service calls via WS-AtomicTransaction, Windows Communication Foundation services via the OleTransactionProtocol, or even transacted MSMQ operations.

    An example of file and database atomic transaction:

    using (connectionDb)
    {
        connectionDb.Open();
        using (var ts = new System.Transactions.TransactionScope())
        {
            try
            {
                File.Copy(sourceFileName, destFileName, overwrite);
                connectionDb.ExecuteNonQuery();
                ts.Complete();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            { }
        }
    }
    

    See the following links for more information:

    TxF on Codeplex

    Msdn reference

    Note: Remember DTC comes with a heavy performance penalty.

    0 讨论(0)
提交回复
热议问题