@@ -15,19 +15,24 @@ void handle_fetch1000(boost::shared_ptr<postgres_asio::connection> connection, s
15
15
16
16
int tuples_in_batch = PQntuples (res.get ());
17
17
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;
19
19
if (tuples_in_batch == 0 )
20
20
{
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
+ });
22
26
return ;
23
27
}
28
+ BOOST_LOG_TRIVIAL (info) << " calling async_exec FETCH 1000 in mycursor...." ;
24
29
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)); });
25
30
}
26
31
27
32
int main (int argc, char *argv[])
28
33
{
29
34
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 );
31
36
std::string connect_string = " user=postgres password=postgres dbname=test" ;
32
37
33
38
if (argc > 1 )
@@ -37,8 +42,8 @@ int main(int argc, char *argv[])
37
42
38
43
boost::asio::io_service fg_ios;
39
44
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
42
47
boost::thread fg (boost::bind (&boost::asio::io_service::run, &fg_ios));
43
48
boost::thread bg (boost::bind (&boost::asio::io_service::run, &bg_ios));
44
49
@@ -48,35 +53,36 @@ int main(int argc, char *argv[])
48
53
{
49
54
if (!ec)
50
55
{
56
+ BOOST_LOG_TRIVIAL (info) << " calling async_exec BEGIN" ;
51
57
connection->exec (" BEGIN" , [connection](int ec, boost::shared_ptr<PGresult> res)
52
58
{
53
59
if (ec)
54
60
{
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 ();
56
62
return ;
57
63
}
64
+ BOOST_LOG_TRIVIAL (info) << " calling async_exec DECLARE mycursor...." ;
58
65
connection->exec (" DECLARE mycursor CURSOR FOR SELECT * from postgres_asio_sample" , [connection](int ec, boost::shared_ptr<PGresult> res)
59
66
{
60
67
if (ec)
61
68
{
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 ();
63
70
return ;
64
71
}
72
+ BOOST_LOG_TRIVIAL (info) << " calling async_exec FETCH 1000 in mycursor...." ;
65
73
connection->exec (" FETCH 1000 in mycursor" , [connection](int ec, boost::shared_ptr<PGresult> res){ handle_fetch1000 (connection, 0 , ec, std::move (res)); });
66
74
});
67
75
});
68
76
}
69
77
});
70
78
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 ();
75
83
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" ;
82
88
}
0 commit comments