新的交易是不允许的,因为在会话中运行其他线程 [英] New transaction is not allowed because there are other threads running in the session

查看:161
本文介绍了新的交易是不允许的,因为在会话中运行其他线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获得新的交易是不允许的,因为在会话中运行其他线程。

它无关,与foreach循环或任何人通常有问题,在与此消息相结合。

我使用了repositoy模式,共同在整个环境的要求开一个EF4。有事,不能确定到底是什么,我只要我努力的SaveChanges与上下文,跨请求得到这个消息,而且只dissappears一次,我回收的应用程序池。

我是不是关闭连接?如何判断?我使用一个新的环境为每个请求?是的。

这是怎么回事?是否有一个变通?

编辑:(上下文工厂)

 私有静态字典<字符串,CoinEntities> _instances;

    公共静态CoinEntities DefaultInstance
    {
        得到
        {
            如果(HttpContext.Current == NULL)
            {// TODO:模拟代替。测试。
                如果(!Instances.ContainsKey(_ TEST))
                    实例[_ TEST] =新CoinEntities();
                返回实例[_ TEST];
            }

            如果(!Instances.ContainsKey(_ DEFAULT))
                实例[_ DEFAULT] =新CoinEntities();

            返回实例[_ DEFAULT];
        }
    }
 

解决方案

我不认为这是没有设置环境唯一的问题(上下文不保留打开的交易 - 你会看到,因为提交的修改的话)。如果你有这样的问题,你很可能不会为每个请求新的上下文实例,或者你有一些多线程/异步处理上的共享上下文实例(=一个连接)。此异常表示,多线程(可能是多重处理的请求)正试图用自己的事务在同一个连接 - 这是不可能的。

角情况下也能提供上下文的连接人工处理,但我猜你会提到它,如果你使用它。

编辑:

您工厂不按要求方面提供 - 它提供了单一的情况下为所有要求!静态字典所以先下 _DEFAULT 创建实例,并把它存储所有的请求密钥和其他所有请求都将使用共享。

Getting "new transaction is not allowed because there are other threads running in the session".

It has nothing to do with foreach loops or anything people usually have problems with in conjunction with this message.

I using a EF4 with a repositoy pattern and common context open throughout the request. Something happens, can't determine exactly what, and I get this message as soon as I try to savechanges with the context, across requests, and it only dissappears once I recycle the app pool.

Am I closing the connection? How can I tell? Am I using a fresh context for every request? Yes.

What's going on? Is there a work-around?

Edit: (context factory)

    private static Dictionary<string, CoinEntities> _instances;

    public static CoinEntities DefaultInstance
    {
        get
        {
            if (HttpContext.Current == null)
            { //todo: mock instead. testing.
                if (!Instances.ContainsKey("_TEST"))
                    Instances["_TEST"] = new CoinEntities();
                return Instances["_TEST"];
            }

            if (!Instances.ContainsKey("_DEFAULT"))
                Instances["_DEFAULT"] = new CoinEntities();

            return Instances["_DEFAULT"];
        }
    }

解决方案

I don't think that this is only problem of not disposed contexts (context doesn't keep opened transaction - you would see it because of uncommitted changes). If you have this problem you most probably don't use the new context instance per request or you have some multi threaded / asynchronous processing on the shared context instance (= one connection). This exception says that multiple threads (probably multiple processed requests) are trying to use their own transaction on the same connection - that is not possible.

Corner case can be manual handling of connections provided to context but I guess you would mention it if you use it.

Edit:

Your factory doesn't provide per request context - it provides single context for all request!!! That static dictionary is shared among all request so the first creates instance and stores it under _DEFAULT key and all other requests will use it.

这篇关于新的交易是不允许的,因为在会话中运行其他线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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