Asp.Net MVC UNitOfWork and MySQL and sleeping connections

I have an MVC web application based on the following architecture

Asp.Net MVC2, Ninject, Fluent NHibernate, MySQL, which uses a unit of work pattern.

Each connection to MySQL creates a sleep connection, which can be considered as an entry in the results of a SHOW PROCESSLIST query.

In the end, this will result in enough connections to retrieve the application pool limit and the web application to crash.

I suspect the connections are not located correctly.

If so, and how should this happen?

Here is a snapshot of the code I'm using:

public class UnitOfWork : IUnitOfWork
{
    private readonly ISessionFactory _sessionFactory;
    private readonly ITransaction _transaction;
    public ISession Session { get; private set; }

    public UnitOfWork(ISessionFactory sessionFactory)
    {
        _sessionFactory = sessionFactory;
        Session = _sessionFactory.OpenSession();
        Session.FlushMode = FlushMode.Auto;
        _transaction = Session.BeginTransaction(IsolationLevel.ReadCommitted);
    }

    public void Dispose()
    {
        if (Session != null)
        {
            if (Session.IsOpen)
            {
                Session.Close();
                Session = null;
            }
        }
    }

    public void Commit()
    {
        if (!_transaction.IsActive)
        {
            throw new InvalidOperationException("No active transation");
        }
        _transaction.Commit();
        Dispose();
    }

    public void Rollback()
    {
        if (_transaction.IsActive)
        {
            _transaction.Rollback();
        }
    }
}




public interface IUnitOfWork : IDisposable
{
    void Commit();
    void Rollback();
}




public class DataService
{
    int WebsiteId = Convert.ToInt32(ConfigurationManager.AppSettings["Id"]);

    private readonly IKeyedRepository<int, Page> pageRepository;
    private readonly IUnitOfWork unitOfWork;

    public PageService Pages { get; private set; }


    public DataService(IKeyedRepository<int, Page> pageRepository,
        IUnitOfWork unitOfWork)
    {
        this.pageRepository = pageRepository;
        this.unitOfWork = unitOfWork;

        Pages = new PageService(pageRepository);

    }

    public void Commit()
    {
        unitOfWork.Commit();
    }

}


public class PageService
{
    private readonly IKeyedRepository<int, Page> _pageRepository;
    private readonly PageValidator _pageValidation;

    public PageService(IKeyedRepository<int, Page> pageRepository)
    {
        _pageRepository = pageRepository;
        _pageValidation = new PageValidator(pageRepository);
    }

    public IList<Page> All()
    {
        return _pageRepository.All().ToList();
    }

    public Page FindBy(int id)
    {
        return _pageRepository.FindBy(id);
    }
}
+5
source share
3

, UoW .

. , .

InRequestScope , GC HttpContext. Ninject Mailing List, HttpApplication. Ninject.

+3

Ninject , IDisposable Dispose d.

Ninject

I would also suggest looking around here, it came up with different storage mechanisms and different containers - most importantly - you need to take control and find out when you connect to the UOW commit / rollback / dispose semantics and not leave it by accident or coincidence (although the Convention is great).

0
source

All Articles