9

DTC is disabled on my machine. It is my understanding that this code should fail, because it uses two data contexts in the same transaction. So, why does it work? (Note: I tried this using .NET 3.5 and .NET 4.0.)

using (TransactionScope transactionScope = new TransactionScope())
{
    UpdateEta();
    UpdateBin();

    transactionScope.Complete();
}

Here are the DAL methods that get called:

public static void UpdateBin(Bin updatedBin)
{
    using (DevProdDataDataContext dataContext = new DevProdDataDataContext(ConnectionString))
    {
        BinRecord binRecord = (from bin in dataContext.BinRecords
                               where bin.BinID == updatedBin.BinId
                               select bin).FirstOrDefault();

        binRecord.BinID   = updatedBin.BinId;
        binRecord.BinName = updatedBin.BinName;

        dataContext.SubmitChanges();
    }
}  

public static void UpdateEta(Eta updatedEta)
{
    using (DevProdDataDataContext dataContext = new DevProdDataDataContext(ConnectionString))
    {
        EtaRecord etaRecord = (from eta in dataContext.EtaRecords
                               where eta.ID == updatedEta.ID
                               select eta).FirstOrDefault();

        etaRecord.ID    = updatedEta.ID;
        etaRecord.Title = updatedEta.Title;

        dataContext.SubmitChanges();
    }
}
Bob Horn
  • 33,387
  • 34
  • 113
  • 219

3 Answers3

6

Are the connection strings different between the two? If not, it might be that they both reuse the same connection out of the same underlying connection pool, eliminating the need to promote to DTC?

CodingWithSpike
  • 42,906
  • 18
  • 101
  • 138
  • 1
    SQL 2008 _will_ resuse the connection if the strings are the same. I don't believe 2005 does and would promote the transaction... – ShaneBlake May 06 '11 at 17:12
  • I think that's it, guys. I am indeed using the same connection string, and I am indeed using SQL Server 2008. However, I can't rely on this behavior, because if the underlying connection becomes unavailable (from the connection pool), then maybe we'd have the DTC problem. Thanks! – Bob Horn May 06 '11 at 17:21
1

I'm not convinced you are using two different contexts from your post.
Besides it was my understanding that if the database connections were to the same database on the same machine then there would be no need to escalate to a DTC. That escalation occurs when two different database servers are used in the transaction.

Pete Stensønes
  • 5,595
  • 2
  • 39
  • 62
0

You database connections are not nested. They are sequentially used. Stick one procedure inside the other and try again.

Edward
  • 1