Sql Connection Leak

Apr 7, 2009 at 7:55 PM
I wrote a quick and dirty iterative test app to look for resource leaks and seemed to have stumbled onto one (the code is below).   Each iteration is adding a connection to my SQL Server session.  Once you reach about 32767 iterations, SQL forces the session closed, which causes an exception in the program.  I think this is related to the exception that gets thrown (usually) when MultipleActiveResults is set to false in the connection string, where the CLR tries to re-use a SqlCommand object that believes it still has an open DBReader attached.  I'll try and stitch in the try/finally block cyril mentions and also try disposing the DBCommand object.

I am using .NET 3.5 sp1 and SQL Server 2008 on my x64 notebook, BTW.  I'm also running Windows 7 beta -- I'll try and eliminate that as a possible factor and report back if there's news.

static void Main(string[] args)

        {

            string connectionString = "Persist Security Info=True;user=sa;password=myPassword;database=Northwind;server=LOCALHOST;MultipleActiveResultSets=true";

            using (SqlConnection con = new SqlConnection(connectionString))

            {

                con.Open();

                Northwind db = new Northwind(new SqlQueryProvider(con, Northwind.StandardPolicy, null));

 

                for (int ii = 0; ii < 40000; ii++)

                {

                    try

                    {

                        var custs =

                                (from c in db.Customers

                                where (c.CustomerID=="ALFKI")

                                select c).FirstOrDefault();

 

                        Console.Out.WriteLine("ii={0}", ii);

                    }

                    catch (Exception ex)

                    {

                        Console.Out.WriteLine("Error on request {0}: {1}", ii, ex.Message);

                    }

                }

 

                /*

                NorthwindTranslationTests.Run(db, true);

                NorthwindExecutionTests.Run(db);

                NorthwindCUDTests.Run(db);

                */

                Console.ReadLine();

            }

        }





Apr 7, 2009 at 8:33 PM
hi,

ok i will test this on my environnement

windows 2003 Sever and SQL 2005
windows vista as client

regards.
Cyril

Apr 7, 2009 at 8:38 PM

It looks like your solution to put a reader.close() in finally{} block inside Project<t> fixed the issue on my system.  It’s not clear to my why that should work. 

Just to make me feel better, I’m also disposing the SqlCommand object in Execute<t>.  I’ll keep looking, but I didn’t want you to waste time since your patch already fixes the problem. 

From: ccyril [mailto:notifications@codeplex.com]
Sent: Tuesday, April 07, 2009 1:34 PM
To: Erik Johnson
Subject: Re: Sql Connection Leak [IQToolkit:52598]

From: ccyril

hi,

ok i will test this on my environnement

windows 2003 Sever and SQL 2005
windows vista as client

regards.
Cyril

Read the full discussion online.

To add a post to this discussion, reply to this email (IQToolkit@discussions.codeplex.com)

To start a new discussion for this project, email IQToolkit@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


This e-mail is for the use of the intended recipient(s) only. If you have received this e-mail in error, please notify the sender immediately and then delete it. If you are not the intended recipient, you must not use, disclose or distribute this e-mail without the author's prior permission. We have taken precautions to minimize the risk of transmitting software viruses, but we advise you to carry out your own virus checks on any attachment to this message. We cannot accept liability for any loss or damage caused by software viruses. Any views and/or opinions expressed in this e-mail are of the author only and do not represent the views of Epicor Software Corporation or any other company within its group.
Coordinator
May 7, 2009 at 1:00 AM
This is fixed in the latest drop.