Remove a SPID after LINQ in Entity Framework

326 Views Asked by At

I have an application where I use Entity Framework to interact with a MS SQL database. In it I make calls to the Dbcontext inside using blocks. The problem that I'm having is that, even after the Dbcontext has been disposed of, the SPID sits there with a status of sleeping until the application exits. How would I remove these hanging SPIDs?

1

There are 1 best solutions below

0
Robert McKee On

You probably don't want to, but this should work:

((SqlConnection)dbContext.Database.Connection).ClearAllPools();

As mentioned above, this can and will negatively impact the performance of your application (and perhaps other applications). Perhaps the real question might be, why do you want to remove the SPID?

You could also add Pooling=false to the connection string which should prevent the application from using the Connection Pool at all. Depending on your application, this may or may not affect performance greatly.