views:

21

answers:

1

So hopefully with some CSLA skills out there can help me see a better way with this one. I have a situation where I want to manually create my transaction from outside the object and set my transactionAttribute to manual.

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?

A: 

Ok so there were a lot of things I needed to change with this. But one of the glaring problems with this situation is that I have the connection being used in the using statement. Which as soon as it exits out it is being disposed of. Since this transaction goes through several of these methods like this it turned out my connection and transaction were being disposed of after the first call.

I just had one of those duh moments right now and I realized it. I hate how sometimes the answer can be so obvious.

spinon