SQLite Forum

Multiple process connections for in-memory DB
Login

Multiple process connections for in-memory DB

(1) By Ivan (Ivan.R) on 2020-07-30 11:14:26 [source]

Hi all,

I'm evaluating use of SQLite in-memory database. The network server product that I'm going to embed it into is using multiple worker processes to handle the traffic (processes are created by forking the main processes after some common initialisation logic has been performed). I need DB data to be shared between all worker processes.

I've checked documentation on in-memory database (https://www.sqlite.org/inmemorydb.html) and as far as I understood, even when using "shared cache" feature it is not possible to share in-memory database across multiple processes

Of course, all database connections sharing the in-memory database need to be in the same process.

However, a small PoC was created where data was written to the DB from one process and read from another process and the data was accessible across multiple processes.

DB was created like this: file::memory:?cache=shared. I'm using SQLite from C++ via sqlite_orm. Version of SQLite used is 3.32.3.

I'm not 100% sure, but I believe some of the ORM objects may be stored in the shared memory.

Does anyone here have any theories as to what is going on? Am I misunderstanding the documentation and it is actually possible to share in-memory database across multiple processes? Or is something else going on here?

Any help would be much appreciated!

(2) By Dan Kennedy (dan) on 2020-07-30 16:21:53 in reply to 1 [link] [source]

However, a small PoC was created where data was written to the DB from one process and read from another process and the data was accessible across multiple processes.

Did the demo turn on URI filenames?

https://www.sqlite.org/uri.html

If URI filenames are not enabled, then you will actually create a file on disk named "file::memory:?cache=shared" instead of an in-memory database. Which would explain seeing the data from multiple processes.

Dan.