4 ms·
I think we are talking past each other and see what you’re saying. Also rereading the initial comment it is now obvious what is going on. For some reason I tho
by epcoa 2y ago
I think we are talking past each other and see what you’re saying.
Also rereading the initial comment it is now obvious what is going on.
For some reason I thought they were claiming weird interactions between statement objects (which would occur if serialization was not used). It appears they are just sharing a single statement object between threads and expecting that to work.
I read past that because it never occurred someone working with the API would think that would work. It would imply some kind of thread local storage, and as you are aware that is definitely not something commonly done on APIs of this type.
> And there is no confusion about who owns what?
What you are describing is a higher level of race condition of course, but not one where the internal state of SQLite is inconsistent or breaking invariants. Specifically there is no confusion about ownership: there is one tx (stack) per connection and it is owned by the connection, to which access is serialized by the library.
In SQLite you implicitly open a transaction with any statement so it’s not necessarily a problem to be “starting a transaction” since the implicit start will be idempotent. And you can certainly use this behavior to run concurrent reads and writers across multiple threads on a single connection without issue.
Yes if you try to run multiple BEGIN statements that will error out. You’re right some ORMs do some fancy magic over a connection to serialize those. Sorry that was not the initial problem I was addressing (though obviously the GP had confusion in this area too)
Commenting too late at night.
- jpc0 2y agoAh yes. We are on the same page now. I 100% would believe what you described in this comment is expected behaviour, it makes sense to me. And yeah I was thinking about this a decent amount today and thought the only way you could even begin doing this is with thread local storage but could not think why they would do that concidering how widely supported (platform wise) sqlite is.
- fnordlord 2y agoI think what you're describing is what I was confused about: sharing prepared statement objects across multiple threads. It makes sense why it would not work but, at least in my case, it was an easy place to find myself when combining the "best practices" of prepared statements and a single shared connection. Thanks for the clear interpretation of what's going on. Until now, it just seemed like a "gotcha." But I always knew it was user error and that sqlite was too battle tested for it to be a bug.