Having an issue with DTC...

Having an issue with DTC...

Old forum URL: forums.lhotka.net/forums/t/7624.aspx


jkellywilkerson posted on Monday, September 14, 2009

Greetings,

Currently I have a need to create a new Rate businessbase BO when a new Charge businessbase BO has been saved. The rates can fall into different tiers; Tier1 is member only, Tier2 is member and member + family, Tier3 is member, member + 1, and member + family, etc.

I am trying to call:
Rate rate = Rate.NewRate(...stuff...);
rate.RateType = RateTypeEnums.RateType.Member;
rate.Amount = 0.00m;
rate.Save();
from within the DataPortal_Insert method after the cmd.ExecuteNonQuery() for the Charge BO insert. This causes a DTC error. I have moved the code to the UI after the save of the Charge object and it works correctly, but is that the correct way of doing things? Should the Charge object also create its associated Rates? Or is creating the Rates in the UI acceptable?

Thanks in advance for any input,

Kelly.

RockfordLhotka replied on Tuesday, September 15, 2009

If you are using TransactionScope and you open more than one connection to the database within the scope of that TransactionScope, then System.Transactions will automatically use the DTC to coordinate the transaction.

That is a well known "feature" of TransactionScope - and one many people suffer from, whether they use CSLA or not.

The obvious solution is to make sure you only use one connection for all data access within a given TransactionScope. That's harder than it looks, especially when dealing with multiple root objects, but it is hard even with child objects (because you must manually pass the open connection as a parameter, etc).

So current versions of CSLA (3.5+?) include Csla.Data.ConnectionManager (and other similar types for LINQ to SQL, EF, etc) to make it very easy - essentially automatic - to reuse the open connection. Take a look at chapters 17-18 of the Expert 2008 Business Objects book to see how this is used. Or look at the current ProjectTracker code - which uses ContextManager for L2S, but ConnectionManager works in much the same way.

In short: you should be able to do what you are doing, you just need to handle the database connection properly.

jkellywilkerson replied on Tuesday, September 15, 2009

Thanks Rocky for taking the time to respond to my issue, and others here for that matter. I do have the book and will give those chapters a look. I have been in the middle of a rather long running project and have been trying to follow (rather loosely) the transition from 2.0.3 to 3.7.0 now. So I have a mixture of the "CSLA styles" in this project. Is the ConnectionManager new to 3.x or did I just miss something from 2.x versions.

Thanks again, Kelly.

RockfordLhotka replied on Tuesday, September 15, 2009

ConnectionManager was introduced in 3.5 or 3.6.

Copyright (c) Marimer LLC