Saturday, August 07, 2010

LINQtoSQL SQL Open Connection Issue

I have seems iDataReader, not properly closed, hogging up SQL Connection pool and not releasing the connection cause my app to run out of connection poll (when it hits 100). But I have never had the problem with LinqToSQL, im not that technical, so with an optimistic attitude, assumed that, its a new Data Access Function, thus connection.close is automatically done.

Sadly i was Wrong, While running OPENSTA stress Test on the App, with 200 users, my app stopped responding at the 100th user. (my connection poll is Maxed out at 100 user). I was puzzled, what could be the cause of the problem?! Couldnt be the LINQtoSQL.

But the reality, IT was my LINQtoSQL statement that didnt close the SQL Connection!

Solution Seems to be


using (NorthwindDataContext ctx = new NorthwindDataContext())
{
var query = from c in ctx.Customers
select c;

var enumerator = query.GetEnumerator();
}



This will Properly 'DISPOSE' the datacontent and ultimately CLOSE the connection.

Got it from Mike Taulty's Blog

As the post Says: "I like simple things so I'm going to stick with my basic law of "If it's disposable, dispose of it" :-)"

Also FAQ on LINQToSQL Connections
Linq to SQL DataContext Lifetime Management

No comments: