I would like to write with several processes in a SQLite database. Here is my c++ code:
stringstream sstream << "BEGIN;" << query << "COMMIT;";
sqlite3_busy_timeout(databasePtr, 60000); // set timeout if sql busy
if((result = sqlite3_exec(databasePtr, (sstream.str()).c_str(), NULL, NULL, NULL)) != SQLITE_OK){
/** ERROR or SQLITE_BUSY **/
}
sqlite3_busy_timeout(databasePtr, 0); // reset sql_busy handler
I thought that the sqlite3_busy_timeout leads to success. But I've checked the results and found that not all data is written to the database. Where is my mistake?
Does someone knows how often sqlite3_exec is called when the sqlite3_busy_ti开发者_StackOverflow社区meout is set to 60000 (ms)? Or is there only one call after 60000 (ms) if the first try returns with SQLITE_BUSY?
I've tried to fix the problem with the following code. But it looks like there is only one active process all the time. The other processes will not finished...
do{
if((result = sqlite3_exec(databasePtr, (sstream.str()).c_str(), NULL, NULL, NULL)) != SQLITE_OK){
if(result == SQLITE_BUSY){
sleep(60000); // sleep 10 sec
}else{
/** ERROR **/
}
}
}while(result == SQLITE_BUSY);
Default busy handler installed by sqlite3_busy_timeout (sqliteDefaultBusyCallback() in src/main.c) sleeps for 1ms and then tries again, then for 2ms, 5ms... until total time of sleeping exceeds your specified timeout value.
So your fix essentially replicates what is done internally, but with unusably long wait time (argument of sleep() is in seconds, so your code sleeps for like 18 hours). By the way, even 60000ms = 60s seems like too much for timeout as in sqlite3_busy_timeout, not to mention for back-off time between retries.
精彩评论