不稳定的 LINQ to SQL 错误:“在调用 SubmitChanges 期间无法执行操作。”

发布于 2024-09-30 09:31:57 字数 2339 浏览 4 评论 0原文

好的,所以我从以下行中收到此错误:

  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),或者如果会话在数据库中,则调用 SqlXmlSessionStore.InsertSession(session)。请求是新的,并且会话 ID 丢失或唯一。

每次执行 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;
     }

这似乎仍然是一种非常非常老套的工作方式,我想深入了解问题的根源,所以请告知是否有人知道如何解决这。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转身泪倾城 2024-10-07 09:31:57

您无法更改 SubmitChanges 内部的更改集,也无法在分部类的重写方法中调用 SubmitChanges。我怀疑你正在这样做,但我不完全遵循你的代码片段。

编辑

我不明白如何管理 DataContext 的生命周期。在你说的问题的第一部分

我正在创建一个单独的实例
每次我调用时的 DataContext
服务

但在第二部分你说

我尝试创建一个新实例
DataContext 每次我做
SubmitChanges(),但这导致
我没有 Connection 对象

接下来我不明白的是,只调用一次 SubmitChanges 的更改方法是如何工作的,因为它会丢弃第一次调用后对数据所做的任何更改,它必须要么不记录所有数据,要么第一次调用提交更改后不需要任何调用。

Datacontext 的实例方法不是线程安全的,您不能在应用程序启动时将其放入全局变量中(您正在这样做吗?);您通常需要为每个 HTTP 请求一个新的 Datacontext,您可以使用控制反转框架或仅在代码中完成此操作,在请求开始时新建 dataconext 并在最后调用提交更改。

另一种技术是使用多个DataContext,创建datacontext,进行更改,调用submit chnages,丢弃DataContext,它非常轻量级。您通常不需要多次调用提交更改,我这样做的唯一一次是为了控制 SQL 语句的执行顺序。

如果您可以构建一个最小的示例并发布整个代码,那就太好了。

You can't change the change set inside of SubmitChanges and you can't call SubmitChanges in the overridden methods of the partial classes. I suspect you are doing this, but I don't completely follow your code fragments.

EDIT

I don't understand how the lifetime of the DataContext is being managed. In the first part of the question you say

I'm creating a separate instance of
the DataContext each time I call the
service

but in the second part you say

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

The next thing I don't understand is how your altered method that calls SubmitChanges only once works, since it discards any changes to the data made after it's first call it must either be not logging all of your data or any call after the first call to submit changes is not needed.

The instance methods of the Datacontext are not thread safe, you can't put it in a global variable at application start up (are you doing that?); you usually want a new Datacontext for each HTTP request, which you can do using an Inversion of Control framework or just in code, new up the dataconext at the beginning of the request and call submit changes at the end.

Another technique is to use several DataContexts, create the datacontext, make the changes, call submit chnages, discard the DataContext, it's very lightweight. You don't often need to call submit changes more than once, the only time I have done this is to control the execution order of the SQL statements.

It would be good if you could construct a minimal example and post the whole code.

狼性发作 2024-10-07 09:31:57

从我的帖子中不可能知道这一点,但我发现了问题。我在HttpModule中设置了依赖注入,配置功能上有一个锁。我认为它来自我第一次学习如何使用 StructureMap 时从某个地方复制(然后忘记)的旧代码。我把锁取下来,它就工作了。 (好吧,至少它开始产生新的、不相关的错误)。

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

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.

叫思念不要吵 2024-10-07 09:31:57

我建议您附加一个调试器并捕获该行为中的异常。从那里你应该能够追踪它来自哪里。

或者,覆盖 SubmitChanges 并在每次调用时记录堆栈跟踪以追踪错误的根源。

I suggest you attach a debugger and catch the exception in the act. From there you should be able to trace where it is coming from.

Alternatively, override SubmitChanges and log the stacktrace on ever call to track down the original of the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文