26156

Does ObjectContext.Connection.BeginTransaction() use MSDTC?

Question:

I want confirm if the Transaction that Entity Framework's ObjectContext.Connection.BeginTransaction() method returns uses the support of MSDTC (Microsoft Distributed Transaction Coordinator) or not?

Is there any way to use transactions without support of MSDTC?

Answer1:

It will automatically promote to a transaction coordinated by MSDTC under certain conditions. Each time you call ObjectContext.SaveChanges() a new transaction is created if there isn't already one in scope (if there is already an active transaction in scope, it will enlist in that transaction). However, by default, the connection will also be opened and closed each time you call ObjectContext.SaveChanges(). So if you're calling ObjectContext.Connection.BeginTransaction() at the beginning of a method, then calling ObjectContext.SaveChanges() multiple times while holding onto the original transaction, with some versions of SQL Server and Entity Framework, this can cause the transaction to get promoted to MSDTC because it's now using different connections within a single transaction. If you're trying to avoid your transaction getting promoted to MSDTC, then explicitly open your connection at the beginning and close it when you're done:

using(MyEntities context = new MyEntities()) using(DbConnection conn = context.Connection) { conn.Open(); DbTransaction transaction = conn.BeginTransaction(); // now do stuff within the transaction scope transaction.Commit(); }

However, it's recommended that you use a TransactionScope, as it's more flexible, less platform-dependent, and will make it easier on you if in the future you decide you do actually need to something that requires MSDTC. EntityFramework will automatically enlist in the transaction if there's an active TransactionScope:

using(TransactionScope transaction = new TransactionScope()) using(MyEntities context = new MyEntities()) using(DbConnection conn = context.Connection) { conn.Open(); // now do stuff within the transaction scope transaction.Complete(); }

Recommend

  • How to Clone A UIImageView
  • Implementation of Atomic Transactions in dynamodb
  • Open MPI to distributed and manipulate 2d array in PGM files
  • Rebinding all properties of all elements in the visual tree?
  • ViewModel properties stay null on HttpPost
  • My “Create” View in my “Index” View, is it possible?
  • Adding object to database using table-per-type inheritance in Mvc 4
  • Changing Entity's State in Entity Framework 4.1
  • Grails Cannot get property 'id' on null object
  • How can I sum two different columns at once where one contains Decimal objects in pandas?
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • How to update powerpivot pivot table filter via cell reference?
  • VBScript to check for open process by user
  • Issue with SVN Commit for certain File Extension
  • Unable to run SDL program in Eclipse but able to do so in Windows Explorer
  • How to sort a same column both in asc order and desc order
  • MySQL multiple IN conditions to subquery with same table
  • mave 3.2 not able to access local nexus instance return 502 code
  • Using $compile in a directive triggers AngularJS infinite digest error
  • Updating Dojo provide
  • Find group of records that match multiple values
  • C# fibonacci function returning errors
  • Not able to aggregate on nested fields in elasticsearch
  • Should I or shouldn't I use the CachingConnectionFactory with hornetq 2.4.1
  • How to write order and limit within cakephp joins array
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • Is it possible to access block's scope in method?
  • What is the “return” in scheme?
  • Incrementing object id automatically JS constructor (static method and variable)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • bootstrap to use multiple ng-app
  • Free memory of cv::Mat loaded using FileStorage API
  • Change div Background jquery
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How does Linux kernel interrupt the application?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Busy indicator not showing up in wpf window [duplicate]
  • Why do underscore prefixed variables exist?