交易范围功能

发布于 2024-12-12 04:03:00 字数 397 浏览 1 评论 0原文

我使用下面的代码来应用事务范围,

TransactionOptions transOption = new TransactionOptions();
transOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transOption))
{
  //Code to delete    
  //Code to insert

  scope.Complete();
}

该范围不起作用,如果我在插入中出现错误,记录将被删除而不是回滚

I am using the below code to apply the transaction scope

TransactionOptions transOption = new TransactionOptions();
transOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transOption))
{
  //Code to delete    
  //Code to insert

  scope.Complete();
}

the scope is not working where if I have an error in the insert the record is deleted not rolled back

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

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

发布评论

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

评论(2

缱绻入梦 2024-12-19 04:03:00

您需要告诉我们您正在使用哪个数据库服务器。如果您收到任何异常消息,或者

代码似乎正确,您是否检查了(MSDTC)?检查它是否在您的操作系统上启用。

you need to tell us which database server you are using. and if you are getting any exception messages or not

anyway the code seems correct, did you check the (MSDTC)? check if it's enabled on your OS.

一紙繁鸢 2024-12-19 04:03:00

http://msdn.microsoft.com/en-us /library/system.transactions.transactionscope.complete.aspx 状态:

TransactionScope.Complete() 表示范围内的所有操作均已成功完成。

因此,除非您的“要插入的代码”抛出终止使用块的异常,否则您的代码始终调用scope.Complete()并提交数据库更改。

如果您的“要删除的代码”和“要插入的代码”正在内部处理异常,那么它们需要将信息返回到您的使用块以指示一切是否正常。例如,假设您的“代码”块是方法调用,如果一切正常则返回 true,如果失败则返回 false,您可以编写:

bool allGood;

allGood = CodeToDelete();

if(allGood)
{
  allGood = CodeToInsert();
}

if(allGood)
{
  scope.Complete();
}

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.complete.aspx states:

TransactionScope.Complete() Indicates that all operations within the scope are completed successfully.

So unless your "Code to Insert" is throwing an exception which terminates the Using block, your code is always calling scope.Complete() and committing your database changes.

If your "Code to Delete" and "Code to Insert" are handling exceptions internally, then they need to return info to your Using block to indicate whether or not everthing worked. For example, assuming your "code" blocks are method calls that return true if all good or false if failed, you can write:

bool allGood;

allGood = CodeToDelete();

if(allGood)
{
  allGood = CodeToInsert();
}

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