C#轻量级企业事务 - TransactionScope
using System;
using System.Data.SqlClient;
using System.Transactions; namespace SomeDBTransaction
{
class Program
{
static void Main(string[] args)
{
string con1 = "SERVER=.; DATABASE=db1; UID=sa; PWD=llh";
string con2 = "SERVER=.; DATABASE=db2; UID=sa; PWD=llh";
string sqlStr1 = "U_t1";
string sqlStr2 = "U_t1"; int resu=CreateTransactionScope(con1, con2, sqlStr1, sqlStr2);
Console.WriteLine("受影响的行数:"+resu); Console.Read();
} // 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);
command1.CommandType = System.Data.CommandType.StoredProcedure;
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);
command1.CommandType = System.Data.CommandType.StoredProcedure;
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);
}
catch (Exception ex)
{
writer.WriteLine("ERROR: {0}",ex.Message);
}
// Display messages.
Console.WriteLine(writer.ToString()); return returnValue;
}
} }
CODE
代码很简单啦 ~~,就多加了一个using而已
C#轻量级企业事务 - TransactionScope的更多相关文章
- Cassandra如何利用线性一致性来实现轻量级的事务
分布式数据库会面临着一个独特的挑战,就是数据必须要严格的按照读,写顺序执行.如创建用户,转账,两个潜在的写操作竞态条件必须要确保一个写操作必须在另外一个之前发生.在Cassandra中,使用Paxos ...
- 分布式事务TransactionScope
分布式事务TransactionScope 以下是分布式事务的所有情况的例子了,包含了事务套事务,事务套存储过程事务,经过测试,TransactionScope对于分布式事务的各种情况支持的很好. 使 ...
- 探索逻辑事务 TransactionScope
一.什么是TransactionScope? TransactionScope即范围事务(类似数据库中的事务),保证事务声明范围内的一切数据修改操作状态一致性,要么全部成功,要么全部失败回滚. MSD ...
- 代码块事务—TransactionScope
今天上班遇到这样的业务:将删除的用户信息记录到记录表,再删除用户表中的信息. 可以说是不幸也可以说是幸运的. 在以往遇到这样的业务,我会考虑到各种出现异常或者失败的情况.在删除一张表数据失败的情况,对 ...
- 【NET Core】事务TransactionScope
.NET FrameWork时期: TransactionScope是FCL System.Transactions命名空间下的分布式事务组件,它默认为本地事务,当系统有需要时可以自动提升为分布式事务 ...
- 分布式事务TransactionScope所导致几个坑
记录一下,个人见解,欢迎指正 错误: 1.该伙伴事务管理器已经禁止了它对远程/网络事务的支持. (异常来自 HRESULT:0x8004D025)2.事务已被隐式或显式提交,或已终止3.此操作对该事务 ...
- 轻量级企业私有云 JimV 分享
当前云市场分析 云分两种,公有云.私有云.目前市面上的云产品,对于中小规模的企业来讲,痛点有如下几点: 私有云: 1.VMware ESXi 类: a) 授权费用昂贵: b) 创建虚拟机费时费力: 2 ...
- C# TransactionScope 事务类
微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看Tran ...
- 事务使用中如何避免误用分布式事务(System.Transactions.TransactionScope)
1:本地事务DbTransaction和分布式事务TransactionScope的区别: 1.1:System.Data.Common.DbTransaction: 本地事务:这个没什么好说了,就是 ...
随机推荐
- hibernate配置之<property name="hbm2ddl.auto">create</property>导致每次创建SessionFactory都清空数据库中的数据
参考:http://stackoverflow.com/questions/6611437/how-to-make-hibernate-not-drop-tables 我遇到的问题就是: List l ...
- MyEclipse配置Tomcat 并编写第一个JSP程序
安装myeclipse之后配置tomcat服务器,在window里选择servers 选择tomcat的文件夹路径(我的是从别人那里考过来的文件夹) 选中上enable即可 出现了这个界面 在这里可以 ...
- Java 数据结构之ArrayList
ArrayList:数组队列,就是动态数组,可以动态的增加和减少元素.实现了ICollection和IList接口.灵活的设置数组的大小 具体的用法: 1.创建:ArrayList list = ne ...
- Android 获取最近应用的缩略图
最近有项需求是获取应用的缩略,用于在动画时显示.因此就对此块知识简要了解了一下. 在android中获取视频文件的缩略图有三种方法: 1.从媒体库中查询 新视频增加后需要SDCard重新扫描才能给新增 ...
- 基于Linux的oracle数据库管理 part3( 存储 网络 常用命令 )
主要内容 1. 常用存储模式 2. 配置网络 3. 维护指令 常用存储模式 - 文件系统 优点:管理方便, 缺点:读写慢 - 裸设备 只没有被格式化和挂载的磁盘, 只能有程序直接访问, 不能被操作系统 ...
- 非常非常好!写了好久 k-th-smallest-in-lexicographical-order
https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/ 我没做出来.还是脑子不够清醒. 下面这个解法真的很棒很棒. ...
- 【干货】国外程序员整理的 C++ 资源大全
关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++ Standard ...
- 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,循环控制及其优化
上两篇博客 8皇后以及N皇后算法探究,回溯算法的JAVA实现,递归方案 8皇后以及N皇后算法探究,回溯算法的JAVA实现,非递归,数据结构“栈”实现 研究了递归方法实现回溯,解决N皇后问题,下面我们来 ...
- git plumbing 更加底层命令解析-深入理解GIT
原文: http://rypress.com/tutorials/git/plumbing 本文详细介绍GIT Plumbing--更加底层的git命令,你将会对git在内部是如何管理和呈现一个项目r ...
- UVa 10250 The Other Two Trees
还是读了很长时间的题,不过题本身很简单. 可以把四棵树想象成正方形的四个顶点,已知两个相对顶点的坐标,求另外两个坐标. 不过,原题可没直接这么说,中间需要一些小证明. 题中说有一个平行四边形然后分别以 ...