.NET实现事务的编码方式
1,在T-SQL语句中用begin tran,end tran的方式
begin tran
--select top(1) * from dbo.test with(updlock)
update test with(updlock)
set name ='name_upd'
where id = 1
commit tran
2,利用ADO.NET的SqlTransaction类
3,利用.NET2.0提供的TransactionScope类
// 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的博客
https://blog.appliedis.com/2015/05/27/the-magic-of-transactionscope/
下面这些情况会被提升成MSDTC分布式事务
1)不同服务器的数据库
2)同一服务器,不同数据库(连接字符串不同)。
3)同一服务器,同一数据库(连接字符串相同)。但出现了嵌套调用的情况。
就是外层开了一个SqlConnection,还没有关闭,里面又New了一个SqlConnection,这时,.Net的连接池会开两个单独的连接,两个连接当然会 当成分布式的事务了。
要保证事务不被提升成分布式事务,有两种方式。
1)数据库字符串只能用同一个,事务内的所有操作,都共用同一个SqlConnection。
2)数据库字符串只能用同一个,事务内的所有操作,都必须串行的关闭自己开的连接。这种情况连接池会共用同一连接。
一般写了DBHelper的工程,都是满足这个条件的。
要在同一事务内,访问同一服务器的不同数据库。有两种方式:
① 在sql文中,显示指定数据库名: TestDB..Table1
② 利用SqlConnection的ChangeDatabase方法,更改成目标数据库名,然后执行SQL语句,记住用完切回原来的数据库。
4,如果是跨服务器部署数据库,那就要必须用分布式事务了,启用MSDTC的方法
http://blog.csdn.net/zy0421911/article/details/52225082
可以用DTCPing.exe软件测试两端是否正常连通
.NET实现事务的编码方式的更多相关文章
- spring事务的开启方式(编程式和声明式)
1.编程式事务:编码方式实现事务管理(代码演示为JDBC事务管理) Spring实现编程式事务,依赖于2大类,分别是上篇文章提到的PlatformTransactionManager,与模版类Tran ...
- Spring事务管理实现方式之编程式事务与声明式事务详解(转)
原文:https://blog.csdn.net/liaohaojian/article/details/70139151 编程式事务 编码方式实现事务管理(代码演示为JDBC事务管理) Spring ...
- Redis与KV存储(RocksDB)融合之编码方式
Redis与KV存储(RocksDB)融合之编码方式 简介 Redis 是目前 NoSQL 领域的当红炸子鸡,它象一把瑞士军刀,小巧.锋利.实用,特别适合解决一些使用传统关系数据库难以解决的问题.Re ...
- Java不同编码方式,中英文字符所占字节数
测试代码 public class Test { public static void main(String[] args){ String[] charsetNames={ "UTF-8 ...
- form表单编码方式设置为multipart/form-data,后台参数出现乱码情况
一般在上传图片过程中,form中的编码方式一般采用multipart/form-data方式编码,但是后台这取参数时,可能会出现乱码情况:这里后台要采用转换编码方式: 页面: 后台:获取表单元素时,
- servlet获取表单数据的方式和编码方式
.在servlet中获取表单的数据的几种方式 1>request.getParameter(“name”)://获取指定名称的值,返回值类型是一个字符串 2>request.getPa ...
- python 改变字符串的编码方式
字符串str的编码方式为utf-8,转化为gbk,分为两步 1. str=str.decode('utf-8') 2. str=str.encode('gbk')
- [No000040]取得一个文本文件的编码方式
using System; using System.IO; using System.Text; /// <summary> /// 用于取得一个文本文件的编码方式(Encoding). ...
- Java文件读写操作指定编码方式防乱码
读文件:BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高效读取. 可以指定缓冲区的大小,或者可使用默认的大小.大多数情况下,默认值就足够大了. 通常,Re ...
随机推荐
- luoguP3302 [SDOI2013]森林 主席树 启发式合并
题目链接 luoguP3302 [SDOI2013]森林 题解 本来这题树上主席树暴力启发式合并就完了 结果把lca写错了... 以后再也不这么写了 复杂度\(O(nlog^2n)\) "f ...
- BZOJ.2229.[ZJOI2011]最小割(最小割树)
题目链接 题意:给定一张无向图,求任意两点之间的最小割. 在所有点中任选两个点作为源点\(S\).汇点\(T\),求它们之间的最小割\(ans\),并把原图分成两个点集\(S',T'\),用\(ans ...
- BZOJ5384 有趣的字符串题 回文树
神奇的结论: 一个字符串的所有回文后缀的长度,可以形成$k$个等差数列,$k$是$log$级的 考虑前$R$个字符组成的字符串,对于一个等差数列,假设组成这个等差数列的回文串,最短的叫$a$,最长的叫 ...
- java中哪些数值不能被初始化
main方法中的变量不能被初始化 final修饰的变量不能被初始化·
- CocosCreator资源工作流程
--摘自官方文档 资源工作流程 添加资源 资源管理器 提供了三种在项目中添加资源的方式: 通过 创建按钮 添加资源 在操作系统的文件管理器中,将资源文件复制到项目资源文件夹下,之后再打开或激活 Coc ...
- Android中AES256加密的实现
AES加密是我们在工作中常用到一种加密方式,并且在java中也已经实现好了其相应的接口. 但是Java自带的JDK默认最多实现128位及其以下的加密.如果使用java自带的api实现aes256将会报 ...
- AngularJS中写一个包裹HTML元素的directive
有这样的一个场景,这里有一个表单: <form role="form"> ...</form> 我们希望在form的外层动态包裹上一层. 有可能是这样 ...
- Servlet第五课:Cookie的使用
目标规划: 通过这一节课,我们能够懂得怎样使用Cookie.以及怎样获取Cookie中的内容. 插播广告:博客之星评选.点击投我一票.谢谢. Cookie的具体概述. 1. Cookie 是保存在cl ...
- Fortran中的指针使用
Fortran中的指针如何使用,功能怎样,下面的的5个例子足可以让你明白一切! 对于单个值,用起来很简单,但是无法体现指针的强大功能, 示例1: program test_pointer_1 impl ...
- LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 (转)
很多伙伴在更新VS2010,或者卸载VS2012安装2010后,建立Win32 Console Project/MFC项目时会出现"LINK : fatal error LNK1123: 转 ...