Skip to content

MARS requirement gives issues for DB's that don't support this #1665

Closed

Description

@bartdevriendt

Working on my provider for MySQL i'm running into troubles with the MARS requirement. As MySql doesn't support this all the outer queries fail with the already open DataReader exception. I managed to workaround this by catching the exception in my DB Command wrapper object and opening a new connection to the database and creating a reader on this new connection. Luckily the MySql connector has connection pooling by default so not too many connections are opened, but still all the "cloned" connections are untracked and undisposed.

Is there a possibility this MARS requirement is reconsidered, or some kind of option is introduced to turn this of ?

Activity

  1. added this to the 7.0.0 milestone on Feb 20, 2015
  2. rowanmiller commented on Feb 20, 2015

    @rowanmiller
    Contributor

    @anpete - can you drive some discussion around this

  3. anpete commented on Feb 20, 2015

    @anpete
    Contributor

    @bartdevriendt This is really just a limitation of our default query compiler in our core provider framework - one we will probably address at some point. A couple of things to consider:

    1. When compiling a query, you have complete control over the expression tree produced and you can opt out of our default behavior by overriding the appropriate VisitX method (or expression tree visitor) in your query model visitor.
    2. Have you considered completely buffering the results from the outer reader so that you can close that reader?

    Cheers,
    Andrew.

  4. bartdevriendt commented on Feb 27, 2015

    @bartdevriendt
    ContributorAuthor

    By replacing all necessary services and overriding the default behaviour it would indeed be possible to overcome the limitation. Closing this issue.

  5. removed their assignment
    on May 6, 2015
  6. roji commented on May 25, 2015

    @roji
    Member

    @bartdevriendt, @anpete, @rowanmiller, Npgsql has the same issue with the lack of MARS support.

    Since non-MARS is a pretty common case (maybe even the normal case?), doesn't it make sense for the EF core to contain a compatible query compiler and/or necessary services? This way driver writers would simply choose the non-MARS one rather than reimplementing their own?

    On a related note, I'm (still) very unfamiliar with the actual mechanics going on, but it seems odd for something as simple as an Include() to depend on MARS, even in the default query compiler. Wasn't this handled without MARS in EF6? Was there a big architecture change that made it impossible to continue doing so? And handling that by buffering up the entire outer reader seems very problematic from a performance/memory-usage perspective.

    Thanks for any help/pointers on how to proceed with this...

  7. roji commented on May 25, 2015

    @roji
    Member

    Just to add a little on my last note...

    Npgsql is nearing the end of a pretty huge rewrite (3.0.0-beta.1 has just been released). The option of implementing MARS via SQL cursors does exist, but doing it in a correct and efficient way would be a pretty big task that I really want to avoid for the immediate future. I'd also like to avoid implementing a performance-problematic solution such as buffering an entire reader (a sort of poor man's MARS, I guess).

    So if EF7 is compatible with an efficient, non-MARS way of doing things, that would be ideal - and it would be great to have the necessary services available for use in EF7 itself. Otherwise we really need to think we to do on our side...

  8. reopened this on May 25, 2015
  9. divega commented on May 25, 2015

    @divega
    Contributor

    Hi @roji, we actually did adress this issue in PRs #2177 and #2198. Unfortunately the latter appears in GitHub as if it had been force-pushed so there aren't any changes associated with it, but in summary, providers override 'RelationalConnection.IsMultipleActiveResultSetsEnabled' to control the EF runtime behavior. If a provider returns 'false' EF will automatically buffer the remaining results of any pending query before starting a new one.

    Hope this helps.

  10. roji commented on May 25, 2015

    @roji
    Member

    @divega, thanks for the quick answer. Great, I'll go and implement that and will write back if there are any issues.

    Do I understand correctly that this non-MARS simply buffers the data from the open reader in memory? Do you guys view this as an adequate solution from a performance perspective? At least for stuff like simple Includes, I was expecting simple SQL joins rather than any reliance on MARS or buffering...

    (apologies in advance if I'm misunderstanding things, am not very familiar with EF internals)

  11. divega commented on May 25, 2015

    @divega
    Contributor

    @roji It depends 😄 but we believe that in many cases this will be better than the alternative.
    @anpete implemented it so he may want to correct me or provide more details, but here are a couple of points that should help explain it:

    1. The implementation of Include() still uses JOINs in cases in which they would preserve the cardinality of the results. It only switches to multiple queries when it needs to traverse a collection navigation property, a case in which JOINs would cause an explosion of the number of rows and data redundancy in the results.
    2. Independently of the implementation of Include() an application can attempt to execute a query while iterating over the results of another query which would require MARS or buffering to work correctly. The design addresses (or at least has the potential to address) that scenario as well.
  12. 13 remaining items

  13. inossidabile commented on Mar 7, 2017

    @inossidabile

    Well that's strange. I'm running a single request from a properly injected context and if it uses Include it throws this error. So it's only related to having include or not. Also with having include it does two queries indeed.

    I'm using EF6, last stable. With .NET Core 1.1. Driver is MySQL (Pomelo.EntityFrameworkCore.MySql 1.1). Am I missing something?

  14. roji commented on Mar 7, 2017

    @roji
    Member

    This repository is for Entity Framework Core, not EF6. You're better off opening an issue with a code sample at your provider's repo (Pomelo).

  15. inossidabile commented on Mar 7, 2017

    @inossidabile

    Yeah, I use that really. Miswritten. I didn't realise that could be a provider issue. Thanks!

  16. divega commented on Mar 8, 2017

    @divega
    Contributor

    @inossidabile filing an issue on the Pomelo provider repo sounds like a good idea.

    But in case it : that provider is for EF Core and not EF6. And EF6 never used MARS for Include queries.

  17. inossidabile commented on Mar 8, 2017

    @inossidabile

    Yeah, like I said, I mistakenly wrote that. I'm using EF Core.

  18. PeterKneale commented on Feb 13, 2018

    @PeterKneale

    Does this mean that the below async scenario will only work with MsSqlServer with MARS enabled?
    I am attempting to port an application from MsSqlServer to Postgres/NPgSql using EF Core 2 and would be surprised if this was not possible because of the use of the multiple concurrent async queries. Is that the case?

    var task1 = _context.Users.SingleAsync(x => x.Id == 1);
    var task2 = _context.Projects.SingleAsync(x => x.Id == 1);
    
    Task.WaitAll(task1, task2);
    
    var user = task1.Result;
    var project = task2.Result;
  19. ajcvickers commented on Feb 13, 2018

    @ajcvickers
    Contributor

    @PeterKneale In general WaitAll should not be used with EF Core because EF cannot execute multiple queries in parallel. If this is working on SQL Server (or for any provider) then it is because EF is serializing the two queries to run consecutively. While this will likely work for cases only involving queries because of the way the internal query state machine works, it will likely not work if the async calls are doing anything that is strictly not just a query. It's pretty hard to know when that's the case, and since there is no concurrency advantage to doing this, it means it's best to just not use WaitAll at all.

  20. modified the milestones: , 1.0.0 on Oct 15, 2022
  21. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions