CommandBehaviour.closeconnection

  • Thread starter Thread starter venkat.murthy
  • Start date Start date
V

venkat.murthy

While retrieving multiple rows from a dataset, we go for a datareader.
Following best practices, i had implemented the
commandbehaviour.closeconnection to ensure that associated connection is
closed, when datareader is closed.

But, i need to return a datareader from a method in class one to a method in
class two.

Can i close the connection object in class one, and still continue to use
the datareader object in class two.

What would be the impact if i close the datareader in class two, without
closing connection in class one.


Any code snippet on the performance wise would prove great help.

Regards,
V
 
hi
Can i close the connection object in class one, and still continue to use> the datareader object in class two.
The answer is no. coz the reader needs an open connection

When we Executereader, using of commandbehaviour.closeconnection is the best
way to go coz the runtime will close the connection for you when the reader
is finished.

also this is the best method when we pass a reader between each layer in an
n-tired design

regards
Ansil
 
No. When you pass a DataReader to another layer you MUST use
CommandBehavior.CloseConnection AND the receiving layer MUST close the
DataReader to close and free the connection. You cannot close the source
connection in the sending layer as this is the pipe to return the data.
Passing DataReaders between routines is a dangerous practice and I don't
recommend it as it requires both sender and receiver to be coded correctly.
NO, you can't depend on the runtime (the garbage collector) to cleanup the
orphaned connection object. It simply does not happen soon enough (it might
take hours). As a result of this strategy, your connection pool fills during
production and you're pooched.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top