一、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 事务处理 -环境事务的更多相关文章

  1. C# Transaction 事务处理 -依赖事务

    在DependentTransaction()方法中,实例化CommittableTransaction类,创建一个根事务,显示事务的信息.接着, tx.DependentClone()方法创建一个依 ...

  2. 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 ...

  3. Python Django,事务,transaction.atomic,事务保存点

    from django.shortcuts import renderfrom django.http import HttpResponsefrom django.views.generic imp ...

  4. Compensating Transaction Pattern(事务修正模式)

    Undo the work performed by a series of steps, which together define an eventually consistent operati ...

  5. laravel transaction : laravel 的事务是不支持eloquent的, 要用DB::的方式

    数据库事务处理# 你可以使用 transaction 方法,去执行一组数据库事务处理的操作: DB::transaction(function() { DB::table('users')->u ...

  6. Oracle Database Transaction Isolation Levels 事务隔离级别

    Overview of Oracle Database Transaction Isolation Levels Oracle 数据库提供如下事务隔离级别: 已提交读隔离级别 可串行化隔离级别 只读隔 ...

  7. day056-58 django多表增加和查询基于对象和基于双下划线的多表查询聚合 分组查询 自定义标签过滤器 外部调用django环境 事务和锁

    一.多表的创建 from django.db import models # Create your models here. class Author(models.Model): id = mod ...

  8. SET TRANSACTION - 设置当前事务的特性

    SYNOPSIS SET TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ O ...

  9. START TRANSACTION - 开始一个事务块

    SYNOPSIS START TRANSACTION [ ISOLATION LEVEL { READ COMMITTED | SERIALIZABLE } ] [ READ WRITE | READ ...

随机推荐

  1. [CF1010D]Mars Over_位运算性质

    Mars rover 题目链接:http://codeforces.com/problemset/problem/1010/D 数据范围:略. 题解: 因为每次只改一个,改完之后改回去,这个性质很重要 ...

  2. [转帖]「知乎知识库」— 5G

    「知乎知识库」— 5G 甜草莓 https://zhuanlan.zhihu.com/p/55998832 ​ 通信 话题的优秀回答者 已关注 881 人赞同了该文章 谢 知识库 邀请~本文章是几个答 ...

  3. Redis 数据结构 & 原理 & 持久化

    一 概述 redis是一种高级的key-value数据库,它跟memcached类似,不过数据可以持久化,而且支持的数据类型也很丰富. Redis支持五种数据类型:string(字符串),hash(哈 ...

  4. 【k8s第一步】Kubernetes-Linux系统初始化【已修正错误】

    ⒈配置Linux的IP地址 vim /etc/sysconfig/network-scripts/ifcfg-ens33v ifcfg-ens33是网卡的最新命名规范,它会从BIOS => PC ...

  5. Ural 1298 Knight 题解

    目录 Ural 1298 Knight 题解 题意 题解 程序 Ural 1298 Knight 题解 题意 给定一个\(n\times n(1\le n\le8)\)的国际象棋棋盘和一个骑士(基本上 ...

  6. (转)如何真正实现由文档驱动的API设计?

    前言 本文主要介绍了一种新的开发思路:通过反转开发顺序,直接从API文档中阅读代码.作者认为通过这种开发方式,你可以更清楚地知道文档表达出什么以及它应该如何实现. 如果单从API文档出发,由于信息量不 ...

  7. Codeforces 1097E. Egor and an RPG game

    传送门 首先考虑怎么算 $f(n)$ (就是题目里面那个 $f(n)$) 发现可以构造一组序列大概长这样: ${1,3,2,6,5,4,10,9,8,7,15,14,13,12,11,...,n(n+ ...

  8. 对vuex分模块管理

    为什么要分模块: 由于使用单一状态树,应用的所有状态会集中到一个比较大的对象.当应用变得非常复杂时,store 对象就有可能变得相当臃肿.为了解决以上问题,Vuex 允许我们将 store 分割成模块 ...

  9. canvas学习之初级运用

    <html> <head> <meta charset=utf-8> <title>绘制简单图形</title> <style typ ...

  10. fetch---基本使用

    一.fetch fetch是一种XMLHttpRequest的一种替代方案,在工作当中除了用ajax获取后台数据外我们还可以使用fetch.axios来替代ajax 二.安装 执行npm instal ...