TransactionScope 的基本原理简介

TransactionScope 的基本原理简介C#的事务编程1Db事务DbConnection中创建基于当前连接的DbTransaction2使用TransactionScope,创建环境事务一旦创建,在这个环境包含的DbCo

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

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

 

<span role="heading" aria-level="2">TransactionScope 的基本原理简介
 
<span role="heading" aria-level="2">TransactionScope 的基本原理简介
<span role="heading" aria-level="2">TransactionScope 的基本原理简介
 
 
<span role="heading" aria-level="2">TransactionScope 的基本原理简介
 

以下来自MSDN:

Transaction 类,以及隐式编程模型使用 TransactionScope 类,在其中事务自动管理基础结构。

System_CAPS_important重要事项

TransactionScope 类,以便为您自动管理环境事务上下文。TransactionScope 和 DependentTransaction 跨多个函数调用或多个线程调用需要使用相同的事务的应用程序的类。Implementing An Implicit Transaction Using Transaction Scope 主题。Writing A Transactional Application。

 

TransactionScope 通过 new 语句中,事务管理器确定哪些事务参与进来。 一旦确定,该范围将始终参与该事务。 环境事务是在代码中执行的事务。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 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 = 0;
    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 = 0;
                    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;
}

 

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

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154885.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ce修改器怎么用 ce修改器使用基础教程[通俗易懂]

    ce修改器怎么用 ce修改器使用基础教程[通俗易懂]这篇文章是教大家CE修改器的使用方法,教程简单易学,有需要的小伙伴就赶紧和小编一起来学习一下吧。我们先下载并打开,下载地址:点击前往然后打开隐藏.隐藏CE修改器接着进入您玩的游戏这时我们进游戏后打开CE的最左上边的小电脑“文件”菜单-“打开进程”-打开MAIN进程(M开头有数字的)然后输入你当前的敏捷如:555(在HEX栏输入)接着我们点首次搜索.弄好后左边出现一大堆(RP好的只有一个,跳到9步)加几点敏捷,再输入你当前的敏捷如:558点再次搜索这次只有一个数据了,双击它,它会出现在下面

    2025年6月21日
    3
  • 汉语拼音发音教学视频_钢琴手把手教学视频

    汉语拼音发音教学视频_钢琴手把手教学视频pycharm汉化pycharm怎么改成汉语,手把手教学,超详细(汉语插件安装教程)首先,打开pycharm。然后点击左上角File(文件)会弹出如下页面继续点击蓝色位置Settings…(设置)会弹出一个页面如下:继续点击蓝色位置Plugins(插件)在搜索栏中输入chinese,如图然后安装第二个(可以滑动找一下),点击Install(安装),会加载一下下载进度条,然后变成这样:安装之后点击绿色按钮RestartIDE,会弹出点击蓝色按钮Restart,然后pycharm会重启,重启后

    2022年8月26日
    6
  • Java基础入门笔记07——泛型类

    Java基础入门笔记07——泛型类泛型用来限制集合的存入类型:指定一种。(保证类型安全)Java中的泛型只在编译过程中生效,编译结束就清除相关信息,泛型信息不会进入运行阶段泛型的使用1.泛型类package study1118;public class Test01 { public static void main(String[] args) { //指定泛型为String A<String> a1 = new A<>(); a1.setKe

    2022年8月8日
    8
  • 输入3个数a,b,c,要求输出最大值_二维数组求最大值及下标

    输入3个数a,b,c,要求输出最大值_二维数组求最大值及下标7-1 求最大值及其下标 (20分) 本题要求编写程序,找出给定的n个数中的最大值及其对应的最小下标(下标从0开始)。输入格式: 输入在第一行中给出一个正整数n(1<n≤10)。第二行输入n个整数,用空格分开。输出格式: 在一行中输出最大值及最大值的最小下标,中间用一个空格分开。 输入样例: 6 2 8 10 1 9 10 输出样例: 10 2#inc…

    2022年8月18日
    6
  • MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password: YES)无法打开的解决方法

    MySQL登录时出现Access denied for user ‘root‘@‘localhost‘ (using password: YES)无法打开的解决方法

    2021年10月2日
    61
  • Kafka零拷贝_kafka读取数据

    Kafka零拷贝_kafka读取数据Kafka除了具备消息队列MQ的特性和使用场景外,它还有一个重要用途,就是做存储层。用kafka做存储层,为什么呢?一大堆可以做数据存储的MySQL、MongoDB、HDFS……因为kafka数据是持久化磁盘的,还速度快;还可靠、支持分布式……啥!用了磁盘,还速度快!!!没错,kafka就是速度无敌,本文将探究kafka无敌性能背后的秘密。首先要有个概念,kafka高性能的背后…

    2022年9月21日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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