efcore: Union: new transaction is not allowed because there are other threads running in the session
I have the same problem after upgrading to EF Core 2.1, at least for now in two different places in the code, the same code was running with no problems before upgrade from 2.0
The issue seems to be related to union, after changing the code as below the error disappeared. In BOTH places the problem fixed by dividing the union to multi selects and do union in memory.
This code failed
var uids = _db.Table1.Where(x => x.AccountUId == accountUId).Select(x => x.UId)
.Union(_db.Table2.Where(x => x.AccountUId == accountUId).Select(x => x.UId))
.ToList();
This code succeeded
var uids1 = _db.Table1.Where(x => x.AccountUId == accountUId).Select(x => x.UId).ToList();
var uids2 = _db.Table2.Where(x => x.AccountUId == accountUId).Select(x => x.UId).ToList();
var uids = uids1.Union(uids2); //UNION in memeory
But now there is an extra database request! It seems EF stuck with other internal selects in the union!
Note:
I was getting the Exception later in SaveChanges. In Sql profiler, SaveChanges was not running any Sql command, but after the error, I saw the “stuck” Select in Sql profiler.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (11 by maintainers)
Commits related to this issue
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to roji/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to roji/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to roji/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to dotnet/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to roji/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to roji/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to dotnet/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to roji/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to dotnet/efcore by roji 5 years ago
- Implement set operations (Union, Intersect...). Navigation/include support not included. Fixes #6812 Fixes #12549 — committed to dotnet/efcore by roji 5 years ago
This works for me. IQueryable Union is doing good now. Thanks!
Hi @ajaybhargavb I managed to reproduce it with your example and few changes. You should call saveChanges after you create the list. Bellow is an example using the latest EFCore version 2.2.1 can you please reopen that issue?
I can confirm that I also have found this issue and it seems to also be centered around unions. It’s not an open project and I’m not sure what can be shared.
Example
Without the commented
ToList()'sI also get a"new transaction is not allowed because there are other threads running in the session"when calling acontext.SaveChanges()(in my case_unitOfWork.Save()) later in in the calling function - after an insert.This happens consistently; it isn’t load effected. The calls in this request are not async. This is a WebApi application. There are no concurrent threads working with the same DbContext?
WebApi .csproj project is:
Business Logic library .csproj is:
@ajcvickers I’m facing the same problem again on other location and used the same workaround to pass that exception!
EF Core 2.1.4
Not working!
Working!
var paymentMethodIds = bankIds.Union(creditIds); // TODO: this should be one db request, due to EF bug it was splitted and union done in memory!`
Reopening to give us a chance to investigate this further, even if we didn’t get an answer about the repro. Here are a couple of possible things to try: