如何用ADO.NET实现嵌套SQL事务?
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过保存点(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 viaTransactionScope
, 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.
关于甲骨文:
On Oracle:
如果您希望提交所有步骤或不提交任何步骤,那么一个事务不是更合适吗?您可以将现有事务对象传递给 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.