sqlite:多線程操作資料庫“database is locked”解決方案

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   ar   strong   檔案   sp   

1. 使sqlite支援多線程(不確定是否非加不可,暫且加上,以備後患)

可以在編譯時間/啟動時/運行時選擇線程模式,參考:http://www.cnblogs.com/liaj/p/4015219.html

我的修改:

1)添加編譯選項:

-DSQLITE_THREADSAFE=2

2)開啟資料庫檔案使用sqlite3_open_v2替代sqlite3_open

sqlite3_open_v2(strDbName,sqlite_p, SQLITE_OPEN_READWRITE| SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX, NULL);

 

2. 使用sqlite3_busy_handler對SQLITE_BUSY狀態進行處理(必須)

參考:

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

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

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

我的修改:

static int callback_in_busy(void *ptr, int count){    int timeout = *((int *)ptr);        usleep(timeout);    return 1;}static void SqlGetLock(sqlite3 *sqlite_p, int ms){    if (ms > 0)    {        sqlite3_busy_handler(sqlite_p, callback_in_busy, (void*)&ms);    }    else    {        sqlite3_busy_handler(sqlite_p, 0, 0);    }}int SqlExec(sqlite3 *sqlite_p, const char *strSql){    char *pErrMsg = NULL;    int rc = SQLITE_OK;    int ret = -1;        SqlGetLock(sqlite_p, 200);        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;    }    return ret;}

 

sqlite:多線程操作資料庫“database is locked”解決方案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.