Concurrency

One weak spot with sqlite is with scaling and concurrency. Sqlite3 does have a few things to help with concurrency such as sqlite3_busy_timeout and sqlite3_busy_handler. After fiddling with the timeout feature for a while, it seemed that using the busy handler would allow for more control. Yet, even with a busy handler, sqlite may still return an error code. So along with concurrency, this version of Xql3 (Xql3.cpp) uses C++ exceptions via an exception class Xql3Exception (Xql3Exception.h) that returns both an error code as well as an error message.

A timer using nanosleep is in the sleeper test program (sleeper.cpp). Run lock (lock.cpp) to lock the sqlite database in order to test concurrency (lock X where X is how many times a second to lock/unlock the database). For fun, it's possible to even run multiple copies of lock at the same time with different parameters. While lock is running and the ":" (locked) and "." (unlocked) are being displayed, run concurrency_v1 (concurrency_v1.cpp). Although most of the time the busy detection will hit a busy/retry before all the sql statements and then be able to run them all before another busy is detected, the output below shows a busy occurring while trying to step the first statement and then again while trying to step the third statement. The second output is when the retry count was reduced from the hard coded 200 to 2, to test that an error code would be returned with the thrown execption.

Browse all code for concurrency.


Xql3::busy() --> 0
Xql3::busy() --> 1
step rc=(101) --> not an error
step rc=(101) --> not an error
Xql3::busy() --> 0
Xql3::busy() --> 1
Xql3::busy() --> 2
Xql3::busy() --> 3
step rc=(101) --> not an error
step rc=(101) --> not an error
step rc=(101) --> not an error
Xql4::callback()
Xql4::callback()
step rc=(101) --> not an error
rowid|text|int|time
1|sample v2b text1|2|2007-08-24 05:16:16
2|sample v2b text2|2|2007-08-24 05:16:16

Xql3::busy() --> 0
Xql3::busy() --> 1
Xql3::busy() --> 2
sqlite3_error: 5 --> database is locked
Xql3Exception: 5 --> step