DataContext.SubmitChanges 超时并带有奇怪的堆栈跟踪

发布于 2025-01-05 06:13:34 字数 3013 浏览 5 评论 0原文

我的为期两年的大型项目刚刚在两周前上线。虽然我们遇到了正常的启动问题,但它非常成功,除了一个奇怪的错误已经出现。

这个故事已经被改变了;实际项目与电影票无关。

我们的网站允许电影院的员工打印电影票。一旦他们决定了要打印的内容,他们就会转到打印票据网页并单击“打印”。这会将一批数据发送到电影票打印机,几秒钟后,用户的票证打印机开始打印票。

我们对门票进行编号,并希望确保不使用重复的编号。执行此操作的代码如下所示:

using (TicketContext context = new TicketContext(ConnectionString)) {

    // call a stored proc that allocates a group of tickets with fancy transaction stuff

    var result = context.GetNextTicketGroup(tickets.Count).FirstOrDefault();
    if (result == null || !result.Column1.HasValue || result.Column1.Value == -1)
        return "There was an error allocating ticket numbers";
    int ticketNumber = result.Column1.Value;

    // Put the ticket numbers in the ticket objects
    int i;
    for (i = 0; i < ticketCount; i++) {
        tickets[i].TicketNumber = ticketNumber + i;
    }
    for (i = 0; i < ticketCount; i++) {
        // Convert the ticket object that we've been working with into 
        // a record that is the kind we have in the database and 
        // put that into the database.  CreateTicket() doesn't do any 
        // database stuff directly.
        DBTicket newticket = CreateTicket(ticket[i]); 
        context.Tickets.InsertOnSubmit(newticket);
    }
    // Each ticket is tied to an Issuance ID - Get a list of IssuanceIDs from the
    // list of tickets and mark them as printed
    var IDs = items.Select(id => id.IssuanceID).Distinct();
    foreach (Guid g in IDs) {
        var Issuance = context.TicketIssuances.Where(ti => ti.ID == g).FirstOrDefault();
        if(Issuance != null) {
            Issuance.Printed = true;
        }
    }

    // Submit the changes to the database
    context.SubmitChanges();

    // Send the printers to the ticket printer via a web service
    using (TicketService.TicketSoapClient ssc = new TicketService.TicketSoapClient()) {
        var a = tickets.ToArray();
        if (ssc.PrintTickets(a) == false)
            return "Error printing.";
    }
}   // LINE 392

现在,不寻常的部分是 context.SubmitChanges() 超时,但堆栈跟踪如下所示:

 [...]  
 at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
 at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
 at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
 at Portal.Tickets.PrintTickets.PrintTicketList(List`1 items) in d:\Source\PrintTickets.aspx.cs:line 392
 at Portal.Tickets.PrintTickets.btnPrintTickets_Click(Object sender, EventArgs e) in d:\Source\PrintTickets.aspx.cs:line 236
 at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
 at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

我在上面的代码中调用了第 392 行;请注意,此时数据上下文超出了范围,远在超时应该发生之后。

这些都是小工作,如果没有某种僵局或其他原因,肯定不会超时;我们一次只将数十条记录放入数据库,并且服务器不会承受过多的负载。我们 99% 以上的作业提交都顺利完成,只有一小部分遇到了这个错误,但是当他们这样做时,其中有几个人在几分钟之内就遇到了这个错误。除了奇怪的堆栈跟踪之外,我倾向于完全归咎于数据库服务器。有什么见解吗?

My big, two-year long project just went live two weeks ago. While we've had the normal launch issues, it's been pretty successful, except for one strange error that has reared its head.

This story has been changed; the actual project has nothing to do with movie tickets.

Our site allows employees at movie theaters to print movie tickets. Once they've decided what they want to print, they go to the print-tickets web page and click 'print'. This sends a batch of data to the movie ticket printer and a few seconds later the user's ticket printer starts pumping out tickets.

We number the tickets, and want to make sure that no duplicate numbers are used. The code to do it looks like this:

using (TicketContext context = new TicketContext(ConnectionString)) {

    // call a stored proc that allocates a group of tickets with fancy transaction stuff

    var result = context.GetNextTicketGroup(tickets.Count).FirstOrDefault();
    if (result == null || !result.Column1.HasValue || result.Column1.Value == -1)
        return "There was an error allocating ticket numbers";
    int ticketNumber = result.Column1.Value;

    // Put the ticket numbers in the ticket objects
    int i;
    for (i = 0; i < ticketCount; i++) {
        tickets[i].TicketNumber = ticketNumber + i;
    }
    for (i = 0; i < ticketCount; i++) {
        // Convert the ticket object that we've been working with into 
        // a record that is the kind we have in the database and 
        // put that into the database.  CreateTicket() doesn't do any 
        // database stuff directly.
        DBTicket newticket = CreateTicket(ticket[i]); 
        context.Tickets.InsertOnSubmit(newticket);
    }
    // Each ticket is tied to an Issuance ID - Get a list of IssuanceIDs from the
    // list of tickets and mark them as printed
    var IDs = items.Select(id => id.IssuanceID).Distinct();
    foreach (Guid g in IDs) {
        var Issuance = context.TicketIssuances.Where(ti => ti.ID == g).FirstOrDefault();
        if(Issuance != null) {
            Issuance.Printed = true;
        }
    }

    // Submit the changes to the database
    context.SubmitChanges();

    // Send the printers to the ticket printer via a web service
    using (TicketService.TicketSoapClient ssc = new TicketService.TicketSoapClient()) {
        var a = tickets.ToArray();
        if (ssc.PrintTickets(a) == false)
            return "Error printing.";
    }
}   // LINE 392

Now, the unusual part is that context.SubmitChanges() times out, but the stack trace looks like this:

 [...]  
 at System.Data.Linq.ChangeDirector.StandardChangeDirector.Insert(TrackedObject item)
 at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
 at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
 at Portal.Tickets.PrintTickets.PrintTicketList(List`1 items) in d:\Source\PrintTickets.aspx.cs:line 392
 at Portal.Tickets.PrintTickets.btnPrintTickets_Click(Object sender, EventArgs e) in d:\Source\PrintTickets.aspx.cs:line 236
 at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
 at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

I called out line 392 in the code above; note that it is at the point where the datacontext is going out of scope, well after the timeout should have happened.

These are small jobs that certainly wouldn't time out with out some sort of deadlock or something; we're only putting tens of records into the database at a time and the server is not under excessive load. 99%+ of our job submission go through just fine, it's only a tiny fraction that hit this error, but when they do, several of them hit it within a few minutes of each other. I'd be inclined to entirely blame the database server, except for the weird stack trace. Any insight?

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

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

发布评论

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

评论(1

吃→可爱长大的 2025-01-12 06:13:34

如果超时,这看起来像是一个导致死锁的竞争条件。尝试增加一些整数值作为票证的唯一 ID 而不是简单地使用 GUID 的动机是什么? LINQ-to-SQL 确实通过设置 DataContext.Log 属性提供了一些有限的日志记录,但我不知道这在您的生产场景中有多有用/可行。

This looks like a race condition to a deadlock if there is a time out. What was the motivation for trying to increment some integer value as the unique ID for tickets instead of simply using a GUID? LINQ-to-SQL does offer some limited logging by setting the DataContext.Log property, but I don't know how useful/viable this would be in your production scenario.

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