3

My application creates a in-memory database (:memory:) using sqlite as a back end.

I want my master thread to create a connection to a in-memory database and this connection to be shared by multiple threads. Is this possible? SQLite 3.7.8 is available for download right now.

Is the shared cached a possible way to go?

4
  • I don't think so since every connection to :memory: creates an independent DB in memory... see sqlite.org/inmemorydb.html Commented Oct 30, 2011 at 14:45
  • Maybe a duplicate of [this question][1] [1]: stackoverflow.com/questions/3267077/… Commented Oct 30, 2011 at 14:49
  • 1
    @NicolasModrzyk there is no answer to your link. However it is the same question, indeed. Commented Oct 30, 2011 at 14:53
  • @Yahia, yes, multiple open calls will make multiple in-memory databases; so, just do one open and share the connection among the threads. Commented Oct 30, 2011 at 16:06

1 Answer 1

3

If you open the connection to your in-memory database using serialized mode, then the connection may be shared among multiple threads.

For this to work, your SQLite must be compiled threadsafe -- this is the default.

Depending on your application, you may get better performance with a large shared cache to an on-disk database, or with WAL mode if you have many reader threads.

Example:

sqlite3 *pDb

if (sqlite3_open_v2(":memory:", &pDb, SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK) {

    start_thread1_with_db_handle(pDb);

    start_thread2_with_db_handle(pDb);

    // etc.
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure what you need; an example added to answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.