SQLite: Multi-threaded operations database "locked" workaround

Source: Internet
Author: User
Tags sqlite

1. Enable SQLite to support multi-threaded (not sure whether it is not, add, for the future)

Thread mode can be selected at compile time/start/Run time, reference: http://www.cnblogs.com/liaj/p/4015219.html

My changes:

1) Add compilation options:

-dsqlite_threadsafe=2

2) Open the database file using SQLITE3_OPEN_V2 instead of Sqlite3_open

SQLITE3_OPEN_V2 (Strdbname,sqlite_p, sqlite_open_readwrite| Sqlite_open_create | Sqlite_open_nomutex, NULL);

2. Use Sqlite3_busy_handler to process the Sqlite_busy status (required)

Reference:

Https://www.sqlite.org/c3ref/busy_handler.html

http://iihero.iteye.com/blog/1222539

http://blog.csdn.net/guofu8241260/article/details/36378291

My changes:

Static intCallback_in_busy (void*ptr,intcount) {    intTimeout = * ((int*) PTR);    Usleep (timeout); return 1;}Static voidSqlgetlock (Sqlite3 *sqlite_p,intms) {    if(MS >0) {Sqlite3_busy_handler (sqlite_p, Callback_in_busy, (void*) &ms); }    Else{Sqlite3_busy_handler (sqlite_p,0,0); }}intSqlExec (Sqlite3 *sqlite_p,Const Char*strSQL) {    Char*perrmsg = NULL;intrc =SQLITE_OK; intRET =-1; Sqlgetlock (Sqlite_p, $); RC= Sqlite3_exec (sqlite_p, strSQL, NULL, NULL, &perrmsg); if(RC! =SQLITE_OK) {printf ("%s%d sqlite3_exec error:%s, strSQL = [%s].\n", __func__, __line__, Sqlite3_errmsg (sqlite_p), strSQL); if(Perrmsg! =NULL) {printf ("%s%d sqlite3_exec error:%s\n", __func__, __line__, perrmsg);        Sqlite3_free (PERRMSG); } ret= -1; }    Else{ret=0; }    returnret;}

SQLite: Multi-threaded operations database "locked" workaround

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.