开发者

how to Create a Transactionscope between saving a file and inserting a record in DB in C#

开发者 https://www.devze.com 2023-03-16 05:53 出处:网络
I have a problem, f开发者_如何学JAVAor saving a file and inserting a record in DB in a TransactionScope; Means saving file and inserting record, must depend together = or both or neither. Can anybody

I have a problem, f开发者_如何学JAVAor saving a file and inserting a record in DB in a TransactionScope; Means saving file and inserting record, must depend together = or both or neither. Can anybody help me please?


Transactional NTFS

One of the coolest parts about Transactional NTFS is that it can 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.

MSDN link

Alpha FS provides Transaction NTFS in .NET. see Alphaleonis.Win32.Filesystem.KernelTransaction(Transaction transaction). You can get the current transaction by Transaction.Current

using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    //KernelTransaction is in AlphaFS
    KernelTransaction kt = new KernelTransaction(Transaction.Current);

    //Append "hello" to text file named "text.txt"
    Alphaleonis.Win32.Filesystem.File.WriteAllText(kt, "text.txt", "hello");

    //No text appended because exception will be thrown
    throw new Exception("oops");

    ts.Complete();
}


try
{
    // Start DB Transaction
    // Save To DAtabase code
    // Save To File Code
    // Commit DB Transaction
}
catch
{
    // Rollback DB Transaction
}

Please notice Sequence of DB should be first then Saving to the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消