Solved

Problem with multiple connections using a shared Linq Context

Posted on 2008-09-30
7
2,346 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 62

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 62

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

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

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 62

Expert Comment

by:Fernando Soto
ID: 22626671
Very good.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now