LINQ to SQL错误,错误:“在调用SubmitChanges期间无法执行操作." [英] Erratic LINQ to SQL Error: "operation cannot be performed during a call to SubmitChanges."

查看:177
本文介绍了LINQ to SQL错误,错误:“在调用SubmitChanges期间无法执行操作."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我从行中得到了这个错误:

  System.Data.Linq.DataContext.CheckNotInSubmitChanges() +42
  System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +54

我正在做的是跟踪应用程序的状态并记录每个请求.该应用程序以Json,Xml和Html呈现输出.

问题在于错误是不稳定的.仅每隔几个请求就会发生一次.当我开始执行Ajax请求时,该错误开始发生.通过快速的请求,我已经能够确定错误发生的频率更高(例如,如果我重复单击一个链接).

每次调用引发错误的服务时,我都会创建一个单独的DataContext实例.我真的很难弄清楚问题出在哪里,对于任何正在发生的事情,我将不胜感激.谢谢.

***

 [InvalidOperationException: The operation cannot be performed during a call to SubmitChanges.]
    System.Data.Linq.DataContext.CheckNotInSubmitChanges() +80
    System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +73
    Magic.Model.Sessions.SqlXmlSessionStore.SubmitChanges() in SqlXmlSessionStore.cs:17
    Magic.Model.Sessions.SqlXmlSessionStore.UpdateSession(Session session) in SqlXmlSessionStore.cs:64
    Magic.Web.SessionService.OpenSession(MagicRequestContext requestContext) in SessionService.cs:36
    Magic.Web.SessionService.Magic.Model.Sessions.ISessionService.OpenSession(IRequestContext requestContext) in SessionService.cs:23

提到的方法是:

private bool SubmitChanges()
{
   _sqlContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
   return _sqlContext.ChangeConflicts.Count == 0;   
}

public bool UpdateSession(Session session)
{
   var record = _sqlContext.SessionRecords.Single(x => x.SessionId == session.Key);
   _converter.PopulateRecordData(session, record);
   return SubmitChanges();
}

如果会话在数据库中并且处于活动状态,则所有会话服务类都将调用SqlXmlSessionStore.UpdateSession(session);如果请求是新的,并且会话ID缺失或唯一,则调用SqlXmlSessionStore.InsertSession(session).

每次尝试执行SubmitChanges()时,我都尝试创建DataContext的新实例,但这导致我没有Connection对象,即使我拉同一个conn也是如此.设置中的字符串.可能与我的本地计算机有关吗?

好的,所以我做了一些有效的事情,但是我不确定这是否会有我无法预见的问题.

我只允许DataContext提交一次.通过将SubmitChanges()代码更改为:

    private bool _canSubmit = true;

    bool SubmitChanges(){
        if(_canSubmit)
        {
            _sqlContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
            _canSubmit = false;
            return _sqlContext.ChangeConflicts.Count == 0;      
        }
        return false;
     }

这似乎仍然是一种非常棘手的方法,我想深入探讨此问题,因此请告知是否有人知道如何解决此问题.

解决方案

从我的帖子中不可能知道这一点,但是我发现了问题.我在HttpModule中设置了依赖项注入,并且配置功能上有一个锁.我认为这是我第一次学习如何使用StructureMap时从某个地方复制(然后忘记)的旧代码中得到的.我卸下了锁,它起作用了. (嗯,至少它开始产生新的,不相关的错误).

哦,它影响我的DataContext的原因是因为包装Datacontext实例的类在锁内.

Okay, so I'm getting this error from the lines:

  System.Data.Linq.DataContext.CheckNotInSubmitChanges() +42
  System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +54

What I'm doing is tracking an application's state and logging each request. The app renders output in Json, Xml and Html.

The thing is the error is erratic. It only happens every few requests. The error started happening when I started doing Ajax requests. I've been able to determine that the error occurs more frequently with rapid requests (i.e. if I click a link repeatedly).

I'm creating a separate instance of the DataContext each time I call the service that is throwing the error. I am having a really difficult time figuring out what the issue is here, and I would really appreciate any guidance and/or explanation as to what is happening. Thank you.

* EDIT : **

 [InvalidOperationException: The operation cannot be performed during a call to SubmitChanges.]
    System.Data.Linq.DataContext.CheckNotInSubmitChanges() +80
    System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode) +73
    Magic.Model.Sessions.SqlXmlSessionStore.SubmitChanges() in SqlXmlSessionStore.cs:17
    Magic.Model.Sessions.SqlXmlSessionStore.UpdateSession(Session session) in SqlXmlSessionStore.cs:64
    Magic.Web.SessionService.OpenSession(MagicRequestContext requestContext) in SessionService.cs:36
    Magic.Web.SessionService.Magic.Model.Sessions.ISessionService.OpenSession(IRequestContext requestContext) in SessionService.cs:23

The methods mentioned are:

private bool SubmitChanges()
{
   _sqlContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
   return _sqlContext.ChangeConflicts.Count == 0;   
}

public bool UpdateSession(Session session)
{
   var record = _sqlContext.SessionRecords.Single(x => x.SessionId == session.Key);
   _converter.PopulateRecordData(session, record);
   return SubmitChanges();
}

All the session service class does is call SqlXmlSessionStore.UpdateSession(session) if the session is in the db and active, or SqlXmlSessionStore.InsertSession(session) if the request is new and the session id is missing or unique.

I tried creating a new instance of the DataContext each time I did a SubmitChanges(), but that resulted in me not having a Connection object, even when I pull the same conn. string from settings. Could this be something having to do with my local machine?

Okay, so I did something that is working, but I am not sure if there will be a problem with this that I am not foreseeing.

I only allow the DataContext to submit once. I accomplished this by changing the SubmitChanges() code to:

    private bool _canSubmit = true;

    bool SubmitChanges(){
        if(_canSubmit)
        {
            _sqlContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
            _canSubmit = false;
            return _sqlContext.ChangeConflicts.Count == 0;      
        }
        return false;
     }

This still seems like a very very hacky way for this to work, and I would like to get to the bottom of the issue, so please advise if anyone has any idea how to fix this.

解决方案

There would have been no way to know this from my post, but I found the problem. I set up the dependency injection in an HttpModule, and there was a lock on the configuration function. I think it was there from old code I copied (and then forgot about) from somewhere when I was first learning how to use StructureMap. I removed the lock and it worked. (well, at least it began generating new, unrelated errors).

Oh, and the reason it was affecting my DataContext was because the classes that wrapped instances of the Datacontext were inside the lock.

这篇关于LINQ to SQL错误,错误:“在调用SubmitChanges期间无法执行操作."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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