In-depth SQLite use summary of multithreading _java

Source: Internet
Author: User
Tags mutex sqlite

SQLite supports 3 threading modes:
single thread: this mode, no mutex, multithreading is not secure. All mutex locks are disabled and error occurs when used concurrently. Sqlite_threadsafe=0 parameters are added when SQLITE is compiled, or enabled when Sqlite3_config (Sqlite_config_singlethread) is invoked before initializing SQLITE.

Multithreading: This mode is safe as long as a database connection is not used by multiple threads at the same time. The source code is to enable Bcoremutex, disable Bfullmutex. The fact is to disable the lock on the database connection and prepared statement (prepared statements), so you cannot use the same database connection or prepared statement concurrently in multiple threads. Enabled by default when SQLite is added with the sqlite_threadsafe=2 parameter at compile time. If the Sqlite_threadsafe is not 0, you can invoke Sqlite3_config (sqlite_config_multithread) enabled before initializing SQLITE, or set up Sqlite_open_ when you create a database connection Nomutex flag.

Serial: SQLite is thread-safe. Enable all locks, including Bcoremutex and Bfullmutex. Because both the database connection and the prepared statement are locked, multithreading uses these objects without concurrency and becomes serial. Enabled when SQLite Sqlite_threadsafe = 1 parameter is added when compiling. If the Sqlite_threadsafe is not 0, you can invoke Sqlite3_config (sqlite_config_serialized) enabled before initializing SQLITE, or set up Sqlite_open_ when you create a database connection Fullmutex flag .

    The initialization referred to here refers to the call to the sqlite3_initialize () function, which calls the Sqlite3_open () is invoked automatically, and only the first call is valid.

To achieve thread safety, SQLITE must set the SQLITE_THREADSAFE preprocessing macro to 1 at compile time. On Windows and Linux, this is done in a good binary release that has been compiled. If you are unsure whether the library you are using is thread-safe, you can call the Sqlite3_threadsafe () interface to find it. call Sqlite3_threadsafe () to obtain the Sqlite_threadsafe parameters for the compile period .

That is, thread mode can be specified at compile time (when the SQLite library is compiled from the source code), at startup (when the application is initialized with SQLite), or at run time (when creating a database connection). In general, the pattern specified at run time overrides the specified mode at startup, and the pattern specified at startup overrides the schema specified at compile time. However, once a single-threaded mode is specified, it cannot be overwritten. The default threading mode is serial mode.

Select thread mode at compile time
You can specify the threading mode by defining the Sqlite_threadsafe macro. If not specified, the default is serial mode. Define macros sqlite_threadsafe=1 Specify the use of serial mode; =0 use single-threaded mode; =2 use multithreaded mode.

The return value of the Sqlite3_threadsafe () function can determine the thread pattern specified at compile time. If single-threaded mode is specified, the function returns FALSE. If serial or multi-threaded mode is specified, the function returns True. Because the Sqlite3_threadsafe () function is selected before multithreaded mode and at startup and at run time, it cannot distinguish between multithreaded and serial mode and the mode of startup and runtime.

The last sentence can be realized by the Sqlite3_threadsafe function to understand the SQLITE_API int sqlite3_threadsafe (void) {return sqlite_threadsafe;} If a single-threaded pattern is specified at compile time, the critical mutex logic is omitted at construction time, so it is not possible to specify either serial mode or multithreaded mode at startup or at run time.

Select thread Mode at startup
If you do not specify a single-threaded pattern at compile time, you can use the Sqlite3_config () function to modify the threading mode when the application is initialized. Parameter Sqlite_config_singlethread can be specified as
single-threaded mode, Sqlite_config_multithread specified as multithreaded mode, sqlite_config_serialized specified as serial mode.

Select thread mode at run time
If you do not specify a single-threaded mode at compile-time and startup time, each database connection can be specified as either multithreaded or serial mode individually, but cannot be specified as single-threaded mode. If you specify single-threaded mode at compile time or at startup, you cannot specify multithreaded or serial mode when creating a connection.

Specifies the threading mode using the third parameter of the SQLITE3_OPEN_V2 () function when creating a connection. Sqlite_open_nomutex identifies a connection that creates a multithreaded mode; Sqlite_open_fullmutex identifies a connection that creates a serial mode. If you do not specify an identity, or if you use the Sqlite3_open () or Sqlite3_open16 () function to create a database connection, the thread mode specified at compile time or startup is used as the default thread mode.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.