The following error message is displayed: datareader associated with this command which must be closed first.

Source: Internet
Author: User

One of my applications is written in dbml generated by LINQ, and an error occurs when two operations are executed at the same time.

Operation 1: Read a table

Operation 2: Write a table (a long operation lasts about 1 minute)

 

Add the following parameters in Web. config to eliminate this error.

Connectionstring = "Data Source = servername; initial catalog = databasename; Integrated Security = sspi;Multipleactiveresultsets = true"

 

I did this. while writing a table, there is no error in reading the table. However, the table write operation started to report errors. It seems that the problem has not been solved. There is also an explanation:

I posted this question on the azgroups mailing list and got a number of excellent responses. basically my problem is that I shouldn't be using a Singleton pattern for the datacontext but shocould be instantiating a new datacontext for each job unit that's got Med.

 

I noticed myCodeThis may be because of an error. In order not to create a new dbdatacontext instance for each table operation, I have built a static strength. It seems that you need to define a separate instance for each operation. So it seems that every operation page defines an object instance and closes it after the operation is complete. Do you have any good solutions?

 

This does not work: public static mydbdatacontext DB = new mydbdatacontext ();

 

I wrote some discussions in this article. It seems that there is no result yet. I will take a closer look.

Http://weblogs.asp.net/bigyanr/default.aspx

====================================

Original Problem description link,

Http://guyellisrocks.com/sql-server/linq-to-sql-architecture-question/

 

LINQ to SQL architecture question

I have structured a project with a single LINQ to SQL. dbml file and a single dataaccess class that is used to call stored procs and query against this dbml class. the dataaccess class implements the Singleton pattern inasmuch as their's a static dataaccess property which is used for all DB access.

The business layer of the application creates an instance of the dataaccess object, callthe appropriate data access function and then returns.

When running as a web application it is being hit rapidly by 2 clients: (1) a browser callto it about 1 to 50 times a second (writes data to DB) and (2) an Excel spreadsheet cballs the web app via a Web Service on an ad hoc basis and does a query for data.

When the spreadsheet does the query (the browser's hitting the site up to 50 times a second at the same time) I am getting several errors such as the following:

    • Invalid attempt to call metadata when reader is closed
    • There is already an open datareader associated with this command which must be closed first.
    • Executenonquery requires an open and available connection. The connection's current state is closed.
    • A transport-level error has occurred when processing ing results from the server. (provider: Session provider, error: 18-connection has been closed by peer)
    • A severe error occurred on the current command. The results, if any, shocould be discarded.
    • A transport-level error has occurred when processing ing results from the server. (provider: Shared Memory provider, error: 0-No process is on the other end of the pipe .)

The first 2 errors were solved by adding Mars to the connection string: multipleactiveresultsets = true

The rest of them I solved by adding a static Lock Object to the dataaccess class and wrapping all CALS to the database in a lock (lockobj ){}.

This solution doesn' t feel right though.

Comments on the general approach I 've taken... comments on the lock () solution...

June 29 2008 guy Ellis 1 comment

Filed under: SQL Server

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.