I require database access operations from several threads, through a singleton object, which holds a database connection. I read from SQLite3's website, saying that 'an sqlite3 structure could only be used in the same thread that called sqlite3_open() to create it. You could not open a database in one thread then pass the handle off to another thread for it to use'. I'm wondering under my situation, is there any threat of thread-unsafety?
In serialized mode, SQLite can be safely used by multiple threads with no restriction.
SQLite engine do not have built-in security to protect databases, rather, it relies on its environment such as the operating system to provide security for database content. While Android provides security mechanisms for SQLite databases, it has been shown to be inadequate.
A good rule of thumb is to avoid using SQLite in situations where the same database will be accessed directly (without an intervening application server) and simultaneously from many computers over a network. SQLite will normally work fine as the database backend to a website.
Is it safe to share an sqlite3 connection between threads? Yes, unless you change the default THREADSAFE option.
If the SQLite library is compiled with -DSQLITE_THREADSAFE you are OK with the more recent SQLite 3 versions.
The author of SQLite says:
Beginning with version 3.5.0, SQLite enforces this itself using its
own internal mutexes, so the application is free to (try to) use the
same database connection from multiple threads at the same time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With