Isolated ADO.NET connection and transaction within a transactionscope

♀尐吖头ヾ 提交于 2019-12-25 04:44:18

问题


Background: I have an ASP.NET MVC application that wraps controller actions in a TransactionScope using an Action Filter.

Further downstream (in my infrastructure layer); I'd like to open up a new ADO.NET connection (to the same db as my outer TransactionScope, if that is relevent) and have it participate in its own transaction - without being tied to the current TransactionScope started at the controller level; how do I go about doing this?


回答1:


You pass in TransactionScopeOption.RequiresNew:

TransactionOptions to = new TransactionOptions;
to.IsolationLevel = <desiredIsolationLevel>;
using (TransactionScope scope = new TransactionScope(
   TransactionScopeOption.RequiresNew, to))
{
  ... do work here
  scope.Complete ();
}

Be carefull though, there be dragons under that bridge. Specifically is easy to deadlock with yourself if the encompassing scope has pending uncommitted writes from its own transaction scope and you attempt to touch the very same entities in the inner standalone transaction (the new scope).



来源:https://stackoverflow.com/questions/3790430/isolated-ado-net-connection-and-transaction-within-a-transactionscope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!