You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sets up the connection pooling. Creates `ENV['MYSQL_START_POOL_SIZE']``Mysql2::Client` instances up front. This is provided as a separate method to allow for use within process forking where connections would need to be created after forking the process.
167
+
Sets up connection pooling using `connection_pool` with `ENV['MYSQL_MAX_POOL_SIZE']`and `ENV['MYSQL_POOL_TIMEOUT']`. Connections are created lazily by the pool when first needed.
165
168
166
169
```ruby
167
170
connector.setup
168
171
```
169
172
170
173
#### #dispose
171
174
172
-
Closes all the `Mysql2::Client` connections and removes the connection pool. Intended as a clean-up method to be used on process fork shutdown.
175
+
Closes pooled `Mysql2::Client` connections and removes the pool. Intended as a clean-up method to be used on process fork shutdown.
173
176
174
177
```ruby
175
178
connector.dispose
176
179
```
177
180
178
181
#### #check_out
179
182
180
-
Check out a client from the connection pool. Will create new `Mysql2::Client` instances up-to `ENV['MYSQL_MAX_POOL_SIZE']` times if no idle connections are available.
183
+
Checks out a `Mysql2::Client` instance from the pool, sanitizes it, and returns it.
184
+
When pooling is disabled, it returns a newly created client.
181
185
182
186
```ruby
183
187
client = connector.check_out
184
188
```
185
189
186
190
#### #check_in
187
191
188
-
Check in a client to the connection pool
192
+
Checks a client back in to the pool.
193
+
When pooling is disabled, it closes the provided client.
189
194
190
195
```ruby
191
196
client = connector.check_out
@@ -195,7 +200,17 @@ connector.check_in(client)
195
200
196
201
#### #with_client
197
202
198
-
Called with a block. The method checks out a client from the pool and yields it to the block. Finally it ensures that the client is always checked back into the pool.
203
+
Called with a block. The method obtains a client (from the pool when enabled), yields it to the block, and guarantees cleanup.
204
+
205
+
When pooling is enabled, it uses the pool lifecycle (`ConnectionPool#with`) and supports optional discarding of the current pooled connection:
206
+
207
+
```ruby
208
+
connector.with_client(discard_current_pool_connection:true) do |client|
209
+
# use client
210
+
end
211
+
```
212
+
213
+
When pooling is disabled, it creates a fresh client for the block and closes it afterwards.
199
214
200
215
```ruby
201
216
connector.with_client do |client|
@@ -205,6 +220,17 @@ connector.with_client do |client|
205
220
end
206
221
```
207
222
223
+
**Warning: re-entrant connections within the same thread**
224
+
225
+
The `connection_pool` gem implements thread-local connection tracking. When a thread already holds a connection via `with_client` (or `check_out`), any nested call to `with_client` or `check_out` on the **same thread** returns the **same connection** — it does not check out a second one from the pool.
226
+
227
+
This means that if you fire an async query on a connection and then attempt to run a second query (e.g. via `run_query` or `connector.query`) from within the same `with_client` block, the nested call will receive the already-checked-out connection. Sanitization will then fail with:
228
+
229
+
```
230
+
Connection sanitization failed: This connection is still waiting for a result,
231
+
try again once you have the result
232
+
```
233
+
208
234
It can optionally accept an existing client to avoid starting new connections in the middle of a transaction. This can be used to ensure that a series of queries are wrapped by the same transaction.
209
235
210
236
```ruby
@@ -280,10 +306,65 @@ The default options used to initialise MySQL2::Client instances:
On shutdown, stop the publisher before disposing the connector:
333
+
334
+
```ruby
335
+
publisher.stop
336
+
connector.dispose
337
+
```
338
+
339
+
#### Customising CloudWatch dimensions and namespace
340
+
341
+
Use `MysqlFramework::Stats::DimensionMap` to configure the CloudWatch namespace and dimensions. Each attribute falls back to the corresponding environment variable when not set explicitly:
0 commit comments