Mahesh KP
Mahesh KP

Reputation: 6446

Reader close and dispose

Please look at the code. we have a method like this which returns reader. ie

public IRecordsReader GetValues()
{
   IRecordsReader TestReader.....

   return TestReader;
}

and we are calling that method inside another method like this

IRecordsReader ResultReader = GetValues();
ResultReader.Close();
ResultReader.Dispose();

ie we are closing and disposing that reader.

My doubt is that we are not properly closing the TestReader in the GetValues() method. So whether it will cause any connection pool issues? Please give your suggestions.

Upvotes: 0

Views: 227

Answers (1)

Deeptechtons
Deeptechtons

Reputation: 11125

You should probably wrap your SqlDataReader with using so that it would automatically close and dispose the IDisposable object.

Like below

using(IRecordsReader ResultReader = GetValues())
{
//do your stuff
}
//resultReader is closed and disposed from now

More Info

since you do this

public IRecordsReader GetValues()
{
   IRecordsReader TestReader.....

   return TestReader;
}

and this

IRecordsReader ResultReader = GetValues()

this is what you are actually doing

IRecordsReader ResultReader = TestReader

so you are indirectly closing/disposing the TestReader instance. You get it?. Put it in code and try you will know :)

Answer

OP Asked -So whether it will cause any connection pool issues? Please give your suggestions.

Unless your connection limit is not exceeded you won't have a issue. But since you are closing and disposing the resource you will not have any issues :)

Upvotes: 1

Related Questions