在.net 2.0 中执行分布式事务:隐式事务篇(SQL Server 与 Oracle)

项目涉及到多个数据库的查询更新操作,也就必然需要分布式事务的支持,查了MSDN知道 .net 2.0 中利用新增的 System.Transactions 命名空间可以简单的实现分布式事务:

None.gifSystem.Transactions 基础结构通过支持在 SQL Server、ADO.NET、MSMQ 和 Microsoft 分布式事务协调器 (MSDTC) 中启动的事务,使事务编程在整个平台上变得简单和高效。它提供基于 Transaction 类的显式编程模型,还提供使用 TransactionScope 类的隐式编程模型,在这种模型中事务是由基础结构自动管理的。强烈建议使用更为方便的隐式模型进行开发。
具体参考:http://msdn2.microsoft.com/zh-cn/library/system.transactions(VS.80).aspx 以及相关的连接都提供了非常详细的信息

参考MSDN的Demo做了SQL Server 2000 的 使用事务范围实现隐式事务测试,事务可以正常提交以及回滚:
ContractedBlock.gifExpandedBlockStart.gif
None.gifprivate void Test1()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
// 使用事务范围实现隐式事务
InBlock.gif        
// ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxtransactions/html/1ddba95e-7587-48b2-8838-708c275e7199.htm
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
using (TransactionScope ts = new TransactionScope()) dot.gif{
InBlock.gif            
//Create and open the SQL connection.  The work done on this connection will be a part of the transaction created by the TransactionScope
InBlock.gif
            SqlConnection myConnection = new SqlConnection("server=(local);Integrated Security=SSPI;database=northwind");
InBlock.gif            SqlCommand myCommand 
= new SqlCommand();
InBlock.gif            myConnection.Open();
InBlock.gif            myCommand.Connection 
= myConnection;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (!chkGenError.Checked) dot.gif{
InBlock.gif                
//Restore database to near it's original condition so sample will work correctly.
InBlock.gif
                myCommand.CommandText = "DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)";
InBlock.gif                myCommand.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//Insert the first record.
InBlock.gif
            myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
InBlock.gif            myCommand.ExecuteNonQuery();
InBlock.gif
InBlock.gif            
//Insert the second record.
InBlock.gif
            myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'MidEastern')";
InBlock.gif            myCommand.ExecuteNonQuery();
InBlock.gif
InBlock.gif            myConnection.Close();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (chkCommit.Checked) dot.gif{
InBlock.gif                ts.Complete();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**/////Call complete on the TransactionScope or not based on input
InBlock.gif            //ConsoleKeyInfo c;
InBlock.gif            
//while (true) {
InBlock.gif            
//    Console.Write("Complete the transaction scope? [Y|N] ");
InBlock.gif            
//    c = Console.ReadKey();
InBlock.gif            
//    Console.WriteLine();
InBlock.gif
InBlock.gif            
//    if ((c.KeyChar == 'Y') || (c.KeyChar == 'y')) {
InBlock.gif            
//        // Commit the transaction
InBlock.gif            
//        ts.Complete();
InBlock.gif            
//        break;
InBlock.gif            
//    }
InBlock.gif            
//    else if ((c.KeyChar == 'N') || (c.KeyChar == 'n')) {
InBlock.gif            
//        break;
InBlock.gif            
//    }
InBlock.gif            
//}
ExpandedSubBlockEnd.gif
        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
private void Test2()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
string connectString1 = "server=(local);Integrated Security=SSPI;database=northwind";
InBlock.gif        
string connectString2 = "server=(local);Integrated Security=SSPI;database=pubs";
InBlock.gif        
string dt = DateTime.Now.ToString();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
using (TransactionScope transScope = new TransactionScope()) dot.gif{
InBlock.gif            
using (SqlConnection connection1 = new
ExpandedSubBlockStart.gifContractedSubBlock.gif               SqlConnection(connectString1)) 
dot.gif{
InBlock.gif                
// Opening connection1 automatically enlists it in the 
InBlock.gif                
// TransactionScope as a lightweight transaction.
InBlock.gif
                connection1.Open();
InBlock.gif
InBlock.gif                
// Do work in the first connection.
InBlock.gif
                SqlCommand command1 = new SqlCommand();                
InBlock.gif                command1.Connection 
= connection1;
InBlock.gif
InBlock.gif                
//Restore database to near it's original condition so sample will work correctly.
InBlock.gif
                command1.CommandText = "DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)";
InBlock.gif                command1.ExecuteNonQuery();
InBlock.gif
InBlock.gif                
//Insert the first record.
InBlock.gif
                command1.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
InBlock.gif                command1.ExecuteNonQuery();
InBlock.gif
InBlock.gif                
//Insert the second record.
InBlock.gif
                command1.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'MidEastern')";
InBlock.gif                command1.ExecuteNonQuery();
InBlock.gif
InBlock.gif                
// Assumes conditional logic in place where the second
InBlock.gif                
// connection will only be opened as needed.
InBlock.gif
                using (SqlConnection connection2 = new
ExpandedSubBlockStart.gifContractedSubBlock.gif                    SqlConnection(connectString2)) 
dot.gif{
InBlock.gif                    
// Open the second connection, which enlists the 
InBlock.gif                    
// second connection and promotes the transaction to
InBlock.gif                    
// a full distributed transaction. 
InBlock.gif
                    connection2.Open();
InBlock.gif                   
InBlock.gif                    
// Do work in the second connection.
InBlock.gif
                    SqlCommand command2 = new SqlCommand();                    
InBlock.gif                    command2.Connection 
= connection2;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
if (!chkGenError.Checked) dot.gif{
InBlock.gif                        
//Restore database to near it's original condition so sample will work correctly.
InBlock.gif
                        command2.CommandText = "DELETE FROM stores WHERE (stor_id = '9797') OR (stor_id = '9798')";
InBlock.gif                        command2.ExecuteNonQuery();
ExpandedSubBlockEnd.gif                    }

InBlock.gif
InBlock.gif                    
//Insert the first record.
InBlock.gif
                    command2.CommandText = "Insert into stores (stor_id, stor_name) VALUES ('9797', 'ebay')";
InBlock.gif                    command2.ExecuteNonQuery();
InBlock.gif
InBlock.gif                    
//Insert the second record.
InBlock.gif
                    command2.CommandText = "Insert into stores (stor_id, stor_name) VALUES ('9798', 'amazon')";
InBlock.gif                    command2.ExecuteNonQuery();                    
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
//  The Complete method commits the transaction.
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if (chkCommit.Checked) dot.gif{
InBlock.gif                transScope.Complete();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif    
private void Test3()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
string connectString1 = "server=(local);Integrated Security=SSPI;database=northwind";
InBlock.gif        
string connectString2 = "server=(local);Integrated Security=SSPI;database=pubs";
InBlock.gif        
string dt = DateTime.Now.ToString();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
using (TransactionScope transScope = new TransactionScope()) dot.gif{
InBlock.gif            
using (SqlConnection connection1 = new
ExpandedSubBlockStart.gifContractedSubBlock.gif               SqlConnection(connectString1)) 
dot.gif{
InBlock.gif                
// Opening connection1 automatically enlists it in the 
InBlock.gif                
// TransactionScope as a lightweight transaction.
InBlock.gif
                connection1.Open();
InBlock.gif
InBlock.gif                
// Do work in the first connection.
InBlock.gif
                SqlCommand command1 = new SqlCommand();
InBlock.gif                command1.Connection 
= connection1;
InBlock.gif
InBlock.gif                
//Restore database to near it's original condition so sample will work correctly.
InBlock.gif
                command1.CommandText = "DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)";
InBlock.gif                command1.ExecuteNonQuery();
InBlock.gif
InBlock.gif                
//Insert the first record.
InBlock.gif
                command1.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
InBlock.gif                command1.ExecuteNonQuery();
InBlock.gif
InBlock.gif                
//Insert the second record.
InBlock.gif
                command1.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'MidEastern')";
InBlock.gif                command1.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Assumes conditional logic in place where the second
InBlock.gif            
// connection will only be opened as needed.
InBlock.gif
            using (SqlConnection connection2 = new
ExpandedSubBlockStart.gifContractedSubBlock.gif                SqlConnection(connectString2)) 
dot.gif{
InBlock.gif                
// Open the second connection, which enlists the 
InBlock.gif                
// second connection and promotes the transaction to
InBlock.gif                
// a full distributed transaction. 
InBlock.gif
                connection2.Open();
InBlock.gif
InBlock.gif                
// Do work in the second connection.
InBlock.gif
                SqlCommand command2 = new SqlCommand();
InBlock.gif                command2.Connection 
= connection2;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if (!chkGenError.Checked) dot.gif{
InBlock.gif                    
//Restore database to near it's original condition so sample will work correctly.
InBlock.gif
                    command2.CommandText = "DELETE FROM stores WHERE (stor_id = '9797') OR (stor_id = '9798')";
InBlock.gif                    command2.ExecuteNonQuery();
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
//Insert the first record.
InBlock.gif
                command2.CommandText = "Insert into stores (stor_id, stor_name) VALUES ('9797', 'ebay')";
InBlock.gif                command2.ExecuteNonQuery();
InBlock.gif
InBlock.gif                
//Insert the second record.
InBlock.gif
                command2.CommandText = "Insert into stores (stor_id, stor_name) VALUES ('9798', 'amazon')";
InBlock.gif                command2.ExecuteNonQuery();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//  The Complete method commits the transaction.
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if (chkCommit.Checked) dot.gif{
InBlock.gif                transScope.Complete();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
    
None.gif
 
测试的时候需要在 Sql Server 服务管理器中开启 MSDTC,我是再 SQL 2k上做的测试,2k5上应该得到更好的支持。
方法 Test1() 是单数据库的事务,只是为了测试;
 Test2()和Test3() 没有实质区别,都是自动注册事务。
完整代码下载:/Files/Jinglecat/DTCSQL.rar

同时我也尝试了一个Oracle版本,Oracle 10g 2,其中数据库orcl 是默认的启动数据库,而数据库nhrs是我自己建的一个数据库,测试通过,代码跟SQL没有两样,之前网上查到有网友说OracleClient还不支持 MSDTC,查了很多资料,确实在 ado.net 1.x 有问题,现在可以确信 ado.net 2.0 已经可以支持。
完整代码下载:/Files/Jinglecat/DTCORA.rar

现在银杏事务已经可以满数项目需求了,有时间再多更多的研究了^_^

转载于:https://www.cnblogs.com/Jinglecat/archive/2007/04/07/704297.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/436528.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

广州计算机自考独立办学,广东省06年自学考试独立办班广州考点办学计划的通知...

独立办班是广东省承担主考任务的普通高等院校中独立举办的自学考试辅导班(简称独立办班),在省考委的领导下,接受省考办的指导与管理。根据广东省考试中心《关于印发2006年广东省自学考试独立办班办学计划的通知》(粤考试中心[2006]164号)文件精神&#x…

android 按下缩小效果松开恢复_Android自定义ScrollView实现放大回弹效果

背景在很多项目中我们都会用到ScrollView这个控件,因为ScrollView能够在屏幕内容多时下上滑动以适配加载的内容。但是ScrollView滑动时效果感觉太死板了,这个时候我们如果给它添加一个回弹的动画效果,会让界面交互更加舒服,提升用…

【转】OData的初步认识

What – OData是什么? OData - Open Data Protocol,是一个设计和使用RESTful API的标准。REST本身只是一个构建web服务的思想和理念,其没有规定一个统一的标准来限制开发人员该如何设计RESTful API。其实我们实际开发中的确也没有遵循某个统…

选择Windows CE wince嵌入式操作系统 的十大理由

Windows CE是什么? Windows CE是一个具有战略意义的操作系统。它拥有windows操作系统的特点,支持32位虚拟内存机制、按需分配内存和内存映射文件;他也是抢先式多任务并具有强大通信能力的Win32嵌入式操作系统,是微软专门为信息设…

三元运算符 在数据绑定中的使用

在使用 datalist 等控件绑定前台数据的时候&#xff0c;有时希望一行只显示定长字符&#xff0c;多出来的部分以省略号代替&#xff0c;我使用三元运算符来做&#xff1a;<asp:DataList id"DataList1"runat"server"RepeatColumns"5"Width&quo…

软件测试中软,软件测试报告 - 中软模板.docx

Webplug platform测试报告TOC \o "1-3" \h \z \u HYPERLINK \l "_Toc259473567" 1. 引言 PAGEREF _Toc259473567 \h 1 HYPERLINK \l "_Toc259473568" 1.1 目的 PAGEREF _Toc259473568 \h 1 HYPERLINK \l "_Toc259473569" 1.2 术语 PAG…

【转】ABP源码分析四十三:ZERO的本地化

ABP Zero模块扩展了ABP基础框架中的本地化功能&#xff0c;实现了通过数据库对本地化功能进行管理。其通过数据库保存本地化语言及其资源。 ApplicationLanguage&#xff1a;代表本地化语言的实体类。一种语言就是一个ApplicationLanguage实例。 ApplicationLanguageProvider&a…

WinCE流设备驱动简介及GPIO驱动的实现

作者&#xff1a;ARM-WinCE 流设备驱动实际上就是导出标准的流接口函数的驱动&#xff0c;这是文档上面的定义。在WinCE中&#xff0c;所有的流设备都导出流设备接口&#xff0c;这样WinCE中的Device Manager可以加载和管理这些流设备驱动。 流设备驱动的架构如图&#xff1a;…

GridView数据导入Excel/Excel数据读入GridView

1 protectedvoidButton1_Click(objectsender, EventArgs e)2 {3 Export("application/ms-excel", "学生成绩报表.xls");4 }5 6 privatevoidExport(stringFileType, stringFileName)7 {8 Response.Charset "GB2312";9 Response.ContentEncodi…

计算机出现蓝屏怎么解决,电脑出现蓝屏故障0x00000019怎么办?

原标题&#xff1a;电脑出现蓝屏故障0x00000019怎么办&#xff1f;电脑开机出现蓝屏故障0x00000019怎么处理&#xff1f;电脑出现蓝屏&#xff0c;一般都会有提示错误代码或者出错的文件名等&#xff0c;最近就有用户跟小编反映&#xff0c;开机出现了蓝屏故障&#xff0c;提示…

画世界怎么用光影_怎么绘制光影?插画人物光感的塑造教程

怎么绘制光影&#xff1f;在角色氛围图里面他们的主要构成是&#xff1a;角色环境&#xff1b;打光色彩组成&#xff0c;有同学理解为角色场景结合起来&#xff0c;大概就是人和景组合构成。但我们过去都是的画都是只画角色&#xff0c;那么现在想营造氛围感&#xff0c;在画的…

【转】ABP源码分析四十五:ABP ZERO中的EntityFramework模块

AbpZeroDbContext&#xff1a;配置ABP.Zero中定义的entity的Dbset EntityFrameworkModelBuilderExtensions:给PrimitivePropertyConfiguration添加了扩展方法用于创建Index。 AbpZeroDbModelBuilderExtensions&#xff1a;给DbModelBuilder添加了扩展方法用于表的重命名。 AbpZ…

Enterprise Library系列文章回顾与总结

http://www.readygo.com.cn/NETFW/070216/Enterprise-Library-JiLiWenZhangHuiGuYuLongJi.htm 转载于:https://www.cnblogs.com/encounter/archive/2007/04/24/2188877.html

机载计算机结构,机载计算机

摘要 针对目前自动测试设备的通用性设计&#xff0c;提出了一种基于PXI总线的测试平台。文中对PXI测试系统、接口适配器和开关网络进行了说明&#xff1b;介绍了测试软件和故障诊断系统的设计&#xff1b;分析了该系统设计过程中面临的通用性、故障诊断与定位等问题。其设计思想…

WinCE中串口驱动及接口函数介绍

作者&#xff1a;ARM-WinCE 在WinCE中&#xff0c;串口驱动实际上就是一个流设备驱动,具体架构如图&#xff1a; 串口驱动本身分为MDD层和PDD层。MDD层对上层的Device Manager提供了标准的流设备驱动接口(COM_xxx)&#xff0c;PDD层实现了HWOBJ结构及结构中若干针对于串口硬件操…

广告牌定时器怎么设置时间_定时开关如何设置时间呢

定时开关如何设置时间呢创意家居 作者&#xff1a;fiorile 时间&#xff1a;2018-06-26 15:56:58 浏览量&#xff1a;7950 网民普遍认知程度&#xff1a;30% 来源&#xff1a;住范儿rita1982提出定时开关怎么设置时间呢的问题&#xff0c;小XHING重点说明微电脑如何调定时开关的…

【转】ABP源码分析四十六:ABP ZERO中的Ldap模块

通过AD作为用户认证的数据源。整个管理用户认证逻辑就在LdapAuthenticationSource类中实现。 LdapSettingProvider&#xff1a;定义LDAP的setting和提供DefautValue。主要提供配置访问AD数据库的账号信息。 LdapSettings/ILdapSettings:通过settingManager获取LDAP settings Ab…

广州.NET俱乐部 VSTS活动报道

首先要感谢广州.NET开发人员一直以来对俱乐部的支持&#xff0c;另外&#xff0c;要感谢微软鞠海洋提供的丰富礼品。本次活动主线是VSTS&#xff0c;俱乐部的讲师黎波为我们讲解了VSTS的基本组成和功能分布&#xff0c;让我们大家从宏观上了解VSTS。在活动上&#xff0c;微软的…

东北师范大学计算机学院的导师,东北师范大学计算机科学与信息技术学院研究生导师简介-王佳男...

东北师范大学计算机科学与信息技术学院研究生导师简介-王佳男东北师范大学 免费考研网/2016-04-15姓名王佳男职称高级工程师专业办公室电话**Emailwangjnnenu.edu.cn研究领域智能算法&#xff0c;计算机网络个人简介个人简历 教学工作科研教研论文/著作获奖情况资源链接信息修改…

WinCE文件目录定制及内存调整

作者&#xff1a;ARM-WinCE 这个说起来比较简单&#xff0c;WinCE的文件目录结构以及文件的位置都是在DAT文件中定义的。所有的dat文件会在WinCE编译时合并成initobj.dat文件&#xff0c;WinCE会根据DAT中的描述生成相应目录。 关于DAT的格式&#xff0c;可以参考我以前的一片…