Вопрос

I got a serviced component which looks something like this (not written by me):

[Transaction(TransactionOption.Required, Isolation = TransactionIsolationLevel.Serializable, Timeout = 120), EventTrackingEnabled(true)]
public class SomeComponent : ServicedComponent
{
    public void DoSomething()
    {
        try
        {
            //some db operation
        }
        catch (Exception err)
        {
            ContextUtil.SetAbort();
            throw;
        }
}

Is the ContextUtil.SetAbort() really required? Won't the exception abort the transaction when the component is left?

Это было полезно?

Решение

Only if you want to manage the transaction manually.

Your component will vote automatically to abort (in case any exception is raised), or commit, if you decorate your operation with the [AutoComplete] attribute in this way:

[AutoComplete]
public void DoSomething()

EDIT:

For more info about this attribute, see MSDN here:

The transaction automatically calls SetComplete if the method call returns normally. If the method call throws an exception, the transaction is aborted.

Anyway if you are (in the rare case) that really need to manage the transaction manually, is really important that you don't leave your transactions in doubt. I'm missing in your code the ContextUtil.SetComplete(); that should be explicitly called.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top