kevin
kevin

Reputation: 14065

TransactionScope related practice

Can I do any other things (not related to DataBase) in the TransactionScope ?

Will it be a bad practice ?

e.g

Starting a workflow inside the scope.

I don't want to save in DB unless starting the workflow fails.

If it's a bad practice, what would be a good practice?

Thanks in advance.

using (TransactionScope scope = new TransactionScope())
{
    using (ComponentCM.Audit auditComponent = new ComponentCM.Audit())
    {
        using (AccessCM.Biz dataAccess = new AccessCM.Biz())
        {
            auditComponent.SaveInDB();
            dataAccess.SaveinDB()

            StartWorkflow();
        }
    }
    scope.Complete();
}

Upvotes: 0

Views: 263

Answers (2)

Davin Tryon
Davin Tryon

Reputation: 67296

I agree with Oded.

I'd also add that the scope of the TransactionScope should define the state changes that are defined in your unit of work. In other words, state changes inside the scope should all succeed or fail together and are usually part of the same logical operation.

In order to keep the scope of the as small as possible, one common pattern is optimistic concurrency. When using optimistic concurrency, you typically only open a transaction scope when performing a write operation on the state store. In this case, you are excepting that usually the data you are working on will not have changed (by another operation) between the time that you query and commit.

Upvotes: 1

Oded
Oded

Reputation: 498934

You want transactions to take as little time as possible, as they can cause locking on the database.

In this regard, it is better to do other work outside of the transaction scope.

Upvotes: 3

Related Questions