“什么!你要同时操作SQL Server、Oracle、DB2?”
用户的需求总是变态的,同时操作,分布式事务,唉越想越头疼。
没办法,MSDTC映入眼帘,还好,有东西能帮我们解决,但这个东西调通实在不易,关防火墙,卸杀毒软件,改注册表,改host文件,还好终于通了(配DTC的方法,网上搜吧)
好,终于开始用他写东西了,恩,微软就是微软,什么东西都做的这么简单,DTC也封装的很好,简单易学,或者你根本不用了解什么叫DTC,会学sql语句就行了。那到底怎么写呢?
private void ExecuteMSDTC()
{
ServiceConfig config = new ServiceConfig();
config.Transaction = TransactionOption.Required;
config.TransactionTimeout = 100;
ServiceDomain.Enter(config);//进入MSDTC
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlConnection conn = new SqlConnection(strConn1);
conn.Open();
SqlCommand cmd = new SqlCommand("Insert into table values(1,1) ", conn);
cmd.ExecuteNonQuery();
conn.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
OracleConnection oConn = new OracleConnection(strConn2);
oConn.Open();
OracleCommand oCmd = new OracleCommand("Insert into table values('1')", oConn);
oCmd.ExecuteNonQuery();
oConn.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ContextUtil.SetComplete();//提交
}
[Transaction(TransactionOption.Required, Timeout = 1000)]
public class DTCManager : ServicedComponent
{
public delegate void method();
public method ExecuteMethod;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public void Execute()
{
ExecuteMethod();
}
public void Commit()
{
ContextUtil.SetComplete();
}
public void Rollback()
{
ContextUtil.SetAbort();
}
}
恩,这里用了个代理来实现方法的执行,这样更灵活一些吧,hoho。既然是Com+的东西当然要注册了,不过你不用去手动注册的DLL,那个命令我不会-_-b.......还好微软帮我们做咯,ServicedComponent会自动注册,So,你只需要把你的程序集强命名咯。Yes,就这么简单。
再加一点这个东西的简单测试:
Over了,客户变态的需求啊,永远满足不了,唉。。。。
最后再提醒几个问题,Orcale的DTC不支持DDL语句,也就是说,你不能在事务里面执行CreateTable等操作。DB2的问题更恶心,不能修改表,可能是我才疏学浅吧,不过我用DB2的管理器修改表都报错,我晕菜,唉~~~~
用户的需求总是变态的,同时操作,分布式事务,唉越想越头疼。
没办法,MSDTC映入眼帘,还好,有东西能帮我们解决,但这个东西调通实在不易,关防火墙,卸杀毒软件,改注册表,改host文件,还好终于通了(配DTC的方法,网上搜吧)
好,终于开始用他写东西了,恩,微软就是微软,什么东西都做的这么简单,DTC也封装的很好,简单易学,或者你根本不用了解什么叫DTC,会学sql语句就行了。那到底怎么写呢?
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
呵呵,就这么简单咯,用ServiceConfig来使你的代码进入DTC执行状态,然后后面的东西都是在DTC里执行的咯,ContextUtil.SetComplete()来提交这个事务,当然了,这只是段示意代码,要加个try,如果出错,就让他回滚ContextUtil.SetAbort()。在进入DTC和提交之间的这个区域,你所有new的连接都将是在DTC中执行,你可以全部回滚它们,这样的分布式事务就OK咯。
嘿嘿,众所周知,DTC是Com+的服务,也就是说,刚才的两个conn都是在com+的某个context下执行的。于是微软为我们提供了另一个东东来执行这个DTC咯。那就是ServicedComponent+TrancationAttribute,我们再来看如何实现这个东西呢?
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
恩,这里用了个代理来实现方法的执行,这样更灵活一些吧,hoho。既然是Com+的东西当然要注册了,不过你不用去手动注册的DLL,那个命令我不会-_-b.......还好微软帮我们做咯,ServicedComponent会自动注册,So,你只需要把你的程序集强命名咯。Yes,就这么简单。
再加一点这个东西的简单测试:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Over了,客户变态的需求啊,永远满足不了,唉。。。。
最后再提醒几个问题,Orcale的DTC不支持DDL语句,也就是说,你不能在事务里面执行CreateTable等操作。DB2的问题更恶心,不能修改表,可能是我才疏学浅吧,不过我用DB2的管理器修改表都报错,我晕菜,唉~~~~