简体   繁体   English

使用Telerik Open Access时,如何解决“多线程”和“不允许新交易”错误?

[英]How do I fix “multiple threads” and “new transaction is not allowed” errors when using Telerik Open Access?

We have a asp.net MVC application that uses Telerik Open Access. 我们有一个使用Telerik Open Access的asp.net MVC应用程序。 We're having all kinds of problems just saving data once we put it into production. 一旦将数据投入生产,我们就会遇到各种各样的问题。 Telerik no longer supports Open Access, so we can't get any help from them, but we're supposed to be going live right now and don't have the budgeted hours to change now. Telerik不再支持Open Access,因此我们无法从他们那里获得任何帮助,但是我们应该现在就可以上线,并且没有预算时间可以更改。 Can someone give me some suggestions on how to get around these problems? 有人可以给我一些如何解决这些问题的建议吗?

We're getting the same errors when updating and inserting records, but these problems don't always occur. 在更新和插入记录时,我们会遇到相同的错误,但并非总是会出现这些问题。 I never get these errors running the solution from Visual Studio on my computer or once the project is deployed to our testing server. 在我的计算机上从Visual Studio运行解决方案时,或者将项目部署到我们的测试服务器后,我都不会遇到这些错误。 On the production server, when several users are using the application, we start to see errors. 在生产服务器上,当多个用户使用该应用程序时,我们开始看到错误。

Example code would be this insert function: 示例代码将是以下插入函数:

public void CreateAttachments(tblCoDoc obj)
{
    try
    {
        dat.Add(obj);
        dat.SaveChanges();
    }
        catch (Exception exception)
        {
            throw exception;
        }
}

and this update function: 和此更新功能:

public void UpdateWorkOrderApprGen(tblWorkOrder obj)
    {
        var context = new KoorsenOpenAccessContext();
        var upd =
        (
            from workOrder in dat.tblWorkOrders
            where workOrder.WorkOrderId == obj.WorkOrderId
            select workOrder
        ).FirstOrDefault();

        if (upd != null)
        {
            upd.ReferenceNumber = obj.ReferenceNumber;
            upd.CustomerContract = obj.CustomerContract;
            upd.VendorContract = obj.VendorContract;
            upd.DateResolved = obj.DateResolved;

            try
            {
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

In both cases, those methods are in a class called Repository. 在这两种情况下,这些方法都在一个名为Repository的类中。 There is a private variable defined for this class (KOpenAccessContext is the class defined in the project implementing a OpenAccessContext class): 为此类定义了一个私有变量(KOpenAccessContext是在项目中定义的实现OpenAccessContext类的类):

private static KOpenAccessContext dat = null;

and then in the Repository constructor, that private variable is assigned to a new KOpenAccessContext: 然后在Repository构造函数中,将该私有变量分配给新的KOpenAccessContext:

dat = new KoorsenOpenAccessContext();

The error messages we're getting are 我们收到的错误消息是

Telerik.OpenAccess.Exceptions.DataStoreException: Telerik.OpenAccess.RT.sql.SQLException: New transaction is not allowed because there are other threads running in the session. Telerik.OpenAccess.Exceptions.DataStoreException:Telerik.OpenAccess.RT.sql.SQLException:不允许新事务,因为会话中正在运行其他线程。

and

[InvalidOperationException: Unable to start second transaction] [InvalidOperationException:无法启动第二笔交易]

The first one is the most common. 第一个是最常见的。

This post: SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the session . 这篇文章: Entity Framework的SqlException-不允许新事务,因为会话中有其他线程在运行 suggests the problem is from the save being in a for loop, which is not the case. 提示问题出在保存中,原因是保存在for循环中,事实并非如此。

The 3rd answer down suggests putting the code in a using transaction and using context blocks; 第三答案建议将代码放入使用事务和上下文块中。 I get this error: 我收到此错误:

Telerik.OpenAccess.OpenAccessException: System.InvalidOperationException: This SqlTransaction has completed; Telerik.OpenAccess.OpenAccessException:System.InvalidOperationException:该SqlTransaction已经完成; it is no longer usable. 它不再可用。

I found this post: http://www.telerik.com/forums/how-do-i-fix-a-new-transaction-is-not-allowed-error-is-telerik-open-access#swcnW_tPGEWglUih1TEAKg suggesting that I create "use short living context instances". 我发现了这篇文章: http : //www.telerik.com/forums/how-do-i-fix-a-new-transaction-is-not-allowed-error-is-telerik-open-access#swcnW_tPGEWglUih1TEAKg建议我创建“使用短期生存环境实例”。 To me that meant to create a new Open Access. 对我来说,这意味着要创建一个新的开放访问。 I tried this and still got the "new transaction is not allowed" error: 我尝试了此操作,但仍然收到“不允许新交易”错误:

public void CreateAttachments(tblCoDoc obj)
{

    try
    {
        var db = new KoorsenOpenAccessContext();
        db.Add(obj);
        db.SaveChanges();
    }
        catch (Exception exception)
        {
            throw exception;
        }
}

I'm really at a loss and I've got a client and boss looking to me for a solution. 我真的很茫然,我有一个客户和老板向我寻求解决方案。 I'd love to know the cause of this (thinking it may be because there are multiple users), but what I really need is a solution. 我很想知道造成这种情况的原因(认为可能是因为有多个用户),但是我真正需要的是一种解决方案。 How can I get around this problem? 我该如何解决这个问题?

I think you have a couple problems going on. 我认为您遇到了一些问题。 You mention you are using a repository pattern with a private static Context that is initialized with this code: 您提到您正在使用带有私有静态上下文的存储库模式,该私有静态上下文使用以下代码初始化:

dat = new KoorsenOpenAccessContext();

If your create and update functions are inside the repository then they should use this instance ( dat ) of the context and not go about creating their own function private instances: 如果您的创建和更新函数在存储库中,则它们应使用上下文的此实例( dat ),而不要创建自己的函数私有实例:

public void UpdateWorkOrderApprGen(tblWorkOrder obj)
    {
        // Don't do this, use the repository instance of the context instead
        // var context = new KoorsenOpenAccessContext();
        var upd =
        (
            from workOrder in dat.tblWorkOrders
            where workOrder.WorkOrderId == obj.WorkOrderId
            select workOrder
        ).FirstOrDefault();

        if (upd != null)
        {
            upd.ReferenceNumber = obj.ReferenceNumber;
            upd.CustomerContract = obj.CustomerContract;
            upd.VendorContract = obj.VendorContract;
            upd.DateResolved = obj.DateResolved;

            try
            {
                dat.SaveChanges();  // Use the repository's context here
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

Make sure your repository implements IDisposable and you dispose of the repository's context properly by calling dat.Dispose() 确保您的存储库实现IDisposable,并通过调用dat.Dispose()正确处理存储库的上下文

Your other option is to skip over the repository pattern and remove the repository private context instance ( dat ). 您的另一个选择是跳过存储库模式并删除存储库私有上下文实例( dat )。 Use the function private instance of the context instead. 请改用上下文的函数私有实例。 Telerik OpenAccess best practices suggest a using statement to ensure the context is closed and disposed of properly: Telerik OpenAccess最佳实践建议使用using语句,以确保上下文已关闭并正确处理:

public void CreateAttachments(tblCoDoc obj)
{    
    try
    {
        using (var db = new KoorsenOpenAccessContext())
        {
            db.Add(obj);
            db.SaveChanges();
        }
    }
    catch (Exception exception)
    {
        throw exception;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何向Telerik Open Access MVC项目添加新表? - How do I add a new table to a Telerik Open Access MVC project? 如何在Telerik窗口中打开Telerik ReportView - How can I open Telerik ReportView in Telerik Window 如何使用Telerik Open Access从不同数据模型的不同表中获取数据 - How to get the data from different tables from different data models using telerik open access MVC Core 2.0 EF Core SqlException:不允许新事务,因为会话中正在运行其他线程 - MVC Core 2.0 EF Core SqlException: New transaction is not allowed because there are other threads running in the session ForEach 中的 db.SaveChanges 导致“不允许新事务,因为会话中正在运行其他线程” - db.SaveChanges in ForEach causes 'New transaction is not allowed because there are other threads running in the session' 使用 RedirectToAction 时如何维护 ModelState 错误? - How do I maintain ModelState errors when using RedirectToAction? SQL Server事务和事务隔离-收到我不知道如何解决的错误 - SQL Server transactions and transaction isolation - getting errors that I don't know how to fix 从控制器返回时,如何获取FileResult以打开新的浏览器实例? - How do i get FileResult to open new browser instance when returning from a controller? 使用jQuery脚本时如何访问方法? - How do I access methods when using a jquery script? 分页时如何保留Telerik MVC网格列的顺序和大小? - How do I preserve Telerik MVC grid column order and sizes when paging?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM