Use and difference between sqlite3 API sqlite3_busy_timeout and sqlite3_busy_handler

Source: Internet
Author: User

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.

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.