Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check SQLite connection's threading mode?

Tags:

sqlite

Is it possible to check the threading mode of an active SQLite connection? I know about sqlite3_threadsafe(), but

The return value of the sqlite3_threadsafe() function shows only the compile-time setting of thread safety, not any run-time changes to that setting made by sqlite3_config().

like image 293
Alexey Romanov Avatar asked Nov 12 '10 07:11

Alexey Romanov


People also ask

Is SQLite multi threaded?

In serialized mode, SQLite can be safely used by multiple threads with no restriction.

How thread safe is SQLite table level lock or DB level lock?

The Android uses java locking mechanism to keep SQLite database access serialized. So, if multiple thread have one db instance, it always calls to the database in serialized manner and of course database is thread-safe.

Can SQLite handle multiple connections?

The current version of SQLite handles multiple connections through its thread-mode options: single-thread, multi-thread, and serialized. Single-thread is the original SQLite processing mode, handling one transaction at a time, either a read or a write from one and only one connection.

What happens if you don't close SQLite connection?

If there is no outstanding transaction open then nothing happens. This means you do not need to worry too much about always closing the database before process exit, and that you should pay attention to transactions making sure to start them and commit at appropriate points.


2 Answers

Hmmm, seven year old question. You have answered it yourself, though. For the next person who comes looking:

sqlite3_db_mutex will return NULL if threading mode is Single-thread or Multi-thread.

So we have:

(sqlite3_threadsafe , sqlite3_db_mutex) : threading mode

(0 , NULL) : Single-thread

(1 , NULL) : Mutli-thread

(1 , valid) : Serialized

like image 164
Barry Smith Avatar answered Oct 21 '22 06:10

Barry Smith


Here is a partial answer:

sqlite3_mutex *sqlite3_db_mutex(sqlite3*);

This interface returns a pointer the sqlite3_mutex object that serializes access to the database connection given in the argument when the threading mode is Serialized. If the threading mode is Single-thread or Multi-thread then this routine returns a NULL pointer.

Now I only need a way to distinguish Single-thread from Multi-thread...

like image 27
Alexey Romanov Avatar answered Oct 21 '22 08:10

Alexey Romanov