Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Data.SQLite in-memory database multi-threading

I am creating a System.Data.SQLite in-memory database using connection string as

"Data Source=:memory:", 

and want to access this database among multi-threads.

Now what I do is to clone the SQLiteConnection object and pass the copy to worker threads.

But I found that different threads actually get individual instances of in-memory database, not a shared one. How can I share one in-memory database among threads?

Thanks!

like image 976
xiagao1982 Avatar asked Sep 14 '12 02:09

xiagao1982


1 Answers

Based on the SQLite documentation for in-memory databases, I would try a datasource named with URI filename convention file::memory:?cache=shared or the like instead of :memory: (and note specifically the cache name that all connections are being told to use). As explained on the page, every instance of a :memory: is distinct from one another, exactly as you found.

Note you may also have to first enable shared-cache mode before making the connections to the in-memory database (as specified in the shared cache documentation with a call to sqlite3_enable_shared_cache(int) for this to work.

like image 66
Googer Avatar answered Oct 11 '22 15:10

Googer