transactionscope mysql_TransactionScope 的基本原理简介

transactionscope mysql_TransactionScope 的基本原理简介C#的事务编程1Db事务DbConnection中创建基于当前连接的DbTransaction2使用TransactionScope,创建环境事务一旦创建,在这个环境包含的DbConnection实例都会根据连接字符串中的Sqlserver连接字符串支持,是否自动附加当前环境事务.连接字符串关键字(Enlist)SqlConnection.ConnectionString属性…

大家好,又见面了,我是你们的朋友全栈君。

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数据库 需要驱动支持。

f42d7d1e7fa0736e0b93329fb9b349be.png

32fa6895f5fdab123a3026663f5e20ab.png

974c79340fbeee35baee9a0d346073c4.png

d848b16cb52292b753476fd916636ac9.png

以下来自MSDN:

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

IC160177.jpeg重要事项

建议您创建使用隐式事务 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 intCreateTransactionScope(string connectString1, stringconnectString2,string commandText1, stringcommandText2)

{//Initialize the return value to zero and create a StringWriter to display results.

int returnValue = 0;

System.IO.StringWriter writer= newSystem.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 = newTransactionScope())

{using (SqlConnection connection1 = newSqlConnection(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 = newSqlCommand(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 = newSqlConnection(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= newSqlCommand(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());returnreturnValue;

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/161935.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • Neokylin-Server离线环境、跨主机、使用Docker部署PXC集群

    Neokylin-Server离线环境、跨主机、使用Docker部署PXC集群Neokylin-Server离线环境、跨主机、使用Docker部署PXC集群Neokylin-Server离线环境、跨主机、使用Docker部署PXC集群一、说明二、部署过程:1.切换root账号或所有语句加sudo;2.关闭6个节点防火墙(或打开端口);3.6个节点导入rpm包后安装基础环境:4.设置所有节点;5.启动docker并导入images;6.时间同步;7.etcd集群配置;8.部署flannel网络;9.n1-n3部署pxc;10.m1-m3部署Haproxy+Keepa

  • 135首经典欧美歌曲 —附下载地址

    135首经典欧美歌曲 —附下载地址135首经典欧美歌曲—附下载地址(2011-03-1212:52:14)转载▼标签:杂谈分类:转载精选源自张海峡新浪博客:http://blog.sina.com.cn/zhanghaixiaf

  • hostapd 移植和使用[通俗易懂]

    hostapd 移植和使用[通俗易懂]一、hostapd简介hostapd是一个用户态用于AP和认证服务器的守护进程。它实现了IEEE802.11相关的接入管理,IEEE802.1X/WPA/WPA2/EAP认证,RADIUS客户端,EAP服务器和RADIUS认证服务器。二、hostapd移植1.打开hostap官网,下载hostapd-2.7.tar.gz源码包;      2.解压源码包:…

  • 清北学堂模拟赛d3t6 c

    清北学堂模拟赛d3t6 c分析:比较神奇的一道题.要把树变成环肯定要先变成链,然后把链给拼接成环.接下来考虑一个脑洞大开的树形dp:设f[i][0]表示i不与父节点相连的链数,f[i][1]表示i与父节点相连的链数,先考虑怎么

  • RPN网络解读

    RPN网络解读RPN网络源码解读在高层的featuremap初次计算anchorbox点数值604099代表一个特征维度生成9个anchorbox,但特征层w,h为啥是60,40不应该是相等的?毕竟backbone网络一系列操作(下采样),w,h同时缩小的。下不管了,不是今天主要问题。step1:也就是说原始生成大约20000boxs,再进行2k排序(每个boxs得分为目标或背景的概率,不管是背景还是目标都要得分高的)设定阈值为12000,也就是说最多保留12000,也有的源码设定2000。step2

  • 【Linux】进程间通信「建议收藏」

    【Linux】进程间通信「建议收藏」目录1.进程间通信1.1.进程间通信的目的1.2.如何实现进程间通信2.管道通信2.1.匿名管道2.1.1创建匿名管道2.1.2.深入理解匿名管道2.2.命名管道2.2.1.创建命名管道3.systemV标准进程间通信3.1.共享内存3.1.1.实现原理3.1.2.代码实现3.2.消息队列(了解)3.2.1实现原理3.3.信号量(了解)3.3.1.实现原理1.进程间通信1.1.

    2022年10月11日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号