-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Concurrent Write on SQLite with single connection #22664
Comments
SQLite connections (and most database connections for that matter) are not thread safe. You'll need one connection per thread. Duplicate of #13837 |
@bricelam Thank you for the answer. The connection is not thread safe for all operations? or only for write operations? If is only write operations, maybe I can consider a lock on Is there a solution to optimize connections to an |
@bricelam is the authority here, but like all ADO.NET providers, the connection object isn't thread-safe for any operations (it internally manages state and does not lock against concurrent use). As written above, the solution here would be to support connection pooling (#13837) just like most other database provider - opening and closing would mean allocating and returning physical connection objects to an internally-managed pool. In the meantime, you can implement some basic pooling yourself in your application - it should be too hard. I'd take a look at ObjectPool - instead of instantiating a new SqliteConnection every time a context is created, you'd allocate one from the ObjectPool. |
Thanks for the details guys 👍 I hope that #13837 will be fixed soon, currently the performance on an encrypted database is really bad. I use I think this is may be related to #19588 and improve performance on all platforms. |
I'm having this same problem. When I try to use
I have a single connection which is shared by each DbContext created by request. Everything works fine when it comes to reading information but in writing it throws me the error: Further technical detailsMicrosoft.EntityFrameworkCore 3.1.14 |
I try to use a single open connection with SQLite database and avoid opening it with each instance of
DbContext
to optimize access, this has a big impact with a encrypted database.It works well with read operations but causes errors with concurrent/parallel write operations:
SQLite Error 1: 'cannot rollback - no transaction is active'.
SQLite Error 1: 'cannot start a transaction within a transaction'.
A single connection with concurrent writes is not supported by
EF Core
? Forced to create and open new connection, with performance loss, to manage concurrent writes?Related to #16234?
Steps to reproduce
https://github.com/MADSENSE/Madsense.EFCore.Tests/tree/sqlite-open-connection
Further technical details
EF Core version: 3.1.8
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: netcoreapp3.1
Operating system: Windows 10 2004
IDE: Visual Studio 2019 16.7.3
The text was updated successfully, but these errors were encountered: