Connection Sharing and DataReaders

Connection Sharing and DataReaders

Old forum URL: forums.lhotka.net/forums/t/5498.aspx


xxxJasonxxx posted on Tuesday, September 30, 2008

I spent a bunch of time converting several objects to share their connection using ConnectionManager and and adding the TransactionScope attribute above the DataPortal_Insert/Update/Delete methods so they will run in the same transaction.  I decided to use the ConnectionManager to share connections to avoid DTC overhead on the transactions.

And I thought life was good until I ran my code.  Now I'm getting error messages about the connection only being able to have one DataReader open at a time.  Doh!  I totally should have seem this coming but somehow I missed it.  I'm about to start digging into my code and I have a feeling I'm going to find a few situations where I can't get the datareader closed before a child object tries to reuse the connection and open a second datareader. 

Has anyone else run into this problem and have any advice/suggestions? 

xxxJasonxxx replied on Tuesday, September 30, 2008

Initially it seems I may just be getting burned when I re-use the connection in DataPortal_Fetch.  Maybe I can't re-use the connection in DataPortal_Fetch (where I open a reader).....but that seems lame.

xxxJasonxxx replied on Tuesday, September 30, 2008

I don't think I can always use the same reader.  Some of the child objects are actual "child" objects so they expect to be passed a reader.  But a few of the child objects are re-used "root" objects that need to open their own reader.

sergeyb replied on Tuesday, September 30, 2008

I see…  So you “root” object can open there own connections then, could they?

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: xxxJasonxxx [mailto:cslanet@lhotka.net]
Sent: Tuesday, September 30, 2008 3:13 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] Connection Sharing and DataReaders

 

I don't think I can always use the same reader.  Some of the child objects are actual "child" objects so they expect to be passed a reader.  But a few of the child objects are re-used "root" objects that need to open their own reader.

xxxJasonxxx replied on Tuesday, September 30, 2008

Yeah, I think you're right.....those child root objects are going to have to open their own connections in DataPortal_Fetch instead of grabbing the shared connection from the ConnectionManager.  Seems disappointing but I don't see how to get around that.

JoeFallon1 replied on Tuesday, September 30, 2008

You are correct.

When a root opens a dr then a child cannot use the same connection. So I create a 2nd connection and open dr2 to pass to the child.

Joe

 

sergeyb replied on Tuesday, September 30, 2008

One suggestion is to use MARS and load all the data from single stored procedure using single reader and iterating through result sets.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: xxxJasonxxx [mailto:cslanet@lhotka.net]
Sent: Tuesday, September 30, 2008 3:04 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Connection Sharing and DataReaders

 

I spent a bunch of time converting several objects to share their connection using ConnectionManager and and adding the TransactionScope attribute above the DataPortal_Insert/Update/Delete methods so they will run in the same transaction.  I decided to use the ConnectionManager to share connections to avoid DTC overhead on the transactions.

And I thought life was good until I ran my code.  Now I'm getting error messages about the connection only being able to have one DataReader open at a time.  Doh!  I totally should have seem this coming but somehow I missed it.  I'm about to start digging into my code and I have a feeling I'm going to find a few situations where I can't get the datareader closed before a child object tries to resuse the connection and open a second datareader. 

Has anyone else run into this problem and have any advice/suggestions? 



Copyright (c) Marimer LLC