Click here to Skip to main content
15,890,609 members
Articles / Web Development / ASP.NET
Tip/Trick

Probable leak while using SqlDataReader in ADO.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
5 Aug 2010CPOL 22.2K   6   5
Probable leak while using SqlDataReader in ADO.NET
I was doing a bit of exploration on Connections with ado.net.While doing this exercise i realized that when using CloseConnection along with the reader i was under the impression that it closes by itself but actually it wont close the connection.

I had pasted the code sample below which i executed during the test.

while (true)
{
    Thread.Sleep(2000);
    SqlConnection objConnection = default(SqlConnection);
    objConnection = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Northwind;Data Source=.;Max Pool Size=1");
    SqlCommand objCommand = new SqlCommand("Select * from customers", objConnection);
    objConnection.Open();
    SqlDataReader reader = objCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
    while (reader.Read())
    {
    }
    Console.WriteLine((i++).ToString());
}


So the tip is either create the sqlconnection object by using - using syntax (which ensures that it Disposes connection Object) or after the execution before leaving make a call to explicitly close the connection.

This ensures that connection will be closed for sure. Hence no leak

I hope this helps!.

Regards,
-Vinayak

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect MindTree Ltd
India India
Motivated achiever who guides organizations in applying technology to business settings, provides added value, and creates project deliverables in a timely manner. An experienced Technical Consultant, have successfully led large project teams of more than 20 people from requirements gathering to implementation and support using C#, .NET ,ADO.NET, ADO.NET Entity Framework,ASP.NET,ASP.NET MVC, WCF and SQL Server.

Comments and Discussions

 
GeneralReason for my vote of 5 yeah this is perfect! Pin
manjeeet7-Aug-10 1:14
manjeeet7-Aug-10 1:14 
GeneralReason for my vote of 5 Good one, I am surprised, thanks for... Pin
vinayakshenoy2000@gmail.com5-Aug-10 20:45
vinayakshenoy2000@gmail.com5-Aug-10 20:45 
GeneralConfusion with sample code attached Pin
r verma11-Aug-10 20:22
r verma11-Aug-10 20:22 
GeneralRe: Confusion with sample code attached Pin
Vinayaka Krishna Shenoy11-Aug-10 21:16
Vinayaka Krishna Shenoy11-Aug-10 21:16 
GeneralRe: Confusion with sample code attached Pin
r verma11-Aug-10 21:47
r verma11-Aug-10 21:47 
Hi,

Correct me if I'm wrong somewhere but as per MSDN "CommandBehavior.CloseConnection" takes care of following task:

When the command is executed, the associated Connection object is closed when the associated DataReader object is closed.

Yes this also means that if you do not close DataReader then too your connection will be closed but this action will be taken by GarbageCollector and one can never guarantee when GC will deallocate things. So you cannot instantly predict whether your connection is automatically closed or not.

Thats the reason closing of DataReader is considered the best practice.

For ensuring connection is closed obviously as you mentioned 'using' construct is helpful but using 'finally' will also work.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.