获取网站上一个错误,当多个用户访问simultaneoulsy同一页 [英] Getting an error on website when multiple users access same page simultaneoulsy

查看:218
本文介绍了获取网站上一个错误,当多个用户访问simultaneoulsy同一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到在一个ASP.NET 4网站下面的错误消息当多个用户访问同一页面:


  

基础提供未能打开。在
  System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf
  等......


我使用实体框架4来访问SQL Server 2008数据库。

页作品有时所以我知道连接字符串是正确的。另外,数据库设置为多用户,我有火星设置为true在连接字符串。

另外,在事件查看器中我有时会在SQL Server消息:


  

服务器将断开连接,因为客户端驱动程序有
  发送多个请求而会话处于单用户模式。


正如我说的,如果我尝试刚刚刷新页面或者点击同一链路同时接入两个不同计算机上的同一个页面这只发生。

任何帮助将是非常美联社preciated。

补充(第一个为ASPNETDB和第二主数据库)的连接字符串:

 <添加名称=ApplicationServices的connectionString =数据源= MyServer的;初始目录= MYDB;集成安全性= FALSE;坚持安全信息= FALSE;用户ID = *** *;密码= ****;连接超时= 120/> <添加名称=MyDBEntities connectionString=\"metadata=res://*/App_$c$c.MyDBModel.csdl|res://*/App_$c$c.MyDBModel.ssdl|res://*/App_$c$c.MyDBModel.msl;provider=System.Data.SqlClient;provider连接字符串=安培; QUOT;数据源= MyServer的;初始目录= MYDB;集成安全性= FALSE;用户ID = ****;密码= ****;连接超时= 120;用户实例= FALSE; MultipleActiveResultSets =真放; QUOT;的providerName =System.Data.EntityClient/>

我用它来访问上下文的类是如下:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用MyDBModel;
使用System.Web.Security;///<总结>
///为MyDataAccess摘要说明
///< /总结>
公共静态类MyDataAccess
{    //私有类成员
    // ------------------------------------------------ -----------------------------------------    私有静态MyDBModel.MyDBEntities DAL =新MyDBModel.MyDBEntities();    //类的构造函数/析构函数
    // ------------------------------------------------ -----------------------------------------    静态MyDataAccess()
    {        //设置实体ObjectContext的合并选项 - 这基本上确保了数据库项目不缓存
        dal.NEWS.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
        dal.NEWS_IMAGES.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;    }    //公共方法进行数据访问
    // ------------------------------------------------ -----------------------------------------    ///<总结>
    ///获得所有最新新闻条目
    ///< /总结>
    公共静态列表<新闻> GetCurrentNewsItems()
    {
        VAR项目=从dal.NEWS新闻
                    其中,news.NEWS_ACTIVE ==真&放大器;&安培;
                    news.NEWS_STARTDATE< = DateTime.Today和放大器;&安培;
                    news.NEWS_EXPIRYDATE> = DateTime.Today
                    排序依据news.NEWS_DATE降
                    选择新闻;        返回NewsManager.GetMyNewsItems(项目).ToList();    }
}

然后在我的.aspx.cs页我会用这样的:

  VAR新闻= MyDataAccess.GetCurrentNewsItems();


解决方案

我相信您的问题正在被存储在一个静态变量的ObjectContext的对象引起的。

ObjectContext类


  

该ObjectContext类不是线程安全的。数据的完整性
  在一个ObjectContext的对象不能在多线程保证
  场景。


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 Security=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.

这篇关于获取网站上一个错误,当多个用户访问simultaneoulsy同一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆