yandex / ozo

OZO is a C++17 Boost.Asio based header-only library for asyncronous communication with PostgreSQL DBMS.
PostgreSQL License
227 stars 46 forks source link

Synchronous database calls #251

Closed nobleread closed 4 years ago

nobleread commented 4 years ago

Obviously, ozo provides asynchronous API, but is there a way to execute some SQL in a blocking manner? I've tried to perform synchronous, i.e. blocking, call to database, but am not sure how to do that without using any completion handlers. What I did instead was to pass some completion handler to ozo and then call boost::asio's io_context.run_once(). Is there a way to use ozo without boost::asio maybe? Or provide some fake completion handler, that forces application to wait until db call is finished?

thed636 commented 4 years ago

Hi!

Obviously, ozo provides asynchronous API, but is there a way to execute some SQL in a blocking manner?

There are two ways:

  1. Calling io_context.run() like in this test right after calling ozo::request.
  2. Using boost::asio::use_future and running io_context.run() in a dedicated thread.

Is there a way to use ozo without boost::asio maybe?

You may try to call libpq functions directly in synchronous mode. The library entities provide access and can work with all the necessary native libpq handlers.

panmareksadowski commented 4 years ago

Hi!

Thanks, your response was helpful. We decided to use 1. way. I would just like to add that can be helpful for somebody, to mention that if the application is running asynchronously in one io_context and we need synchronous blocking database request we need do it in another io_context.