Home > Software design >  This error occurs irregularly "Multiple simultaneous connections or connections with different
This error occurs irregularly "Multiple simultaneous connections or connections with different

Time:09-16

I'm using .Net Framework 4.5 and I get the following error:

"Multiple simultaneous connections or connections with different connection ~" Multiple simultaneous connections or connections with different connection strings inside the same transaction are not currently supported. "

What I'm curious about is that this error doesn't always occur, about 50 times? It means that an error occurs about once a day. I don't know what to do because it only occurs in the production environment and has never occurred in the local test. If you know the exact cause, please let me know.

Best regard.

The simplified source is below.

public PartialViewResult update(string id)
{
    List<Item> list;
    try
    {
        var business = new Business();
        using (var ts = new TransactionScope(TransactionScopeOption.Required))
        {
            business.Update(id);
            ts.Complete();
        }

        if (result)
        {
            list = business.GetList();
        }
        else
        {
            Response.StatusCode = 500;
            return PartialView("Error");
        }

        return PartialView("PartialContent", list);
    }
    catch (Exception ex)
    {
        Response.StatusCode = 500;
        return PartialView("Error");
    }
}

public class Business
{
    public List<Item> GetList()
    {
        var dao = new Repository();
        var list = dao.GetList().ToList();
        return list;
    }
    
    public void Update()
    {
        try
        {
            var dao = new Repository();
            var item = dao.Get(id);
            item.UpdateDate = DateTime.Now;
            dao.Update(item);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}


public class Repository
{
    private SampleContext context;

    private Repository()
    {
        if (context == null)
        {
            context = new SampleContext();
        }
    }
    
    public IQueryable<Item> GetList()
    {
        // SQL
    }
    public IQueryable<Item> Get(string id)
    {
        // SQL
    }
    public void Update(Item item)
    {
        context.Entry(item).State = EntityState.Modified;
        context.SaveChanges();
    }
}

Add.
I tried to modify Repository by comment of Le Vu.

public class Repository : IDisposable
{
    private SampleContext context;

    private Repository()
    {
        if (context == null)
        {
            context = new SampleContext();
        }
    }

    public IQueryable<Item> GetList()
    {
        // SQL
    }
    public IQueryable<Item> Get(string id)
    {
        // SQL
    }
    public void Update(Item item)
    {
        context.Entry(item).State = EntityState.Modified;
        context.SaveChanges();
    }
    
    public void Dispose()
    {
        context.Dispose();
    }
}

The reason for committing in the middle is that even if an error occurs in reacquiring the data after the update, we want to continue the update process as it is.

CodePudding user response:

The root cause is the unmanaged resources related to SampleContext(DbContext) have not been released especially database connections here.

The resolution is the instance of SampleContext need explicitly dispose. It means, we need to implement IDisposable interface in Repository class. And in Dispose, we will dispose the instance of SampleContext (if exist).

The lifetime of Business object on update Action is also a matter.

enter image description here

  • Related