0

I have function A and function B .

Function A runs in loop. updates the data base every 6 seconds.

Function B updates the database when user changes something .

During the long run this two functions are trying to access the database at the same causing in the sqlite crash.

Please suggest a way to avoid this.

Below is my function 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;
        }
    }
  • Some code would be nice? – Arion May 29 '15 at 13:56
  • maybe that helps a bit http://stackoverflow.com/questions/4060772/sqlite3-concurrent-access – tanaydin May 29 '15 at 13:57
  • That is some good information. My functions does the write operations . ideally sqlite write operation should be locked but yet this crash occurs. I am thinking of avoiding the function call by using some flag ? will that be good.? – user3374737 May 29 '15 at 14:04
  • If nothing else helps, I would use a worker thread with a Handler/Looper, posting Runnable-s performing the DB write operations to that thread. – 18446744073709551615 May 29 '15 at 14:25

1 Answers1

0

If you use one helper instance shared between these functions, all of your db access code is serial.

Check this: http://touchlabblog.tumblr.com/post/24474750219/single-sqlite-connection

lean
  • 194
  • 2
  • 14