GithubHelp home page GithubHelp logo

Comments (3)

roji avatar roji commented on June 12, 2024

@jhinder yes, as long as different DbContext instances are being used, you shouldn't get this exception. However, I can't really tell you what you're doing specifically in your code that triggers this - definitely not from the two snippets posted above (and no stack trace); it's always a good idea to post a fully runnable, minimal project or code sample, rather than a partial set of snippets - there's very frequently something hiding in unposted code that explains the problem. I'd take a very careful look at your DbContext usages - of course as a function of the exception stack trace - and try to see where concurrent usage may be happening.

from efcore.pg.

jhinder avatar jhinder commented on June 12, 2024

OK, so I've continued debugging this a bit and I guess I solved it.

Previously, we would export a NpgsqlConnection (initialized with our connection string) instance with a service type of DbConnection and a lifetime of Singleton. The optionsAction of AddDbContext would retrieve that instance from the supplied IServiceProvider and pass it as the first argument to UseNpgsql.

I've now simplified that a bit so that the optionsAction now only retrieves the connection string (via a simple custom type called ConnectionStringProvider whose only method returns the connection string, and that type is exported into the DI as a singleton because it needs IConfiguration) and passes that to UseNpgsql.

I guess the former scenario (multiple scoped services operate on a singleton DbConnection) is unsupported because Npgsql does something in the way of connection management?

Anyway, I can now throw a hundred parallel queries against the database with the code above and I can run the infinite request loop as well, both seem to work just fine.

from efcore.pg.

roji avatar roji commented on June 12, 2024

I guess the former scenario (multiple scoped services operate on a singleton DbConnection) is unsupported because Npgsql does something in the way of connection management?

Well, a DbConnection represents an actual single physical connection to the database - it's very much not usable concurrently from multiple threads (since the same TCP connection can't be used simultaneously...). I'm not sure what the idea was behind having a single DbConnection previously, but it's definitely not a typical or recommended way to do things, and would indeed lead to NpgsqlOperationInProgressException very quickly.

BTW as an alternative to passing in the connection string, you may want to look at NpgsqlDataSource (see the Npgsql docs).

I'll go ahead and close this as everything seems clear and there doesn't seem to be an issue with Npgsql.

from efcore.pg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.