54static void worker(
int thread_id,
const std::string& connection_string)
57 auto context = std::make_shared<database_context>();
58 auto db = std::make_shared<database_manager>(context);
59 db->set_mode(database_types::postgres);
61 auto connect_result = db->connect_result(connection_string);
62 if (!connect_result.is_ok())
70 std::string insert_sql
71 =
"INSERT INTO pool_demo (thread_id, message) VALUES ("
72 + std::to_string(thread_id) +
", 'Hello from thread "
73 + std::to_string(thread_id) +
"')";
75 auto insert_result = db->execute_query_result(insert_sql);
76 if (insert_result.is_ok())
86 auto select_result = db->select_query_result(
87 "SELECT thread_id, message FROM pool_demo ORDER BY thread_id");
88 if (select_result.is_ok())
91 "Read " + std::to_string(select_result.value().size()) +
" row(s)");
94 db->disconnect_result();
100 std::cout <<
"=== connection_pool_demo example ===" << std::endl;
102 std::string connection_string
103 =
"host=localhost port=5432 dbname=example_db user=user password=password";
107 auto context = std::make_shared<database_context>();
108 auto db = std::make_shared<database_manager>(context);
109 db->set_mode(database_types::postgres);
111 auto connect_result = db->connect_result(connection_string);
112 if (!connect_result.is_ok())
114 std::cerr <<
"Setup connection failed. Ensure PostgreSQL is running."
119 db->create_query_result(R
"(
120 CREATE TABLE IF NOT EXISTS pool_demo (
121 id SERIAL PRIMARY KEY,
122 thread_id INTEGER NOT NULL,
126 db->execute_query_result("DELETE FROM pool_demo");
127 db->disconnect_result();
128 std::cout <<
"Setup complete" << std::endl;
132 const int num_threads = 4;
133 std::vector<std::thread> threads;
134 threads.reserve(num_threads);
136 std::cout <<
"\nLaunching " << num_threads <<
" worker threads..." << std::endl;
138 for (
int i = 0; i < num_threads; ++i)
140 threads.emplace_back(
worker, i, connection_string);
144 for (
auto& t : threads)
148 std::cout <<
"\nAll threads completed" << std::endl;
152 auto context = std::make_shared<database_context>();
153 auto db = std::make_shared<database_manager>(context);
154 db->set_mode(database_types::postgres);
156 auto connect_result = db->connect_result(connection_string);
157 if (connect_result.is_ok())
159 auto result = db->select_query_result(
160 "SELECT thread_id, message FROM pool_demo ORDER BY thread_id");
163 std::cout <<
"\nFinal table contents (" << result.value().size()
164 <<
" rows):" << std::endl;
165 for (
const auto& row : result.value())
167 for (
const auto& [col, val] : row)
169 std::cout <<
" " << col <<
" = ";
170 std::visit([](
const auto& v) { std::cout << v; }, val);
173 std::cout << std::endl;
176 db->disconnect_result();
180 std::cout <<
"\n=== connection_pool_demo example completed ===" << std::endl;
static void worker(int thread_id, const std::string &connection_string)
Worker function: creates its own connection, inserts data, reads it back, and disconnects.
Dependency injection container for database system components.