Reputation: 75
Is there any method to close/dispose existing SQL Server connections when users session end in ASP.NET, because I get that error I also use Entity Framework in my application
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.
Upvotes: 0
Views: 1094
Reputation: 6590
This could be because the pool of SQL Connections is exhausted (this is different from the app pool.) You can check that by increasing the pool size through the connection string:
Integrated Security=SSPI;Initial Catalog=northwind;Max Pool Size=100;
Upvotes: 0
Reputation: 1063734
Firstly, forget about sessions. Your connections should not be tied to the session at all - if they are, there is a problem. If the issue is that your EF contexts are tied to the session: then again, I'd say you're doing it very wrong.
There are (IMO) two reasonable scopes for connections in a web app:
using
blocks.Upvotes: 1
Reputation: 43023
Instead of trying to keep the connection for a longer period of time and closing it at the end, you should open and close connections each time you need to do a job in SQL Server.
Connection pooling is efficient in modern databases and there is no need to try to keep connection open to long. This is especially true in situations like websites which won't scale well if you keep your connections open too long.
Upvotes: 0
Reputation: 157088
Use the Session_End in Global.asax
protected void Session_End(object sender, EventArgs e)
{
// End connection
}
Upvotes: 0