@@ -15,19 +15,24 @@ void handle_fetch1000(boost::shared_ptr<postgres_asio::connection> connection, s
1515
1616 int tuples_in_batch = PQntuples (res.get ());
1717 total_count += tuples_in_batch;
18- std::cerr << " got " << tuples_in_batch << " , total: " << total_count << std::endl ;
18+ BOOST_LOG_TRIVIAL (info) << " got " << tuples_in_batch << " , total: " << total_count;
1919 if (tuples_in_batch == 0 )
2020 {
21- connection->exec (" CLOSE mycursor; COMMIT" , [connection](int ec, boost::shared_ptr<PGresult> res) { });
21+ BOOST_LOG_TRIVIAL (info) << " calling async_exec CLOSE mycursor; COMMIT...." ;
22+ connection->exec (" CLOSE mycursor; COMMIT" , [connection](int ec, boost::shared_ptr<PGresult> res)
23+ {
24+ BOOST_LOG_TRIVIAL (info) << " async processing done" ;
25+ });
2226 return ;
2327 }
28+ BOOST_LOG_TRIVIAL (info) << " calling async_exec FETCH 1000 in mycursor...." ;
2429 connection->exec (" FETCH 1000 in mycursor" , [connection, total_count](int ec, boost::shared_ptr<PGresult> res) { handle_fetch1000 (connection, total_count, ec, std::move (res)); });
2530}
2631
2732int main (int argc, char *argv[])
2833{
2934 std::string host;
30- boost::log::core::get ()->set_filter (boost::log::trivial::severity >= boost::log::trivial::info );
35+ boost::log::core::get ()->set_filter (boost::log::trivial::severity >= boost::log::trivial::debug );
3136 std::string connect_string = " user=postgres password=postgres dbname=test" ;
3237
3338 if (argc > 1 )
@@ -37,8 +42,8 @@ int main(int argc, char *argv[])
3742
3843 boost::asio::io_service fg_ios;
3944 boost::asio::io_service bg_ios;
40- std::auto_ptr<boost::asio::io_service::work> work2 (new boost::asio::io_service::work (fg_ios));
41- std::auto_ptr<boost::asio::io_service::work> work1 (new boost::asio::io_service::work (bg_ios));
45+ std::auto_ptr<boost::asio::io_service::work> fg_work (new boost::asio::io_service::work (fg_ios)); // this keeps the fg_ios alive
46+ std::auto_ptr<boost::asio::io_service::work> bg_work (new boost::asio::io_service::work (bg_ios)); // this keeps the bg_ios alive
4247 boost::thread fg (boost::bind (&boost::asio::io_service::run, &fg_ios));
4348 boost::thread bg (boost::bind (&boost::asio::io_service::run, &bg_ios));
4449
@@ -48,35 +53,36 @@ int main(int argc, char *argv[])
4853 {
4954 if (!ec)
5055 {
56+ BOOST_LOG_TRIVIAL (info) << " calling async_exec BEGIN" ;
5157 connection->exec (" BEGIN" , [connection](int ec, boost::shared_ptr<PGresult> res)
5258 {
5359 if (ec)
5460 {
55- std::cerr << " BEGIN failed ec:" << ec << " last_error: " << connection->last_error () << std::endl ;
61+ BOOST_LOG_TRIVIAL (error) << " BEGIN failed ec:" << ec << " last_error: " << connection->last_error ();
5662 return ;
5763 }
64+ BOOST_LOG_TRIVIAL (info) << " calling async_exec DECLARE mycursor...." ;
5865 connection->exec (" DECLARE mycursor CURSOR FOR SELECT * from postgres_asio_sample" , [connection](int ec, boost::shared_ptr<PGresult> res)
5966 {
6067 if (ec)
6168 {
62- std::cerr << " DECLARE mycursor... failed ec:" << ec << " last_error: " << connection->last_error () << std::endl ;
69+ BOOST_LOG_TRIVIAL (error) << " DECLARE mycursor... failed ec:" << ec << " last_error: " << connection->last_error ();
6370 return ;
6471 }
72+ BOOST_LOG_TRIVIAL (info) << " calling async_exec FETCH 1000 in mycursor...." ;
6573 connection->exec (" FETCH 1000 in mycursor" , [connection](int ec, boost::shared_ptr<PGresult> res){ handle_fetch1000 (connection, 0 , ec, std::move (res)); });
6674 });
6775 });
6876 }
6977 });
7078
71- while ( true )
72- {
73- boost::this_thread::sleep ( boost::posix_time::milliseconds ( 1000 )) ;
74- }
79+ BOOST_LOG_TRIVIAL (debug) << " work reset " ;
80+ bg_work. reset ();
81+ BOOST_LOG_TRIVIAL (debug) << " bg join " ;
82+ bg. join ();
7583
76- work1.reset ();
77- work2.reset ();
78- // bg_ios.stop();
79- // fg_ios.stop();
80- bg.join ();
81- fg.join ();
84+ fg_work.reset ();
85+ BOOST_LOG_TRIVIAL (debug) << " fg join" ;
86+ fg.join ();
87+ BOOST_LOG_TRIVIAL (debug) << " done" ;
8288}
0 commit comments