在ADO.NET嵌套事务 [英] Nested Transactions in ADO.NET

查看:104
本文介绍了在ADO.NET嵌套事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,是有可能有n个事务级别上ADO.Net。其次,这是正确的用法?

First, is it possible to have n transactions levels over ADO.Net. Second, is this correct usage?

        var tx = cx.BeginTransaction();

        cx.Execute("insert into atable(id) values(123123)");

        var tx2=tx.BeginTransaction();

        cx.Execute("insert into atable(id) values(123127)");

        tx2.Commit();

        tx.Commit();

... 等等。

... etc.

推荐答案

您可以使用嵌套事务<一href="http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.transactionscope.aspx"相对=nofollow> 的TransactionScope - 但是,他们将只能得到承诺曾经最外层被提交

You can nest transactions using TransactionScope - however, they will only get committed once the most outer one gets committed.

他们都将回滚,如果他们中的任何一个将回退。

They will all be rolled back if any one of them will rollback.

在使用方面 - 你应该使用语句,以确保妥善处置将事务创作

In terms of usage - you should wrap the transaction creation in using statements to ensure proper disposal.

using(var tx1 = new TransactionScope())
{
   cx.Execute("insert into atable(id) values(123123)");

   using(var tx2 = new TransactionScope())
   {
        cx.Execute("insert into atable(id) values(123127)");
        tx2.Complete();
   }

   tx1.Complete()
}

这篇关于在ADO.NET嵌套事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆