C# 的事务编程

1 Db事务

DbConnection 中创建基于当前连接的 DbTransaction

2  使用TransactionScope ,创建环境事务

一旦创建,在这个环境包含的DbConnection 实例 都会根据连接字符串中的

Sqlserver 连接字符串支持,是否自动附加当前环境事务.

连接字符串关键字(Enlist)
       SqlConnection.ConnectionString 属性支持关键字 Enlist,该关键字指示 System.Data.SqlClient 是否将检测事务上下文并自动在分布式事务中登记连接。 如果 Enlist=true,连接将自动在打开的线程的当前事务上下文中登记。 如果 Enlist=false,SqlClient 连接不会与分布式事务进行交互。 Enlist 的默认值为 true。 如果连接字符串中未指定 Enlist,若在连接打开时检测到一个,连接将自动在分布式事务中登记。

Server=(local)SQL2005;Database=Northwind;Integrated Security=SSPI;auto-enlist=false

Mysql 等其他 OLDP数据库 需要驱动支持。

以下来自MSDN:

System.Transactions 基础结构提供了这两个的显式编程模型基于 Transaction 类,以及隐式编程模型使用 TransactionScope 类,在其中事务自动管理基础结构。

重要事项

建议您创建使用隐式事务 TransactionScope 类,以便为您自动管理环境事务上下文。 您还应该使用 TransactionScope 和 DependentTransaction 跨多个函数调用或多个线程调用需要使用相同的事务的应用程序的类。 此模型的详细信息,请参阅 Implementing An Implicit Transaction Using Transaction Scope 主题。 编写事务应用程序的详细信息,请参阅 Writing A Transactional Application。

在实例化 TransactionScope 通过 new 语句中,事务管理器确定哪些事务参与进来。 一旦确定,该范围将始终参与该事务。 此决策基于两个因素:是否存在环境事务以及构造函数中 TransactionScopeOption 参数的值。 环境事务是在代码中执行的事务。 可通过调用 Current 类的静态 Transaction 属性,获取对环境事务的引用。 有关如何使用此参数的详细信息,请参阅的"事务流的管理"部分 Implementing An Implicit Transaction Using Transaction Scope 主题。

如果在事务范围内未不发生任何异常 (即之间的初始化 TransactionScope 对象并调用其 Dispose 方法),则范围所参与的事务可以继续。 如果在事务范围内发生异常,参与到其中的事务将回滚。

当您的应用程序完成所有工作时它想要在事务中执行,应调用 Complete 方法一次,以通知该事务管理器是可接受,即可提交事务。 未能调用此方法中止事务。

调用 Dispose 方法将标记事务范围的末尾。 在调用此方法之后所发生的异常不会影响事务。

如果您修改的值 Current 内某个范围内,将引发异常时 Dispose 调用。 但是,在作用域结束时,以前的值被还原。 此外,如果您调用 Dispose 上 Current 在事务范围创建事务,事务将中止范围的末尾。

// This function takes arguments for 2 connection strings and commands to create a transaction
// involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the
// transaction is rolled back. To test this code, you can connect to two different databases
// on the same server by altering the connection string, or to another 3rd party RDBMS by
// altering the code in the connection2 code block.
static public int CreateTransactionScope(string connectString1, string connectString2,string commandText1, string commandText2)
{// Initialize the return value to zero and create a StringWriter to display results.int returnValue = 0;System.IO.StringWriter writer = new System.IO.StringWriter();try{// Create the TransactionScope to execute the commands, guaranteeing// that both commands can commit or roll back as a single unit of work.using (TransactionScope scope = new TransactionScope()){using (SqlConnection connection1 = new SqlConnection(connectString1)){// Opening the connection automatically enlists it in the // TransactionScope as a lightweight transaction.
                connection1.Open();// Create the SqlCommand object and execute the first command.SqlCommand command1 = new SqlCommand(commandText1, connection1);returnValue = command1.ExecuteNonQuery();writer.WriteLine("Rows to be affected by command1: {0}", returnValue);// If you get here, this means that command1 succeeded. By nesting// the using block for connection2 inside that of connection1, you// conserve server and network resources as connection2 is opened// only when there is a chance that the transaction can commit.   using (SqlConnection connection2 = new SqlConnection(connectString2)){// The transaction is escalated to a full distributed// transaction when connection2 is opened.
                    connection2.Open();// Execute the second command in the second database.returnValue = 0;SqlCommand command2 = new SqlCommand(commandText2, connection2);returnValue = command2.ExecuteNonQuery();writer.WriteLine("Rows to be affected by command2: {0}", returnValue);}}// The Complete method commits the transaction. If an exception has been thrown,// Complete is not  called and the transaction is rolled back.
            scope.Complete();}}catch (TransactionAbortedException ex){writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);}catch (ApplicationException ex){writer.WriteLine("ApplicationException Message: {0}", ex.Message);}// Display messages.
    Console.WriteLine(writer.ToString());return returnValue;
}

TransactionScope 的基本原理简介相关推荐

  1. transactionscope mysql_TransactionScope 的基本原理简介

    C# 的事务编程 1 Db事务 DbConnection 中创建基于当前连接的 DbTransaction 2  使用TransactionScope ,创建环境事务 一旦创建,在这个环境包含的DbC ...

  2. 响应式编程 函数式编程_函数式编程的基本原理简介

    响应式编程 函数式编程 After a long time learning and working with object-oriented programming, I took a step b ...

  3. RabbitMQ的应用场景以及基本原理简介

    1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue)的开源实现. 2.应用场景 2.1异步处理 场景说明:用户注册后,需要发注册邮件和注册短信, ...

  4. JS中函数式编程基本原理简介

    作者:TK  译者:前端小智  来源:medium 为了保证的可读性,本文采用意译而非直译. 在长时间学习和使用面向对象编程之后,咱们退一步来考虑系统复杂性. 在做了一些研究之后,我发现了函数式编程的 ...

  5. 音频编码 -(1)音频编码基本原理简介

    (1)  音频信号的冗余信息 数字音频信号如果不加压缩地直接进行传送,将会占用极大的带宽.例如,一套双声道数字音频若取样频率为44.1KHz,每样值按16bit量化,则其码率为: 2*44.1kHz* ...

  6. 存储程序通用计算机设计方案,第1章计算机设计方案基本原理.doc

    PAGE 58 PAGE 15 计算机设计基本原理 简介 半个多世纪以来,计算机技术取得了惊人的发展.1945年时还没有能存储程序的计算机.现在,花不到一千美元买到的个人计算机比1980年花一百万美元 ...

  7. 带你认识FusionInsight Flink:既能批处理,又能流处理

    摘要:本文主要介绍了FusionInsight Flink组件的基本原理.Flink任务提交的常见问题.以及最佳实践FAQ. 本文分享自华为云社区<FusionInsight HD Flink组 ...

  8. rsa解密的应用_安全-加解密

    内容概要: 加解密基本原理简介 https简介 中间人攻简介 iOS应用简介 对称 加密算法 加密密钥和解密密钥是同一把密钥K,加解密速度快,典型算法有DES.AES等. 加解秘流程 非对称 加密算法 ...

  9. 思科isis路由的优先级_【分享】超全!集成ISIS知识详解~

    IS-IS路由协议简介 IS-IS是国际标准化组织ISO为它的无连接网络协议CLNP设计的一种动态路由协议. 随着TCP/IP协议的流行,为了提供对IP路由的支持,IETF(Internet Engi ...

最新文章

  1. python连接linux服务器并使用命令_python基于paramiko模块实现远程连接Linux虚拟机(服务器)并执行指定命令返回输出结果...
  2. Python 深度学习目标检测评价指标 :mAP、Precision、Recall、AP、IOU等
  3. php开发微信支付获取用户地址
  4. pilt图像处理_图像处理 PIL
  5. 忽然觉得照着技术文档一个demo一个demo的写是一个十分好的学习方式
  6. Python3爬虫知识点总结
  7. 猛将赵云java,这五位三国猛将临危救主,赵云只能排第二位,第一位大家都服...
  8. python中html.replace()_HTML DOM replace() 方法
  9. java连接sqlserver非默认实例连接字符串设置
  10. POJ 2778 DNA Sequence(AC自动机 + 矩阵快速幂)题解
  11. 单片机跑马灯12种c语言程序设计,基于单片机多模式带音乐跑马灯设计附完整程序代码.doc...
  12. 矩阵分析之 伪逆矩阵,左逆,右逆,广义逆
  13. 产品思维30讲(梁宁)-- 整体
  14. Android——ECG心电图的绘制实现
  15. 差分数组(简单易懂)
  16. 什么是单点故障【转载】
  17. 书单 | 无所不能的Python,从技术到办公,总有一款适合你!
  18. vijos- P1383盗窃-黑珍珠 (python + 代码优化)
  19. 键盘的各个部分和指法(图片版)
  20. 机器学习入门-kNN算法实现手写数字识别

热门文章

  1. Java集合框架源码剖析:LinkedHashSet 和 LinkedHashMap
  2. 数据库范式1NF 2NF 3NF BCNF
  3. SUN dataset图像数据集下载
  4. 自己整理的计算机视觉领域稍微容易中的期刊(第一版)
  5. 鸟哥的Linux私房菜(基础篇)- 第二十六章、Linux 核心编译与管理
  6. 安装好的nginx安装新的模块
  7. 为什么全局变量不好?[翻译]
  8. 霍金:人工智能或是人类历史上最后事件
  9. 微信公众号管理系统 RhaPHP1.2.5更新啦!
  10. 数据库的三大范式以及五大约束