两个函数同时访问数据库

two functions accessing database at the same time

本文关键字:访问 数据库 函数 两个      更新时间:2023-10-16

我有函数A和函数B。

函数A在循环中运行。每6秒更新一次数据库。

函数B在用户更改内容时更新数据库。

在长时间运行期间,这两个函数试图同时访问数据库,导致sqlite崩溃。

请建议一个避免这种情况的方法。

下面是函数B

while(1)
    {
        tvAudioMgrInstance->updateDatabase();
        if(errno != EINTR)
        {
            // In Android sleep() function takes argument as seconds
            rc = sleep(PERIODIC_UPDATE_DATABASE_TIME);
        }
        if((rc != 0)||(errno == EINTR))//even checking errno alone is enough..as errno is global to the thread alone
        {
            tvAudioMgrInstance->updateDatabase();
#if TVAUDIOMANAGER_LOG_ENABLE           
            ALOGD("Exit AUDMGR Pthread");
#endif
            break;
        }
    }

如果在这些函数之间共享一个helper实例,则所有数据库访问代码都是串行的。

检查:http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection