Solved

Problem with multiple connections using a shared Linq Context

Posted on 2008-09-30
7
2,370 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

21 Experts available now in Live!

Get 1:1 Help Now