Can I read and write to a SQLite database concurrently from multiple connections?

后端 未结 2 382
鱼传尺愫
鱼传尺愫 2020-11-28 04:25

I have a SQLite database that is used by two processes. I am wondering, with the most recent version of SQLite, while one process (connection) starts a transaction to write

2条回答
  •  攒了一身酷
    2020-11-28 05:17

    SQLite3 explicitly allows multiple connections:

    (5) Can multiple applications or multiple instances of the same application access a single database file at the same time?

    Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.

    For sharing connections, use SQLite3 shared cache:

    Starting with version 3.3.0, SQLite includes a special "shared-cache" mode (disabled by default)

    In version 3.5.0, shared-cache mode was modified so that the same cache can be shared across an entire process rather than just within a single thread.

    5.0 Enabling Shared-Cache Mode

    Shared-cache mode is enabled on a per-process basis. Using the C interface, the following API can be used to globally enable or disable shared-cache mode:

    int sqlite3_enable_shared_cache(int);

    Each call sqlite3_enable_shared_cache() effects subsequent database connections created using sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). Database connections that already exist are unaffected. Each call to sqlite3_enable_shared_cache() overrides all previous calls within the same process.

提交回复
热议问题