[英]Transaction for different databases
I use 2 different dlls to connect to 2 database in my project . 我使用2个不同的dll连接到我项目中的2个数据库。 each dll has it's own DAL layer .
每个dll都有自己的DAL层。
now in a page I need to insert a set of related data into these 2 databases . 现在在页面中,我需要向这两个数据库中插入一组相关数据。 I want to use transaction to make sure data are inserted correctly .
我想使用事务来确保正确插入数据。 but as I just have access to public methods of Insert() of each assembly , not the ADO.net codes .
但是由于我只能访问每个程序集的Insert()的公共方法,而不能访问ADO.net代码。 how can I handle this situation ?
我该如何处理这种情况? is there anything like this in c# ?
C#中有这样的东西吗?
beginTransaction(){
dll1.class.Insert(data);
dll2.className.Insert(relatedData);
....
}
You are looking for the TransactionScope
class: 您正在寻找
TransactionScope
类:
Makes a code block transactional.
使代码块具有事务性。
Example usage: 用法示例:
using (var tx = new TransactionScope())
{
dll1.class.Insert(data);
dll2.className.Insert(relatedData);
tx.Complete(); // if not executed, transaction will rollback
}
As @Anders Abel commented , the servers running the databases involved in the transaction will need the MSDTC service to be running and configured correctly on both the DB servers and the client machines. 正如@Anders Abel所 评论的那样 ,运行事务中涉及的数据库的服务器将需要MSDTC服务在数据库服务器和客户端计算机上正确运行和配置。 The DTCPing utility is very helpful in this regard.
DTCPing实用程序在这方面非常有用。
As Steve B commented , there is an assumption here that all the databases and the associated drivers support distributed transaction enlistment. 正如Steve B所 评论的那样 ,这里假设所有数据库和关联的驱动程序都支持分布式事务登记。 Check your documentation...
检查您的文档...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.