Initial state
The middle part is the disk buffer of the system.
?
Get read lock
You need to obtain a read lock before the write operation to get the basic data of the database in order to parse the SQL statement.
Note shared locks are only for the system's disk cache, not for disk files. The file lock is actually some flag of the system kernel. After the system crash or power-down, the lock will fail. Usually the process of creating a lock exits also causes the lock to fail.
?
Read data from the database
Read the system disk cache first and then read the user space. If the cache is hit, the user space is read directly from the disk cache.
Note that the entire database is not read into memory.
?
Get reserved lock
Before making changes to the database, get reserved lock first. Other process operations that have shared read locks are allowed, but only one database file has to be given to reserved lock. Ensures that only one process at a time can perform database write operations.
?
To create a rollback log file
The log file is the page in the database file that you want to change.
Also included is a header that records the size of the original database file. The number of the page is written to the page of each database.
Note that the file is not written to disk at this time.
?
Changing the page of the database in user space
Each database link has its own copy of the database file. As a result, other database connections can still be read normally.
?
To swipe a log file into disk
In most systems, two flush or fsync operations are required. The first time the file data is brushed into a file, the second is used to change the page number of the log file in the header, and the header is brushed into the file.
?
Get exclusive lock
Get the exclusive lock in two steps. First, a pending lock is obtained to ensure that no new writes and reads are done. Then wait for the other read process to end, release the read lock, and finally get the exclusive lock.
?
Writing data from user space to a database file
At this point, you can determine that no other database connection is being read from the database. This step is usually only written to the disk cache and is not written to the database file.
?
Write changes to the disk file
Call the Fsync or flush operation. This section and the write log file to disk occupy the most time in a transaction.
?
Delete log files
SQLite gives the appearance of have made no changes to the database file or have made the complete set of changes to T He database file depending on whether or not the rollback journal file exists.
Deleting a log file is not atomic, but from the user's perspective, the operation is atomic. Ask the operating system if the file exists, answer yes or No.
In some systems, it is a time-consuming operation to delete a file. SQLite can be configured to change the size of the file to 0 or overwrite the header of the log file with zero. In either case, the log file is not recoverable, so SQLite thinks the commit is complete.
?
Release lock
In this picture, the database contents of the user space have been emptied. In the latest version, optimizations have been made. In the first page of the database, a counter is maintained, and each write operation adds one to the counter. If the counter is not changed, the database connection can reuse the database content in the user space.
?