惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
The Register - Security
The Register - Security
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
罗磊的独立博客
U
Unit 42
S
SegmentFault 最新的问题
Y
Y Combinator Blog
博客园_首页
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
Schneier on Security
Schneier on Security
Know Your Adversary
Know Your Adversary
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Simon Willison's Weblog
Simon Willison's Weblog
V
Vulnerabilities – Threatpost
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
The Hacker News
The Hacker News
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Latest news
Latest news
L
Lohrmann on Cybersecurity
A
About on SuperTechFans
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
S
Securelist
A
Arctic Wolf
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
Scott Helme
Scott Helme
博客园 - 聂微东
博客园 - 【当耐特】
T
Tenable Blog
I
Intezer
D
DataBreaches.Net
B
Blog RSS Feed
Security Latest
Security Latest
C
Cisco Blogs
T
Tor Project blog
N
Netflix TechBlog - Medium

博客园 - Kevin Li

使用WMI远程部署/更新BizTalk程序集 基于WS-AtomicTransaction标准的WCF远程分布式事务(补充) 基于WS-AtomicTransaction标准的WCF远程分布式事务(二) 基于WS-AtomicTransaction标准的WCF远程分布式事务(一) 在Jeffrey Zhao的基础上+反编译System.Web.Extensions.Design得到的完整Ajax代码 在多线程环境下使用HttpWebRequest或者调用Web Service 一篇介绍.NET 2.0 范型挺全面的文章 C# 2.0中的范型和Nullable范型 Windows 2003 下分布式事务协调器(DTC)的配置 使用SQLSourceSafe控制数据库的版本 MySql 主键(自动增加)的数据类型所带来的错误 使用TraceListener 自动保存跟踪信息到文件中 接口属性集成与智能提示的奇怪问题 动态创建程序集中的类 动态创建程序集中的类 使用NUnit测试包含应用程序配置的dll文件 解决“COM+ 无法与 Microsoft 分布式事务协调程序交谈”的问题 [导入]一些很有用的Reflector 插件 Web安全性原则
.net remoting的事务传播以及wcf分布式事务
Kevin Li · 2006-11-10 · via 博客园 - Kevin Li

在.net 1.1或者.net 2.0中要实现分布式事务,如果不涉及远程调用,如调用remoting或者web service的方法,应该说是一件非常简单的事情,只需要用COM+(1.1/ServiceDomain)或者TransactionScope(2.0),通过msdtc协调即可。

但是当事务方法中还需要调用remoting或者web service(不使用ws-at)时,由于remoting不支持事务传播机制,因此即便客户端调用remoting后发生错误回滚,remoting server上已经执行的操作(如数据库操作)也已经无法回滚。

但是在一些特殊的应用中,数据完整性比性能更重要的情况下,如果希望客户端回滚时,remoting server上执行的操作也可以回滚,那么可以使用msdtc的Transaction Internet Protocol(TIP)协议(事实上,msdtc除了支持常用的OleTx分布式事务协议外,还支持XA和TIP协议),让客户端启动的事务“传播”至Remoting Server,那样只要Remoting Server同样使用客户端传播过来的事务,即可实现Remoting下的分布式事务。具体的做法如下:
(1)由于TIP事务协议默认是不启用的,所以第一步首先配置MSDTC支持TIP事务。在msdtc属性页中的点击“安全配置”后选中“启用TIP事务”。
(2)实现ITipTransaction 接口,用于从ContextUtil.Transaction查询TIPTransaction的相关属性。具体是获取TIP事务的TIP URL属性。

ITipTransaction 接口的定义:

[ Guid("17CF72D0-BAC5-11d1-B1BF-00C04FC2F3EF") ]
    [ InterfaceType(ComInterfaceType.InterfaceIsIUnknown) ]
    
public interface ITipTransaction
    
{
        
void Push([In, MarshalAs(UnmanagedType.LPStr)] string pszRemoteTmUrl,
            [In, Out, MarshalAs(UnmanagedType.LPStr)] 
ref string ppszRemoteTxUrl);

        
void GetTransactionUrl([In, Out, MarshalAs(UnmanagedType.LPStr)] 
            
ref string ppszLocalTxUrl);
    }
 

获取TIP URL的代码:

string url = null
                
                    ITipTransaction trxn 
= (ITipTransaction) ContextUtil.Transaction; 
                
                    trxn.GetTransactionUrl(
ref url);
                

(3)通过一定的机制,将获取得到的TIP URL传递到Remoting服务器。
要将tip url传递到remoting server方法很多,可以通过参数传递过去,也可以通过callcontext传递过去,具体可以看附件的代码。

(4)在Remoting Server中,利用TIP URL连接到客户端的事务中,并启动事务。
获取到client传递过来的tip url后,就可以通过它连接到现有的事务协调器上,具体代码:

TransactionCallContext ctx = (TransactionCallContext)CallContext.GetData("DTCTxID");
ServiceConfig config 
= new ServiceConfig();
                
                
if (ctx != null)
                
{
                    config.TipUrl 
= ctx.TipUrl;
                    config.Transaction 
= TransactionOption.RequiresNew;
                }

                
else
                
{
                    config.Transaction 
= TransactionOption.Disabled;
                }

                
                
using(TransactionScope scope = new TransactionScope(config))
                
{
                    rows 
= UpdatePoint();

                    
if( rows > 0 )
                    
{
                        scope.SetComplete();
                    }

                }

通过上面的方式,就可以实现Remoting下的分布式事务了。

源代码:
/Files/walkinhill/RemotingTransactionDemo.rar

参考文章:
http://dotnetjunkies.com/WebLog/chris.taylor/articles/54503.aspx
http://www.codeproject.com/csharp/complus_tip.asp

尽管通过上面的方式实现了remoting下的分布式事务,但是还是有些麻烦,而且一不小心还是会出错的。相比之下,wcf内部已经实现了这样的事务机制,而且只需要配置一下和外加一些特性就可以实现。而且还支持oletransactions和ws-at协议,看来以后用wcf方便多了。

最后有一点,这里只是了解了remoting下的分布式事务,但是具体要不要用,还是要充分考虑性能的问题,毕竟远程调用对性能的消耗还是很大的,特别是事务下对资源的锁导致的性能下降更严重:)