Essentials and Load Testing

I find it hard to understand why this code fails

I have a testing method

IUnitOfWork unitofwork = EFUnitOfWork.CreateInstance(); IRepository<InformationRequest> informationRequestRepository = unitofwork.CreateRepository<InformationRequest>(); IEnumerable<InformationRequest> requests = informationRequestRepository.ToList(); unitofwork.Dispose(); EFUnityOfWork.CreateInstance calls the EFUnitOfwork Constructor public EFUnitOfWork() { _currentContext = new MyDataContext(); } 

Here is the code for CreateRepository

 public IRepository<T> CreateRepository<T>() { return new Repository<T>(_currentContext); } 

The test above does not work when testing a load. When I try to run it, it says: Fix System.Data.EntityException: the underlying provider refused to open. ---> System.InvalidOperationException: The connection was not closed. The status of the current connection is connected.

I get rid of the context and create a new one every time. I do not understand where I am mistaken.

+4
source share
1 answer

Your EFUnitOfWork.CreateInstance() code is a static method.

When 2 threads call this at the same time, they can return the same context. You may then receive an error message.

You can fix this by blocking so that it gets called only one thread at a time. But time will create a bottleneck in productivity.

+2
source

All Articles