none
GenericRepository DbContext multi threading issue RRS feed

  • Question

  • Hi folks,

    we are getting database connection is open issue when we try load test for  concurrent users. We have WebAPI hosted in app server and using EF as ORM. We have created Generic Repository pattern and Unity as Ioc. Below is my call stack

    public class MyRepositoryBase :  IMyRepositoryBase 
       // where TContext : DbContext
        {
            protected readonly DbContext _dataContext;
    
            public MyRepositoryBase(DbContext  dataContext)
            {
                this._dataContext = dataContext;
            }
    
      public virtual IEnumerable<TEntity> Get<TEntity>(
                Expression<Func<TEntity, bool>> filter = null,
                Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
                string includeProperties = null,
                int? skip = null,
                int? take = null)
                where TEntity : class
            {
                return GetQueryable<TEntity>(filter, orderBy, includeProperties, skip, take).ToList();
            }
    ......
    ....
    }
    
    //UnitWork.cs
     public class UnitOfWork :Disposable,IUnitOfWork
        {
            private readonly DbContext _dataContext;
    
            public virtual int Commit()
            {
                return _dataContext.SaveChanges();
            }
    	}
    
    //Repository Class
    
      public class CountryRepository : MyRepositoryBase, ICountryRepository 
        {
    
            public CountryRepository(MyDBEntities dbContext)
                : base(dbContext)
            {
                _dbContext = dbContext;
            }
    public string GetCountry(int employeeId)
       {
         var res = Get<country>().Select(p => new {p.TextKey, p.TextValue}).ToList();
       }
    
         }
    
    //Service.cs
        public class CountryQueryService : ICountryQueryService
        {
            public CountryQueryService(ICountryRepository countryRepository, IUnitOfWork unitOfWork)
            {
                _countryRepository = feedbackRepository;
                _unitOfWork = unitOfWork;
            }
       }	
    
    //Unity.cs
    container.RegisterType<DbContext, MyDBEntities>("MYE", new PerResolveLifetimeManager());

    When i try load test with concurrent users im getting issues like underlyingconnection cannot open or database is opened. If i dont us eteh GenericRepository and use using in my repositoey class it works fine but i need to us eteh Generic Repository pattern. How can i us eteh dbcontext with multiple threading

     using (var _dbContext = new MyDBEntities())
     {

    }

    Request your help on this and really appreciate your help.


    Sunday, September 17, 2017 2:07 PM

All replies

  • When i try load test with concurrent users im getting issues like underlyingconnection cannot open or database is opened. If i dont us eteh GenericRepository and use using in my repositoey class it works fine but i need to us eteh Generic Repository pattern. How can i us eteh dbcontext with multiple threading

    If you are having connection issues, then connections are not being closed properly and/or connection pooling is not happening properly.

    I am not a fan of the generic repository object as a repository object may need to be used for specific business needs beyond being generic. So, I use individual repository objects or have seen individual repositories object  that made the calls to the DAL using the DAO pattern for CRUD using EF and not using EF.

    In the example of generic repository being used in WebAPI with EF, EF DB First and DBset are being used along with Dispose() is being used in the repository so when the repository goes out of scope, it effectively is destroying any objects within the repository, and the connection object is destroyed with the destruction of the connection object closing an open connection. 

    http://www.c-sharpcorner.com/blogs/generic-repository-pattern-with-webapi-and-entity-framework

    Maybe, there should be no Unity used for DBconnect.

    FYI, I consider Code first to be applicable for simple usage and not complex usage or against complex DB schemas.  

    The EF entities shouldn't come past the WebAPI, and you should consider using the DTO pattern.

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    https://entitiestodtos.codeplex.com/

    Sunday, September 17, 2017 7:22 PM