开发者

Getting an error on website when multiple users access same page simultaneoulsy

开发者 https://www.devze.com 2023-03-28 12:27 出处:网络
I\'m getting the following error message on an ASP.NET 4 website when multiple users access the same page:

I'm getting the following error message on an ASP.NET 4 website when multiple users access the same page:

The underlying provider failed to open. at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf etc......

I use Entity Framework 4 to access the SQL Server 2008 database.

The page works sometimes so I know the connection string is correct. Also the Database is set to multi user and I have MARS set to true in connection string.

Also in the event viewer I sometimes get the SQL Server message :

The server will drop the connection, because the client driver has sent multiple requests while the session is in single-user mode.

As I say, this only happens if I try accessing the same page simultaneously on two different machines by just refreshing the page or clicking the same link.

Any help would be much appreciated.

Connection strings added (first one for ASPNETDB and second for main database):

<add name="ApplicationServices" connectionString="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=False;Persist Security Info=False;User ID=****;Password=****;Connect Timeout=120" />

 <add name="MyDBEntities" connectionString="metadata=res://*/App_Code.MyDBModel.csdl|res://*/App_Code.MyDBModel.ssdl|res://*/App_Code.MyDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=MyServer;Initial Catalog=MyDB;Integrated Se开发者_开发技巧curity=False;User ID=****;Password=****;Connect Timeout=120;User Instance=false;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

The Class I use to access the context is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyDBModel;
using System.Web.Security;

/// <summary>
/// Summary description for MyDataAccess 
/// </summary>
public static class MyDataAccess
{

    // Private Class Members
    //-----------------------------------------------------------------------------------------

    private static MyDBModel.MyDBEntities dal = new MyDBModel.MyDBEntities();

    // Class Constructor / Destructor
    //-----------------------------------------------------------------------------------------

    static MyDataAccess()
    {

        // Set Entity ObjectContext Merge Options - this basically ensures database items aren't cached
        dal.NEWS.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
        dal.NEWS_IMAGES.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;

    }

    // Public Methods for Data Access
    //-----------------------------------------------------------------------------------------

    /// <summary>
    /// Get All Current News Items
    /// </summary>
    public static List<NEWS> GetCurrentNewsItems()
    {
        var items = from news in dal.NEWS
                    where news.NEWS_ACTIVE == true &&
                    news.NEWS_STARTDATE <= DateTime.Today &&
                    news.NEWS_EXPIRYDATE >= DateTime.Today
                    orderby news.NEWS_DATE descending
                    select news;

        return NewsManager.GetMyNewsItems(items).ToList();

    }
}

Then within my .aspx.cs page I would use something like:

var news = MyDataAccess.GetCurrentNewsItems();


I believe your issue is being caused by storing your ObjectContext object in a static variable.

from msdn documentation of ObjectContext Class

The ObjectContext class is not thread safe. The integrity of data objects in an ObjectContext cannot be ensured in multithreaded scenarios.

0

精彩评论

暂无评论...
验证码 换一张
取 消