When using SQLite in multi-data connection mode, sqlite_busy is often encountered, because when using the current connection to access data, apply for a corresponding level of lock, some locks of different levels are mutually exclusive. This error will be returned if the lock cannot be applied. At this time, you only need to wait for a moment and wait until other connection operations are completed. After the lock is released, the lock can be obtained and operated.
However, sqlite3 does not implement default processing for retry after sqlite_busy occurs, but provides a processing mechanism of busy handle. There are two APIs for creating busy handle.
Int sqlite3_busy_handler (sqlite3 *, INT (*) (void *, INT), void *)
A function can define a callback function. When the database is busy, SQLite calls this function.
When the callback function is null, clear busy handle and return directly if no lock is applied.
The second function of the callback function will be passed as the number of times the function is called by this busy event.
If the callback function returns a non-0 value, the database retries the current operation. If 0 is returned, sqlite_busy is returned for the current operation.
Int sqlite3_busy_timeout (sqlite3 *, int MS );
Defines the number of milliseconds. When the number of milliseconds is not reached, SQLite will sleep and retry the current operation.
If the lock is not applied for within milliseconds, sqlite_busy is returned for the current operation.
When MS is <= 0, the busy handle is cleared and will be returned if no lock is applied.
We can know from the above that a function can control the number of times of timeout, and a function can control the time of timeout.
However, for a connection, there can only be one busy handle, so the two functions will affect each other. Setting one function will clear the other function at the same time. You should select one function as needed.