Implementing transactions over multiple databases

前端 未结 4 613
余生分开走
余生分开走 2020-12-01 15:34

I am performing data changes on multiple databases, and I want to implement a transaction that will cover all of the changes.

This is what I currently have:

相关标签:
4条回答
  • 2020-12-01 15:46

    Using transactionScope is the answer. It even works with different DBMS!!!

    Transactions over multiple databases

    0 讨论(0)
  • 2020-12-01 15:47

    Use the TransactionScope class like this:

    using (TransactionScope ts = new TransactionScope())
    {
        //all db code here
    
        // if an error occurs jump out of the using block and it will dispose and rollback
    
        ts.Complete();
    }
    

    The TransactionScope class will automatically convert to a distributed transaction if necessary.

    0 讨论(0)
  • 2020-12-01 16:04

    As cletus said, you need some kind of two-phase commit. As the article states, this doesn't always work in practice. If you need a robust solution, you must find a way to serialize the transactions in such a way that you can do them one after the other and roll them back individually.

    Since this depends on the specific case on which you don't provide any details, I can't give you ideas how to attack this.

    0 讨论(0)
  • 2020-12-01 16:07

    If you wish to execute transaction across multiple instances of SQL Server then take a look at the Microsoft Distributed Transaction Coordinator documentation

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