I'm receiving an error in a Net Core 3.1 application using Entity Framework I hope I can get help with:
"Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached."
The site works fine locally but when hosted on Azure with multiple users are on I get the above error so it seems like a performance issue. It happens even with a relatively low number of users on(15). Usually when there's about 70 requests within a minute.
I've set the Max Pool Size higher(up to 3000) which fixes the server crash but introduces 400 errors periodically throughout the application. Based on other answers this is not really a fix but a stopgap solution anyway.
Here's a pic of the open connections when idle after a few days:

I use lazy loading in my database but it is using EF Core Dependency Injection so that should open/close connections automatically no?
How can I debug which section of the code is causing issues in this case?
Here's an example of how we use dependency injection:
[Authorize(Policy = Role.Administrator)]
[Route("api/[controller]")]
[ApiController]
public class LessonsController : ControllerBase
{
private readonly ApplicationDbContext _context;
public LessonsController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet]
public IActionResult Get()
{
var lessons = _context.Lesson.Select(l => new LessonViewModel
{
Id = l.Id,
Name = l.Name,
Order = l.Order
});
return Ok(new { data = lessons });
}
[HttpDelete("{id}")]
public async Task DeleteAsync(int id)
{
var lesson = await _context.Lesson.FindAsync(id);
_context.Lesson.Remove(lesson);
await _context.SaveChangesAsync();
}
}
Here's where I add the db context in Startup/ConfigureServices:
services.AddDbContext<ApplicationDbContext>(options =>
{
options
.UseLazyLoadingProxies()
.UseSqlServer(Configuration.GetConnectionString("DBContextConnectionString"));
});