PRAGMA locking_mode
This pragma sets or queries the database connection locking-mode.
PRAGMA schema.locking_mode; PRAGMA schema.locking_mode = NORMAL | EXCLUSIVE
On the sample database
What PRAGMA locking_mode returns on the sample database, in SQLite 3.54.1 as built for the browser by sql.js:
| locking_mode |
|---|
| normal |
What the SQLite documentation says
This pragma sets or queries the database connection locking-mode. The locking-mode is either NORMAL or EXCLUSIVE.
In NORMAL locking-mode (the default unless overridden at compile-time using SQLITE_DEFAULT_LOCKING_MODE), a database connection unlocks the database file at the conclusion of each read or write transaction. When the locking-mode is set to EXCLUSIVE, the database connection never releases file-locks. The first time the database is read in EXCLUSIVE mode, a shared lock is obtained and held. The first time the database is written, an exclusive lock is obtained and held.
Database locks obtained by a connection in EXCLUSIVE mode may be released either by closing the database connection, or by setting the locking-mode back to NORMAL using this pragma and then accessing the database file (for read or write). Simply setting the locking-mode to NORMAL is not enough - locks are not released until the next time the database file is accessed.
There are three reasons to set the locking-mode to EXCLUSIVE.
- The application wants to prevent other processes from accessing the database file.
- The number of system calls for filesystem operations is reduced, possibly resulting in a small performance increase.
- WAL databases can be accessed in EXCLUSIVE mode without the use of shared memory. (Additional information)
When the locking_mode pragma specifies a particular database, for example:
PRAGMA main.locking_mode=EXCLUSIVE;
then the locking mode applies only to the named database. If no database name qualifier precedes the "locking_mode" keyword then the locking mode is applied to all databases, including any new databases added by subsequent ATTACH commands.
The "temp" database (in which TEMP tables and indices are stored) and in-memory databases always uses exclusive locking mode. The locking mode of temp and in-memory databases cannot be changed. All other databases use the normal locking mode by default and are affected by this pragma.
If the locking mode is EXCLUSIVE when first entering WAL journal mode, then the locking mode cannot be changed to NORMAL until after exiting WAL journal mode. If the locking mode is NORMAL when first entering WAL journal mode, then the locking mode can be changed between NORMAL and EXCLUSIVE and back again at any time and without needing to exit WAL journal mode.
From sqlite.org's pragma documentation for SQLite 3.54.1, in the public domain.