CommandBase transaction for saving not related BO's in shared transactionCommandBase transaction for saving not related BO's in shared transaction
Old forum URL: forums.lhotka.net/forums/t/3134.aspx
cultofluna posted on Tuesday, July 03, 2007
Based on the previous posts:
http://forums.lhotka.net/forums/thread/15688.aspx
http://forums.lhotka.net/forums/thread/15826.aspx
http://forums.lhotka.net/forums/thread/3062.aspx
http://forums.lhotka.net/forums/thread/5047.aspx
comments please on the following solution :)
1. CommandBase DataPortal_Execute for setting the Shared Connection and Transaction
protected override void DataPortal_Execute()
{
using (SqlConnection cn = new SqlConnection(Database.DataConnection))
{
cn.Open();
ApplicationContext.LocalContext["SharedConnection"] = cn;
using (SqlTransaction tr = cn.BeginTransaction())
{
ApplicationContext.LocalContext["SharedTransaction"] = tr;
try
{
_inslag.Status = WmsMutationStatusses.Afgedrukt;
_inslag.Save();
_locatie.IntakeId = (int)_inslag.SysId;
_locatie.Save();
tr.Commit();
}
catch
{
tr.Rollback();
throw;
}
finally
{
if (ApplicationContext.LocalContext.Contains("SharedConnection"))
ApplicationContext.LocalContext.Remove("SharedConnection");
if (ApplicationContext.LocalContext.Contains("SharedTransaction"))
ApplicationContext.LocalContext.Remove("SharedTransaction");
}
}
}
}
2. BusinessBase DataPortal_Update using Shared if available else create new
protected override void DataPortal_Update()
{
//***** Bepaal of er sprake is van een gedeelde connectie en transactie.
//***** Is dit het geval, dan overnemen, anders nieuw starten.
if (ApplicationContext.LocalContext.Contains("SharedConnection") && ApplicationContext.LocalContext.Contains("SharedTransaction"))
{
SqlConnection cn = (SqlConnection)ApplicationContext.LocalContext["SharedConnection"];
SqlTransaction tr = (SqlTransaction)ApplicationContext.LocalContext["SharedTransaction"];
try
{
if (base.IsDirty)
{
ExecuteUpdate(tr);
}
//update child object(s)
UpdateChildren(tr);
}
catch
{
throw;
}
}
else
{
SqlTransaction tr;
using (SqlConnection cn = new SqlConnection(Database.DataConnection))
{
cn.Open();
tr = cn.BeginTransaction();
try
{
if (base.IsDirty)
{
ExecuteUpdate(tr);
}
//update child object(s)
UpdateChildren(tr);
tr.Commit();
}
catch
{
tr.Rollback();
throw;
}
}//using
}
}
Copyright (c) Marimer LLC