[odb-users] query crash

Патрушев Данил Андреевич d.patrushev at prosoftsystems.ru
Fri Nov 3 02:11:44 EDT 2017

> 1) create a query object in thread number 1
> 2) create a functor (say, lambda) containing the code to execute a database request and copy the query object into the functor
> 3) launch a worker thread and call the functor within that thread's context.
> 4) pass the result back to thread number 1
> Is it safe to use odb:: pgsql::query in this manner?

>Probably not. To be more precise I would need more details (maybe code
>samples for each step). For example, what is a "query object"? A prepared
>query or just the query condition?
>Also, what's happening with transactions/connections during all of this?
>Are you passing them as well or create new ones in respective threads
>(the latter would definitely be a bad idea since the result is "bound"
>to the connection of the query).

Hi, Boris. I'll elaborate.
We have a single instance of odb::pgsql::database. We have a custom connection pool, all it does is override the "create" method which creates a connection with the socket options that we need.

1) odb::query<MYTABLE> q(MYTABLE::rowid == 555); // create query in main thread
2) auto functor = [q](){//copy query into the lambda
odb::database* db = database(); //single instance
odb::transaction tran(db->begin());
auto result = db->query<MYTABLE>(q);
//iterate over result and construct a list of db objects;
return list_of_db_objects;

See, by result I mean a list of objects extracted from odb::result<MYTABLE>;

3) auto future = QtConcurrent::run(functor); //(I quess you are familiar with the Qt framework)
We  launch a worker thread and wait for the finished signal (the original query gets destroyed along the way)

4) The functor gets executed in the worker thread's context . After the finished signal is emitted, we extract the result from the future by calling future->result() in main thread;

I quess that if odb::pgsql::query class is not thread-safe, then we are wrong to use it this way .

BTW, in case I have broken any formatting conventions (I guess, I have), please, show me where I can read about them. Thank you.

More information about the odb-users mailing list