Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection leak in Entity Framework

For the past two days I've been trying to resolve the following error:

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.

As far as I have checked all of our DbContext requests are wrapped with using, and still it looks like it doesn't get disposed or something else.

Is there a way to determine if the web app is leaking connection? Ir something else I can try?!

I have tried to increase timeouts in SQL Server + increment the pool size from default value of 100 to 200 as a temporary result but it didn't helped, and that's not the solutions I want.

Any suggestions would help.

like image 969
Biryukov Pavel Avatar asked Oct 31 '22 08:10

Biryukov Pavel


1 Answers

I think I have a case similar to yours. Our old project version (still using EF4) leaks connections when my code does NOT touch the context. The context is created, a new connection allocated, but on Dispose() the context does not seem to return its connection.

If I just perform one little query (.First() on any random table) on that underutilized context, the situation improves.

A more recent branch of my project uses EF6. That code works fine.

What helped me track this down was SQL Profiler. I also knew roughly what my problem user was doing, so I repeated his steps and kept an eye on Profiler's SPID column. Then I stepped through parts of the code until I got one of the ghost connections I was chasing.

(I stumbled across this just now, so I have not had time to investigate further)

like image 122
9Rune5 Avatar answered Nov 15 '22 06:11

9Rune5