sqlpp11 icon indicating copy to clipboard operation
sqlpp11 copied to clipboard

Question regarding thread safety

Open andranik-s opened this issue 7 years ago • 7 comments

Hi,

Thank you for this library.

I just wanted to ask if there are any guarantees regarding thread safety. More specifically, if I compile sqlite3 with thread safety enabled can I use the same sqlpp::sqlite3::connection object for requests from multiple threads? I suspect that the answer is no, but want to make sure anyway.

andranik-s avatar Dec 02 '19 13:12 andranik-s

Hi,

I am not really an expert for sqlite3. The sqlpp::sqlite::connection wraps an sqlite3 connection. You will get the same thread guarantees. I assume that means that you should not share a connection between threads, but I am not sure.

Best,

Roland

rbock avatar Dec 02 '19 13:12 rbock

@rbock thanks for quick reply.

SQLite has a so-called serialized mode that allows using "SQLite by multiple threads with no restriction". At a glance I found a couple of things in sqlpp (like this one) that could probably cause race conditions.

Sharing db connections between threads might not be a great idea, but I was just wondering if it's possible with this library (edit: I mean without explicit locks).

andranik-s avatar Dec 02 '19 14:12 andranik-s

Good point about the transaction state. I'll would need to think about that for a bit (which will take a while because of other constraints). My tendency would be to say you are right: Sharing is dangerous without explicit locks.

rbock avatar Dec 02 '19 15:12 rbock

Thank you for answering my questions.

I'll would need to think about that for a bit (which will take a while because of other constraints)

Should I close the issue then or leave it as reminder? I'll leave it so you can close it yourself if you want to.

andranik-s avatar Dec 02 '19 15:12 andranik-s

Hi, what about MySQL/MariaDB ? I have Server in Qt which is highly multi-threaded. Each thread in requires its own connection to database. is this same in sqlpp11 ?

LightSith avatar Feb 20 '20 13:02 LightSith

@SmitTheTux That would be the safe option, yes.

rbock avatar Feb 20 '20 14:02 rbock

Finally added a few word of documentation:

https://github.com/rbock/sqlpp11/blob/main/docs/Threads.md

rbock avatar Jan 08 '22 17:01 rbock