|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2026, Vesselin Atanasov |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 18 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 19 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 20 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 22 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 23 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 24 | + * POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +// A sample program demonstrating how to create and use a thread-safe connection |
| 28 | +// using a lazy database connection, connection pool and thread-local storage. |
| 29 | +// |
| 30 | +// For details on the actual pattern see |
| 31 | +// /docs/recipes/thread_local_connection.md |
| 32 | + |
| 33 | +#include <optional> |
| 34 | +#include <thread> |
| 35 | +#include <vector> |
| 36 | + |
| 37 | +#include <sqlpp23/tests/postgresql/all.h> |
| 38 | + |
| 39 | +namespace sql = ::sqlpp::postgresql; |
| 40 | + |
| 41 | +// This is our main database connection class. It mimics a regular database |
| 42 | +// connection, while delegating the execution of SQL queries to an underlying |
| 43 | +// sqlpp23 database connection. This underlying database connections is not |
| 44 | +// created immediately upon construction of our lazy connection. Instead the |
| 45 | +// underlying database connection is created the first time when the user |
| 46 | +// tries to execute a database query through operator(). |
| 47 | +// |
| 48 | +// The class constructor received a reference to a connection pool, which later |
| 49 | +// is used to get the underlying database connection. When the constructor is |
| 50 | +// called, the connection pool does not have to be fully initialized, because |
| 51 | +// the constructor does not use the connection pool and just stores the |
| 52 | +// reference to it for later use. |
| 53 | +// |
| 54 | +class lazy_connection { |
| 55 | + private: |
| 56 | + sql::connection_pool& _pool; |
| 57 | + std::optional<sql::pooled_connection> _dbc; |
| 58 | + |
| 59 | + public: |
| 60 | + lazy_connection(sql::connection_pool& pool) : _pool{pool}, _dbc{} {} |
| 61 | + lazy_connection(const lazy_connection&) = delete; |
| 62 | + lazy_connection(lazy_connection&&) = delete; |
| 63 | + |
| 64 | + lazy_connection& operator=(const lazy_connection&) = delete; |
| 65 | + lazy_connection& operator=(lazy_connection&&) = delete; |
| 66 | + |
| 67 | + // Delegate to _dbc any methods of sql::connection that you may need |
| 68 | + // In our example the only delegated method is operator() |
| 69 | + |
| 70 | + template <typename T> |
| 71 | + auto operator()(const T& t) { |
| 72 | + if (!_dbc) { |
| 73 | + _dbc = _pool.get(); |
| 74 | + } |
| 75 | + return (*_dbc)(t); |
| 76 | + } |
| 77 | +}; |
| 78 | + |
| 79 | +sql::connection_pool g_pool{}; |
| 80 | + |
| 81 | +// This is our lazy connection object, which we use to execute SQL queries. |
| 82 | +// It is marked with the thread_local storage class specifier, which means |
| 83 | +// that the C++ runtime creates one instance of the object per thread, each |
| 84 | +// instance having its own underlying database connection. |
| 85 | +// |
| 86 | +// We don't really care about the order in which the connection pool and |
| 87 | +// the global connection object are initialized because, as described above, |
| 88 | +// the constructor of the lazy connection only stores a reference to the pool |
| 89 | +// without actually using it. |
| 90 | +// |
| 91 | +thread_local lazy_connection g_dbc{g_pool}; |
| 92 | + |
| 93 | +int main() { |
| 94 | + const int num_threads = 5; |
| 95 | + const int num_queries = 10; |
| 96 | + |
| 97 | + // Initialize the global connection pool |
| 98 | + g_pool.initialize(sql::make_test_config(), num_threads); |
| 99 | + |
| 100 | + test::createTabBar(g_dbc); |
| 101 | + test::TabBar tb{}; |
| 102 | + // Spawn the threads and make each thread execute multiple SQL queries |
| 103 | + std::vector<std::thread> threads{}; |
| 104 | + for (int i = 0; i < num_threads; ++i) { |
| 105 | + threads.push_back(std::thread([&]() { |
| 106 | + for (int j = 0; j < num_queries; ++j) { |
| 107 | + // For simplicity we don't begin/commit a transaction explicitly. |
| 108 | + // Instead we use the database autocommit mode in which the database |
| 109 | + // engine wraps each quety in its own transaction. |
| 110 | + // |
| 111 | + g_dbc(insert_into(tb).set(tb.intN = i * j)); |
| 112 | + } |
| 113 | + })); |
| 114 | + } |
| 115 | + for (auto&& t : threads) { |
| 116 | + t.join(); |
| 117 | + } |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
0 commit comments