简体   繁体   English

如何在两个不同的数据库中维护事务

[英]How to maintain transactions in two different databases

I have to maintain transactions between two different databases.我必须维护两个不同数据库之间的事务。 I need to rollback if there is any error occurred in Database1 then all changes in database2 should be rollback.如果 Database1 中发生任何错误,我需要回滚,那么 database2 中的所有更改都应该回滚。

I have two connection string in my web.config file.我的 web.config 文件中有两个连接字符串。

The answer depends on if you need distributed transactions between two database server instances, or transactions between two databases in a single instance.答案取决于您是需要两个数据库服务器实例之间的分布式事务,还是单个实例中两个数据库之间的事务。 In the first case you'll need a transaction manager like MSDTC, but in the second case the database server should be able to do the job by itself.在第一种情况下,您需要像 MSDTC 这样的事务管理器,但在第二种情况下,数据库服务器应该能够自己完成这项工作。

TransactionScope will escalate the transaction to MSDTC if necessary.如有必要, TransactionScope会将事务升级到 MSDTC。 The rules for this are somewhat subtle.这方面的规则有些微妙。 If the two databases are on a single SQL Server 2008 instance, you shouldn't need MSDTC.如果这两个数据库位于单个 SQL Server 2008 实例上,则不需要 MSDTC。

See also:也可以看看:

You could use the TransactionScope class:您可以使用TransactionScope class:

using(var tx = new TransactionScope())
{
    // TODO: your DB queries here
    tx.Complete();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM