“User Initiated Transaction” 是什么?

huangapple go评论57阅读模式
英文:

What is a "User Initiated Transaction"?

问题

有许多网络解决方案可以使用 Entity Framework 并解决此错误:

“执行策略 'SqlServerRetryingExecutionStrategy' 不支持用户发起的事务”

我遇到了这个问题,并按照互联网上推荐的修复方法解决了我正在执行的那个 SQL 事务中的问题。问题是,我需要找到我的应用中可能引起相同问题的其他地方并修复它们。但当我不知道什么是“用户发起的事务”时,我该怎么做呢?我已经在互联网上搜索了很多地方,包括微软在此问题上的知识文章此处,但它们没有定义这个术语。

我“认为”这是当您使用 transaction scopeusing BeginTransaction() 时发生的,但我宁愿不要猜测。

请有人定义一下这个术语吗?

英文:

There are many solutions on the web for using Entity Framework and getting this error:

> Execution strategy 'SqlServerRetryingExecutionStrategy' does not
> support user-initiated transactions

I ran into this issue and followed the recommended fixes on the internet and I no longer have the problem in that one sql transaction I was doing. The problem is, I need to find other areas of my app that could cause the same issue and fix them. How can I do this when I don't know what a "User initiated transaction" is. I've looked all over the web including Microsoft's knowlege article on this issue HERE but they don't define this term.

I "think" it is when you are using a transaction scope, or a using BeginTransaction() but I would rather not guess.

Can someone define this term please?

答案1

得分: 1

用户启动的事务是一个使用SqlTransaction启动的数据库事务,您或一些代码启动的事务。例如:

using (SqlConnection objConn = new SqlConnection(strConnString))    
{    
   objConn.Open();    
   objTrans = objConn.BeginTransaction();    
   SqlCommand objCmd1 = new SqlCommand("insert into tblProject values(1, 'TestProject')", objConn);    
   SqlCommand objCmd2 = new SqlCommand("insert into tblProjectMember(MemberID, ProjectID) values(2, 1)", objConn);    
   try    
   {    
      objCmd1.ExecuteNonQuery();    
      objCmd2.ExecuteNonQuery(); // 由于外键约束而引发异常   
      objTrans.Commit();    
   }    
   catch (Exception)    
   {    
      objTrans.Rollback();    
   }    
   finally    
   {    
      objConn.Close();    
   }    
}   
英文:

A user initiated transaction is a database transaction you or some code started using a SqlTransaction. For example:

using (SqlConnection objConn = new SqlConnection(strConnString))    
{    
   objConn.Open();    
   objTrans = objConn.BeginTransaction();    
   SqlCommand objCmd1 = new SqlCommand("insert into tblProject values(1, 'TestProject')", objConn);    
   SqlCommand objCmd2 = new SqlCommand("insert into tblProjectMember(MemberID, ProjectID) values(2, 1)", objConn);    
   try    
   {    
      objCmd1.ExecuteNonQuery();    
      objCmd2.ExecuteNonQuery(); // Throws exception due to foreign key constraint   
      objTrans.Commit();    
   }    
   catch (Exception)    
   {    
      objTrans.Rollback();    
   }    
   finally    
   {    
      objConn.Close();    
   }    
}   

huangapple
  • 本文由 发表于 2023年2月10日 04:07:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定