如何用ADO.NET实现嵌套SQL事务?

发布于 2024-08-28 05:54:19 字数 942 浏览 5 评论 0原文

我需要使用 ADO.NET 在 .NET 中实现嵌套事务。

情况如下:

--> Start Process (Begin Transaction)       

   --> Do DB things       

   --> Begin Transaction for step 1
   --> Step 1
   --> Commit transaction for step 1

   --> Begin transaction for step 2
   --> Step 2
   --> Rollback transaction for step 2

   --> etc ...

   --> Do DB things       


--> End Process(Commit or Rollback ALL commited steps --> a.k.a the process)

可以通过事务范围来完成吗?有人可以发布一个例子吗?

此外,我需要适用于 SQL Server 2005 和 Oracle 10g 数据库的流程...事务范围是否适用于这两种数据库引擎?

编辑:请注意,可能会发生这种情况:

Step1 已提交, Step2 回滚 Step3 已提交。

该流程已提交

(Step1 和 Step3 确实将数据存储到数据库中,step2没有)。

另一方面...

Step1 已提交, Step2 回滚 Step3 已提交。

该过程被回滚。

没有数据提交到数据库

注意:没有可用的数据库架构或域值

I need to implement nested transactions in .NET using ADO.NET.

The situation is as follows:

--> Start Process (Begin Transaction)       

   --> Do DB things       

   --> Begin Transaction for step 1
   --> Step 1
   --> Commit transaction for step 1

   --> Begin transaction for step 2
   --> Step 2
   --> Rollback transaction for step 2

   --> etc ...

   --> Do DB things       


--> End Process(Commit or Rollback ALL commited steps --> a.k.a the process)

Can that be done with transaction scopes? Could anyone post an example?

In addition I'd need the process to work for SQL Server 2005 AND Oracle 10g databases... will transaction scopes work with both database engines?

Edit: Note this situation might happen:

Step1 is commited,
Step2 is rolled back
Step3 is commited.

The process is commited

(Step1 and Step3 do store data into the database, step2 does not)

On the other hand...

Step1 is commited,
Step2 is rolled back
Step3 is commited.

The process is rolled back.

the NO DATA IS COMMITED to the database

Note: No DB schema or domain values available

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说不完的你爱 2024-09-04 05:54:19

您可以通过保存点(SQL Server 上的SAVE TRAN)在 TSQL 中执行此操作,但坦率地说,我不推荐这样做。您不能通过TransactionScope来完成此操作,因为任何中止都是终止的(一旦树中的任何事务指示失败,整个事务就会回滚)。

个人认为:先检查数据,只执行有效的操作。如果失败,那就是终结——回滚它。可能将工作分成可以独立真正提交(或回滚)的原子单元。

You could do that in TSQL via save points (SAVE TRAN on SQL Server), but frankly I don't recommend it. You can't do it via TransactionScope, as any abort is terminal (the entire transaction is rolled back as soon as any transaction in the tree indicates failure).

Personally: check the data first, and only perform valid actions. If it fails, that is terminal - roll it back. Possibly separate the work into atomic units that can be truly committed (or rolled back) in isolation.

腻橙味 2024-09-04 05:54:19

关于甲骨文:

BEGIN    
  SAVEPOINT STEP1;
  -- do some things
  IF your_criteria_for_commit_is_needed THEN
    NULL; -- do nothing
  ELSE
    ROLLBACK TO SAVEPOINT STEP1;
  END IF;

  SAVEPOINT STEP2;
  -- do some other things
  IF your_other_criteria_for_commit_is_needed THEN
    NULL; -- do nothing
  ELSE
    ROLLBACK TO SAVEPOINT STEP2;
  END IF;

  -- SOME NUMBER OF OTHER STEPS

  IF your_criteria_for_all_step_commit_is_needed THEN
    COMMIT; -- commit all changes to DB
  ELSE
    ROLLBACK; -- rollback all changes
  END IF;
END;
/

On Oracle:

BEGIN    
  SAVEPOINT STEP1;
  -- do some things
  IF your_criteria_for_commit_is_needed THEN
    NULL; -- do nothing
  ELSE
    ROLLBACK TO SAVEPOINT STEP1;
  END IF;

  SAVEPOINT STEP2;
  -- do some other things
  IF your_other_criteria_for_commit_is_needed THEN
    NULL; -- do nothing
  ELSE
    ROLLBACK TO SAVEPOINT STEP2;
  END IF;

  -- SOME NUMBER OF OTHER STEPS

  IF your_criteria_for_all_step_commit_is_needed THEN
    COMMIT; -- commit all changes to DB
  ELSE
    ROLLBACK; -- rollback all changes
  END IF;
END;
/
々眼睛长脚气 2024-09-04 05:54:19

如果您希望提交所有步骤或不提交任何步骤,那么一个事务不是更合适吗?您可以将现有事务对象传递给 ADO.Net Command 对象的构造函数,从而在单个事务范围内执行多个更新。

If you are want ALL steps to be committed or NONE then wouldn't one transaction be more appropriate? You can pass an existing transaction object to the constructor of an ADO.Net Command object and thus perform multiple updates within the scope of a single transaction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文