New transaction is not allowed because there are other threads running in the session

后端 未结 4 1263
無奈伤痛
無奈伤痛 2020-12-19 05:14

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

4条回答
  •  春和景丽
    2020-12-19 06:02

    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.

提交回复
热议问题