C# Transaction 事务处理 -环境事务
一、TransactionScope 环境事务
static async Task TransactionScopeAsync()
{
using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
Transaction.Current.TransactionCompleted += OnTransactionCompleted; Utilities.DisplayTransactionInformation("Ambient TX created",
Transaction.Current.TransactionInformation); var s1 = new Student
{
FirstName = "Angela",
LastName = "Nagel",
Company = "Kantine M101"
};
var db = new StudentData();
await db.AddStudentAsync(s1); if (!Utilities.AbortTx())
scope.Complete();
else
Console.WriteLine("transaction will be aborted"); } // scope.Dispose() }
二、嵌套事务
static void NestedScopes()
{
using (var scope = new TransactionScope())
{
Transaction.Current.TransactionCompleted += OnTransactionCompleted; Utilities.DisplayTransactionInformation("Ambient TX created",
Transaction.Current.TransactionInformation); using (var scope2 =
new TransactionScope(TransactionScopeOption.RequiresNew))
{
Transaction.Current.TransactionCompleted += OnTransactionCompleted; Utilities.DisplayTransactionInformation(
"Inner Transaction Scope",
Transaction.Current.TransactionInformation); scope2.Complete();
}
scope.Complete();
} }
事务完成代码
static void OnTransactionCompleted(object sender, TransactionEventArgs e)
{
Utilities.DisplayTransactionInformation("TX completed",
e.Transaction.TransactionInformation);
}
你可能不知道这一点,在 .NET Framework 4.5.0 版本中包含有一个关于 System.Transactions.TransactionScope 在与 async/await 一起工作时会产生的一个严重的 bug 。由于这个错误,TransactionScope 不能在异步代码中正常操作,它可能更改事务的线程上下文,导致在处理事务作用域时抛出异常。
这是一个很大的问题,因为它使得涉及事务的异步代码极易出错。
好消息是,在 .NET Framework 4.5.1 版本中,微软发布了这个 "异步连接" 错误的修复程序。作为开发者的我们需要明确的做到以下两点:
- 如果说你在 TransactionScope 代码中使用 async/await,你需要将框架升级到 .NET 4.5.1 或以上版本。
- 在有包装异步代码的 TransactionScope 的构造函数中指定
TransactionScopeAsyncFlowOption.Enabled .
TransactionScopeAsyncFlowOption
在 .NET 4.5.1中,TransactionScope 有一个名为 TransactionScopeAsyncFlowOption 的新枚举,可以在构造函数中提供。 您必须通过指定,明确地选择跨线程连续的事务流,如下:
using (var tx = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
await SomeMethodInTheCallStackAsync()
.ConfigureAwait(false); tx.Complete();
}
你可能很好奇,默认的 TransactionScopeAsyncFlowOption 是 Suppress(阻止的),因为微软想避免破坏 .NET 4.5.0 版本中代码库中行为。
最后
使用 TransactionScope 结合 async / await 时,你应该更新所有使用 TransactionScope 的代码路径以启用 TransactionScopeAsyncFlowOption.Enabled 。 这样才能使事务能够正确地流入异步代码,防止在TransactionScope下使用时业务逻辑不正常。
C# Transaction 事务处理 -环境事务的更多相关文章
- C# Transaction 事务处理 -依赖事务
在DependentTransaction()方法中,实例化CommittableTransaction类,创建一个根事务,显示事务的信息.接着, tx.DependentClone()方法创建一个依 ...
- 14.3.2.1 Transaction Isolation Levels 事务隔离级别
14.3.2 InnoDB Transaction Model InnoDB 事务模型 14.3.2.1 Transaction Isolation Levels 事务隔离级别 14.3.2.2 au ...
- Python Django,事务,transaction.atomic,事务保存点
from django.shortcuts import renderfrom django.http import HttpResponsefrom django.views.generic imp ...
- Compensating Transaction Pattern(事务修正模式)
Undo the work performed by a series of steps, which together define an eventually consistent operati ...
- laravel transaction : laravel 的事务是不支持eloquent的, 要用DB::的方式
数据库事务处理# 你可以使用 transaction 方法,去执行一组数据库事务处理的操作: DB::transaction(function() { DB::table('users')->u ...
- Oracle Database Transaction Isolation Levels 事务隔离级别
Overview of Oracle Database Transaction Isolation Levels Oracle 数据库提供如下事务隔离级别: 已提交读隔离级别 可串行化隔离级别 只读隔 ...
- day056-58 django多表增加和查询基于对象和基于双下划线的多表查询聚合 分组查询 自定义标签过滤器 外部调用django环境 事务和锁
一.多表的创建 from django.db import models # Create your models here. class Author(models.Model): id = mod ...
- SET TRANSACTION - 设置当前事务的特性
SYNOPSIS SET TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ O ...
- START TRANSACTION - 开始一个事务块
SYNOPSIS START TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ ...
随机推荐
- 《MIT 6.828 Lab 1 Exercise 10》实验报告
本实验的网站链接:MIT 6.828 Lab 1 Exercise 10. 题目 Exercise 10. To become familiar with the C calling conventi ...
- [Cometoj#4 E]公共子序列_贪心_树状数组_动态规划
公共子序列 题目链接:https://cometoj.com/contest/39/problem/E?problem_id=1585 数据范围:略. 题解: 首先可以考虑知道了$1$的个数和$3$的 ...
- 基础python规范
一.注释 合理的代码注释应该占源代码的 1/3 左右,Python 语言允许在任何地方插入空字符或注释,但不能插入到标识符和字符串中间. 在 Python 中,通常包括 3 种类型的注 ...
- CF网络流练习
1. 103E 大意: 给定$n$个集合, 满足对于任意的$k$, 任意$k$个集合的并集都不少于$k$. 要求选出$k$个集合$(k> 0)$, 使得并恰好等于$k$, 输出最少花费. Hal ...
- MySQL设置自增字段
1.MySQL每张表只能有1个自增字段,这个自增字段即可作为主键,也可用作非主键使用,但是请注意将自增字段当做非主键使用时必须为其添加唯一索引,否则系统将会报错 )将自动增长字段设置为主键 CREAT ...
- Git FLS的使用
克隆git地址后,一些文件内容被隐藏. 显示如下: version https://git-lfs.github.com/spec/v1oid sha256:xxxxxxxxxxxxxxxxxxxxx ...
- USB设备描述符
/* USB Standard Device Descriptor */ const u8 Virtual_Com_Port_DeviceDescriptor[] = { 0x12, /* bLeng ...
- 微信小程序带参数生成二维码
wx.request({ url: 'https://api.weixin.qq.com/cgi-bin/token', header: { 'content-type': 'application/ ...
- H5存储方式
数据存储 var arr = [0, 1, 1, 1]; //存储,IE6~7 cookie 其他浏览器HTML5本地存储 if (window.localStorage) { localStorag ...
- Centos7.7安装swoole
系统版本:centos 7.7(查看系统版本:cat /etc/redhat-release) 执行命令安装swoole: yum update -y && yum remove ph ...