Databricks C++ SDK 0.2.4
Interact with Databricks via an SDK
Loading...
Searching...
No Matches
connection_pool.h
Go to the documentation of this file.
1#pragma once
2
4#include <memory>
5#include <vector>
6#include <mutex>
7#include <condition_variable>
8#include <queue>
9#include <future>
10
11// Forward declare Client to avoid circular dependency
12namespace databricks {
13 class Client;
14}
15
16namespace databricks
17{
18 /**
19 * @brief Thread-safe connection pool for managing Databricks connections
20 *
21 * **ADVANCED API**: Most users should enable pooling via PoolingConfig instead.
22 *
23 * This class is for advanced users who need fine-grained control over connection
24 * pooling. For typical use cases, simply set `pooling.enabled = true` when
25 * building a Client, and pooling will be handled automatically.
26 *
27 * The ConnectionPool manages a pool of reusable ODBC connections to improve
28 * performance by eliminating connection overhead for repeated operations.
29 */
31 {
32 public:
33 /**
34 * @brief RAII wrapper for a pooled connection
35 *
36 * Automatically returns the connection to the pool when destroyed.
37 */
39 {
40 public:
41 PooledConnection(std::unique_ptr<Client> client, ConnectionPool *pool);
43
44 // Disable copy
47
48 // Enable move
50 PooledConnection &operator=(PooledConnection &&) noexcept;
51
52 /**
53 * @brief Get the underlying client
54 * @return Reference to the Client
55 */
57
58 /**
59 * @brief Get the underlying client (const version)
60 * @return Const reference to the Client
61 */
62 const Client &get() const;
63
64 /**
65 * @brief Access the client via pointer semantics
66 */
67 Client *operator->();
68
69 /**
70 * @brief Access the client via pointer semantics (const)
71 */
72 const Client *operator->() const;
73
74 private:
75 std::unique_ptr<Client> client_;
76 ConnectionPool *pool_;
77 };
78
79 /**
80 * @brief Construct a ConnectionPool with modular configurations
81 * @param auth Authentication configuration
82 * @param sql SQL configuration
83 * @param min_connections Minimum connections to maintain
84 * @param max_connections Maximum connections allowed
85 */
87 const SQLConfig& sql,
88 size_t min_connections = 1,
89 size_t max_connections = 10);
90
91 /**
92 * @brief Destructor - closes all connections
93 */
95
96 // Disable copy
97 ConnectionPool(const ConnectionPool &) = delete;
98 ConnectionPool &operator=(const ConnectionPool &) = delete;
99
100 // Disable move (pool should be a long-lived object)
102 ConnectionPool &operator=(ConnectionPool &&) = delete;
103
104 /**
105 * @brief Acquire a connection from the pool
106 *
107 * If no connections are available and the pool is not at max capacity,
108 * a new connection is created. Otherwise, waits for a connection to
109 * become available (up to connection_timeout_ms).
110 *
111 * @return PooledConnection RAII wrapper
112 * @throws std::runtime_error if timeout occurs or connection fails
113 */
115
116 /**
117 * @brief Pre-warm the pool by creating minimum connections
118 *
119 * Creates min_connections in the pool without blocking. Useful for
120 * reducing initial latency.
121 */
122 void warm_up();
123
124 /**
125 * @brief Asynchronously pre-warm the pool
126 *
127 * Creates min_connections in the background.
128 *
129 * @return std::future<void> Future that completes when warm-up is done
130 */
131 std::future<void> warm_up_async();
132
133 /**
134 * @brief Get current pool statistics
135 */
136 struct Stats
137 {
138 size_t total_connections; ///< Total connections created
139 size_t available_connections; ///< Connections currently available
140 size_t active_connections; ///< Connections currently in use
141 };
142
143 /**
144 * @brief Get current pool statistics
145 * @return Stats structure with current pool state
146 */
148
149 /**
150 * @brief Shutdown the pool and close all connections
151 */
152 void shutdown();
153
154 private:
155 AuthConfig auth_;
156 SQLConfig sql_;
157 size_t min_connections_;
158 size_t max_connections_;
159 int connection_timeout_ms_;
160
161 std::queue<std::unique_ptr<Client>> available_connections_;
162 size_t total_connections_;
163 size_t active_connections_;
164 bool shutdown_;
165
166 mutable std::mutex mutex_;
167 std::condition_variable cv_;
168
169 /**
170 * @brief Create a new connection (must be called with mutex held)
171 */
172 std::unique_ptr<Client> create_connection();
173
174 /**
175 * @brief Return a connection to the pool
176 */
177 void return_connection(std::unique_ptr<Client> client);
178
179 friend class PooledConnection;
180 };
181
182} // namespace databricks
Main client class for Databricks SQL operations.
Definition client.h:55
RAII wrapper for a pooled connection.
PooledConnection(PooledConnection &&) noexcept
PooledConnection(std::unique_ptr< Client > client, ConnectionPool *pool)
PooledConnection(const PooledConnection &)=delete
Client & get()
Get the underlying client.
PooledConnection & operator=(const PooledConnection &)=delete
Thread-safe connection pool for managing Databricks connections.
void warm_up()
Pre-warm the pool by creating minimum connections.
Stats get_stats() const
Get current pool statistics.
std::future< void > warm_up_async()
Asynchronously pre-warm the pool.
PooledConnection acquire()
Acquire a connection from the pool.
void shutdown()
Shutdown the pool and close all connections.
Core authentication configuration shared across all Databricks features.
Definition config.h:16
Get current pool statistics.
size_t total_connections
Total connections created.
size_t active_connections
Connections currently in use.
size_t available_connections
Connections currently available.
SQL-specific configuration for Databricks SQL operations.
Definition config.h:72