Solved

Problem with multiple connections using a shared Linq Context

Posted on 2008-09-30
7
2,419 Views
Last Modified: 2013-11-11
I have tried to develop a site using linq to manage and process the dynamic data on the site.
When it is just a few people  accessing the site, it works fine. When we have more people accessing the site (we had 20 people testing today) we began recieving the following error:

ANY help would be greatly appreciated!
Thanks!

ExecuteReader requires an open and available Connection. The connection's current state is open. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.
 
Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 
 
Stack Trace: 
 
[InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is open.]
   System.Data.SqlClient.SqlConnection.GetOpenConnection(String method) +4874932
   System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String method, SqlCommand command) +11
   System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async) +144
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +87
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141
   System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12
   System.Data.Common.DbCommand.ExecuteReader() +12
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +975
   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +113
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +344
   System.Data.Linq.DataContext.ExecuteMethodCall(Object instance, MethodInfo methodInfo, Object[] parameters) +69
   DinnTrophyClasses.ConfiguratorDataContext.Site_Basket_Item_GetAwardBasketItems(Nullable`1 intBasketID, Nullable`1 intAwardID, Nullable`1 intAwardComponentID, Nullable`1 intShiptoId) in C:\Projects\DinnTrophy\Solutions\DinnTrophyClasses\Configurator.designer.vb:309
   DinnTrophyClasses.AwardConfigurator.get_AwardsForBasketByBasketId(Int32 IntBasketID) in C:\Projects\DinnTrophy\Solutions\DinnTrophyClasses\AwardConfigurator.vb:197
   CartSummary_Content.RefreshTotals() in C:\Inetpub\wwwroot\DinnTrophy\UserControls\CartSummary_Content.ascx.vb:25
   CartSummary_Content.OnPreRender(EventArgs e) in C:\Inetpub\wwwroot\DinnTrophy\UserControls\CartSummary_Content.ascx.vb:37
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

Open in new window

0
Comment
Question by:tdartt
  • 4
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22607487
It is stating that you can not use the ExecuteReader that is already open. It sounds like you are using the same DataContext object for multiple request.
0
 

Author Comment

by:tdartt
ID: 22607673
Is there a way to use a Datacontext for asynchronous use? or do you need a seperate datacontext for each call? I have a web application that is accessing static information loaded into a single datacontext.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22607868
Hi tdartt;

DataContext Class: From Microsoft documentation.
http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx

In general, a DataContext instance is designed to last for one  "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to  create. A typical LINQ to SQL application creates DataContext instances at method scope or as a  member of short-lived classes that represent a logical set of related database  operations.

With that said you state that, "I have a web application that is accessing static information loaded into a single datacontext." If it is static information then there is no need to re-query the database. I would convert the data into a list. Once that is done you access the list and no need to execute a new query.

Fernando
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Author Comment

by:tdartt
ID: 22607894
Ah!
Thats an excellent idea :) I will give it a shot.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 155 total points
ID: 22624497
Hi tdartt;

Have you tried the solution?

Fernando
0
 

Author Comment

by:tdartt
ID: 22626160
I ended up using Rick Strahls Data Context factory and it worked like a charm :)
http://www.west-wind.com/weblog/posts/246222.aspx

Thanks for the help!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 22626671
Very good.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question