Castle.DynamicProxy.IInterceptor and Parallel transactions

143 views Asked by At

I'm Building a class to intercept transactions DAO methods consultations, but I have a problem. If I run the application, the page called many methods at the same time and I get this error message: SqlConnection does not support parallel transactions.

I noticed that at some point, a random transaction had not run the Commit method. In other research I found that the lack of call commits would be the cause of the problem. However, I would like to know how to make each call transaction can be treated as a single context and not generate this error. I'm using NHiernate.

The class I'm developing is:

public class ServiceTransactionInterceptor : Castle.DynamicProxy.IInterceptor
{
    private readonly ISession db;
    private ITransaction transaction = null;

    public ServiceTransactionInterceptor(ISession db)
    {
        this.db = db;
    }

    public void Intercept(IInvocation invocation)
    {
        bool iAmTheFirst = false;

        if (transaction == null)
        {
            transaction = db.BeginTransaction();
            iAmTheFirst = true;
        }

        try
        {
            invocation.Proceed();

            if (iAmTheFirst)
            {
                iAmTheFirst = false;

                transaction.Commit();
                transaction = null;
            }
        }
        catch (Exception ex)
        {
            if (iAmTheFirst)
            {
                iAmTheFirst = false;

                transaction.Rollback();
                db.Clear();
                transaction = null;
            }

            throw new ServicoException(ex);
        }
    }

}
0

There are 0 answers