SQLite3 and threads
Vitalie Colosov via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Mar 1 21:09:24 PST 2015
Hi,
I am not able to query SQLite3 database files using threads;
without threads it is working fine.
I tried both etc.c.sqlite3 and d2sqlite3, and both seem to be
facing the same issue:
They stuck when executing a select query (using sqlite3_exec(...)
for etc.c.sqlite3 or using RowCache(db.execute(...)) in case of
d2sqlite3).
Since d2sqlite3 is a wrapper for native sqlite3, I think it faces
the same limitation which native sqlite does, so next lines will
describe native SQLite3 code.
---------------------------------------------------------------
This works fine (non-relevant code and validations are omitted
for simplicity)
---------------------------------------------------------------
import etc.c.sqlite3,...
...
extern(C) int myCallback(void *a_parm, int argc, char **argv,
char **column)
{
printf("%s\n", argv[1] ? argv[1] : "NULL"); // this prints
first column of each row, all is well
return 0;
}
void querySQLite(string dbName)
{
sqlite3* db;
auto ret = sqlite3_open(toStringz(dbName), &db);
string query = "SELECT * FROM my_table";
sqlite3_exec(db,toStringz(query),&myCallback,null,null);
sqlite3_close(db);
}
void main()
{
querySQLite("db1.sl3");
querySQLite("db2.sl3");
...// in fact, here is a foreach loop which is calling
querySQLite with about 30 database files
querySQLite("db30.sl3");
}
---------------------------------------------------------------
However, if I change main function to spawn querySQLite, instead
of calling it in sequence from the main thread,
then "myCallback()" is not executed.
void main()
{
spawn(&querySQLite,"db1.sl3");
spawn(&querySQLite,"db2.sl3");
...
spawn(&querySQLite,"db30.sl3");
}
It is stuck inside this line in querySQLite():
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
sqlite3_exec(db,toStringz(query),&myCallback,null,null);
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
If I comment it, the flow continues and returns fine from all
spawn-ed functions, so it is definitely something wrong in this
line.
---------------------------------------------------------------
I think I am missing some kind of thread locking code in
querySQLite() - since it is working with C code I think it needs
more attention.
I tried to compile SQLite with different multithreading options,
but that did not help.
Any advice is much appreciated.
Using dmd.2.066.1.linux
RedHat 5 64bit
Compiled using dmd
sqlite-amalgamation-3080803
Thanks,
Vitalie
More information about the Digitalmars-d-learn
mailing list