I have a situation where I want to manually create my transaction from outside the object and set my transactionAttribute to manual. I tried posting this on StackOverflow but no one had any suggestions so hopefully someone in here can help. Didn't think to start here first for some reason.
So I have some code that looks like this:
using (SqlConnection conn ConnectionManager<SqlConnection>.GetManager("Db").Connection)
{
connectionTransaction = conn.BeginTransaction();
objectRef = objectRef.Save();
//other logic here
objectRef = objectRef.Save();
connectionTransaction.Commit();
}
Then inside the save method there is something like this for the data access:
using (var conn = ConnectionManager<SqlConnection>.GetManager("Db").Connection)
{
using (var cm = conn.CreateCommand())
{
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "Proc_Name";
// param definitions
cm.ExecuteNonQuery();
}
}
When I do this I receive the following error:
ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized.
Ok so that makes sense what it is saying and I could easily fix by assigning the transaction to the command. But I'm wondering if this is actually structured correctly according to CSLA best practices. I can't seem to find a good example in the book I recently got.
I just can't imagine that it is good practice to tell my domain object code to behave like it is in a transaction when there could be times when it isn't.
Can anyone show me the light on what I need to do to fix this correctly?
What does "manually create my transaction from outside the object" mean?
Have you read Chapters 1-5 and 18 of the Expert 2008 Business Objects book? There are four data access models - I list them here: http://forums.lhotka.net/forums/p/8864/42789.aspx#42789 and in the data access FAQ.
You can't start a transaction "above" the data portal - you have to wait until the data portal has invoked your DataPortal_XYZ or object factory method for your root object. There's no place prior to that point where you can start a transaction.
The Save() method is client-side code - so it absolutely must not talk to any database or use any database type code (like using ADO.NET).
Copyright (c) Marimer LLC