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 = ;
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 = ;
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. RabbitMQ的应用场景以及基本原理简介

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

  2. kafka 基本原理简介

    Kafka是啥?用Kafka官方的话来说就是: Kafka is used for building real-time data pipelines and streaming apps. It i ...

  3. LVS基本原理

    LVS基本原理 简介 负载调度器.真实服务器群节点一起被称为LVS.LVS负载调度器(有时也称为负载平衡器),接收所服务的所有接入服务集群的请求,并决定集群中的哪个节点应该回复其请求. 1)负载调度器 ...

  4. 通过 Terracotta实现基于Tomcat的Web应用集群

    [转]通过 Terracotta实现基于Tomcat的Web应用集群 博客分类: 企业应用面临的问题 Java&Socket 开源组件的应用 tomcatweb session集群服务器负载均 ...

  5. JavaScript函数式编程究竟是什么?

    摘要: 理解函数式编程. 作者:前端小智 原文:JS中函数式编程基本原理简介 Fundebug经授权转载,版权归原作者所有. 在长时间学习和使用面向对象编程之后,咱们退一步来考虑系统复杂性. 在做了一 ...

  6. 【ARTS】01_30_左耳听风-201900603~201900609

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  7. Prometheus集群介绍-1

    Prometheus监控介绍 公司做教育的,要迁移上云,所以需要我这边从零开始调研加后期维护Prometheus:近期看过二本方面的prometheus书籍,一本是深入浅出一般是实战方向的:官方文档主 ...

  8. Hadoop 相关知识点(一)

    作业提交流程(MR执行过程) Mapreduce2.x Client:用来提交作业 ResourceManager:协调集群上的计算资源的分配 NodeManager:负责启动和监控集群上的计算容器( ...

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

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

随机推荐

  1. 算法:希尔排序(Shell Sort)

    背景 在三种简单的排序算法中(冒泡.选择和插入)插入排序的算法最好,不过插入过程可能需要进行大量的移动,如何尽可能少的移动元素呢?希尔排序正是基于对这个问题的思考而想出来的,考虑到希尔排序对已排序数组 ...

  2. Unity3d-Particle System系统的学习(三)

    这节课我们来实战下上几节讲的几乎所有Particle System用到的参数. 我们今天制作下图所示的粒子: 类似于带有光晕的魔法球.用到的材质也就是上节课用到的材质贴图. http://pan.ba ...

  3. Python Post img

    from poster.encode import multipart_encode from poster.streaminghttp import register_openers import ...

  4. Adhoc

    没觉得Adhoc还有什么做的,这几年貌似很冷了,从通信的角度讲,实现比较困难,实际意义不大,国内最近又跟风了VANET.以我同学做的为例,他考虑用Adhoc做野外分散点的自组网(一个集体内),但从通信 ...

  5. 再有人问你synchronized是什么,就把这篇文章发给他

    在再有人问你Java内存模型是什么,就把这篇文章发给他.中我们曾经介绍过,Java语言为了解决并发编程中存在的原子性.可见性和有序性问题,提供了一系列和并发处理相关的关键字,比如synchronize ...

  6. 关于DLL文件和EXE文件不在同一目录下的设置【转】

    https://www.cnblogs.com/chaosimple/archive/2012/08/13/2636181.html 关于DLL文件和EXE文件不在同一目录下的设置 在开发程序结束后, ...

  7. require.js 最佳实践

    require.js是一个js库,相关的基础知识,前面转载了两篇博文:Javascript模块化编程(require.js), Javascript模块化工具require.js教程,RequireJ ...

  8. leetcode414-第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

  9. Linux服务器权限管理之sudo高级应用

    Sudo是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,减少了root用户的登陆和管理时间,提高了安全性,Sudo不是对shell的一个代替,它是面向每个命令的. Linux系统的 ...

  10. VS2010 配置PCL1.6.0AII in one 无法启动程序ALL_BUILD

    无法启动程序D:\build\debug\ALL_BUILD 系统找不到指定文件 解决办法:将project_inliers工程设置为启动项目    找到项目右击--设为启动项目. 将project_ ...