Monday, January 15, 2007
Atomic Saves and Transactions
One of our clients was having a problem with atomic saves and transactions in Milos, and it was kind of tricky to figure it out.
Business Objects were sharing the same data context so that the entities would get saved wrapped up on the same transaction. One of the Business Objects has a Business Rule that instantiates another Business Object and calls a method on it to check if a given row exists already on the database. In this scenario, the row was being added on the same transaction, but the call to this look up method was timing out (indicating that it couldn't read the row just added on the transaction. The code looked like this:
public override void VerifyRow(DataRow currentRow, int rowIndex)
{
if (!currentRow.IsNull("fkTrade"))
{
using (TradeBusinessObject oBizObj = new TradeBusinessObject())
{
Guid fkTrade = (Guid)currentRow["fkTrade"];
string tradeName = currentRow["TradeName"].ToString();
DataSet ds = oBizObj.GetList(fkTrade);
if (ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
{
this.LogBusinessRuleViolation(currentRow, rowIndex, "fkTrade", "The Trade '" + tradeName + "' does not exist.");
}
}
}
}
Notice that the TradeBusinessObject has its own data context, and therefore, the call to GetList can't seem any row created by the transaction shared by main business object where the business rule lives in. All that needs to be done in this case is to make the object share the same data context, like so (the following line must come after the object is instantiated, but before the GetList method is called on it):
oBizObj.ShareDataContext((BusinessObject)this.BusinessObject);
Posted @ 4:52 PM by Lassala, Claudio (lassala@foxbrasil.com.br)
Post a Comment:
Comment Title (required):
Your Name (optional):
Your Email (optional):
Your Web Site (optional):
Your Comment (required):