@@ -209,9 +209,15 @@ pub struct PoolOpts {
209
209
constraints : PoolConstraints ,
210
210
inactive_connection_ttl : Duration ,
211
211
ttl_check_interval : Duration ,
212
+ reset_connection : bool ,
212
213
}
213
214
214
215
impl PoolOpts {
216
+ /// Calls `Self::default`.
217
+ pub fn new ( ) -> Self {
218
+ Self :: default ( )
219
+ }
220
+
215
221
/// Creates the default [`PoolOpts`] with the given constraints.
216
222
pub fn with_constraints ( mut self , constraints : PoolConstraints ) -> Self {
217
223
self . constraints = constraints;
@@ -223,6 +229,50 @@ impl PoolOpts {
223
229
self . constraints
224
230
}
225
231
232
+ /// Sets whether to reset connection upon returning it to a pool (defaults to `true`).
233
+ ///
234
+ /// Default behavior increases reliability but comes with cons:
235
+ ///
236
+ /// * reset procedure removes all prepared statements, i.e. kills prepared statements cache
237
+ /// * connection reset is quite fast but requires additional client-server roundtrip
238
+ /// (might also requires requthentication for older servers)
239
+ ///
240
+ /// The purpose of the reset procedure is to:
241
+ ///
242
+ /// * rollback any opened transactions (`mysql_async` is able to do this without explicit reset)
243
+ /// * reset transaction isolation level
244
+ /// * reset session variables
245
+ /// * delete user variables
246
+ /// * remove temporary tables
247
+ /// * remove all PREPARE statement (this action kills prepared statements cache)
248
+ ///
249
+ /// So to encrease overall performance you can safely opt-out of the default behavior
250
+ /// if you are not willing to change the session state in an unpleasant way.
251
+ ///
252
+ /// It is also possible to selectively opt-in/out using [`Conn::reset_connection`].
253
+ ///
254
+ /// # Connection URL
255
+ ///
256
+ /// You can use `reset_connection` URL parameter to set this value. E.g.
257
+ ///
258
+ /// ```
259
+ /// # use mysql_async::*;
260
+ /// # use std::time::Duration;
261
+ /// # fn main() -> Result<()> {
262
+ /// let opts = Opts::from_url("mysql://localhost/db?reset_connection=false")?;
263
+ /// assert_eq!(opts.pool_opts().reset_connection(), false);
264
+ /// # Ok(()) }
265
+ /// ```
266
+ pub fn with_reset_connection ( mut self , reset_connection : bool ) -> Self {
267
+ self . reset_connection = reset_connection;
268
+ self
269
+ }
270
+
271
+ /// Returns the `reset_connection` value (see [`PoolOpts::with_reset_connection`]).
272
+ pub fn reset_connection ( & self ) -> bool {
273
+ self . reset_connection
274
+ }
275
+
226
276
/// Pool will recycle inactive connection if it is outside of the lower bound of the pool
227
277
/// and if it is idling longer than this value (defaults to
228
278
/// [`DEFAULT_INACTIVE_CONNECTION_TTL`]).
@@ -309,6 +359,7 @@ impl Default for PoolOpts {
309
359
constraints : DEFAULT_POOL_CONSTRAINTS ,
310
360
inactive_connection_ttl : DEFAULT_INACTIVE_CONNECTION_TTL ,
311
361
ttl_check_interval : DEFAULT_TTL_CHECK_INTERVAL ,
362
+ reset_connection : true ,
312
363
}
313
364
}
314
365
}
@@ -1340,7 +1391,6 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1340
1391
Ok ( value) => {
1341
1392
opts. pool_opts = opts
1342
1393
. pool_opts
1343
- . clone ( )
1344
1394
. with_inactive_connection_ttl ( Duration :: from_secs ( value) )
1345
1395
}
1346
1396
_ => {
@@ -1355,7 +1405,6 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1355
1405
Ok ( value) => {
1356
1406
opts. pool_opts = opts
1357
1407
. pool_opts
1358
- . clone ( )
1359
1408
. with_ttl_check_interval ( Duration :: from_secs ( value) )
1360
1409
}
1361
1410
_ => {
@@ -1421,6 +1470,16 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1421
1470
} ) ;
1422
1471
}
1423
1472
}
1473
+ } else if key == "reset_connection" {
1474
+ match bool:: from_str ( & * value) {
1475
+ Ok ( parsed) => opts. pool_opts = opts. pool_opts . with_reset_connection ( parsed) ,
1476
+ Err ( _) => {
1477
+ return Err ( UrlError :: InvalidParamValue {
1478
+ param : key. to_string ( ) ,
1479
+ value,
1480
+ } ) ;
1481
+ }
1482
+ }
1424
1483
} else if key == "tcp_nodelay" {
1425
1484
match bool:: from_str ( & * value) {
1426
1485
Ok ( value) => opts. tcp_nodelay = value,
@@ -1538,7 +1597,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1538
1597
}
1539
1598
1540
1599
if let Some ( pool_constraints) = PoolConstraints :: new ( pool_min, pool_max) {
1541
- opts. pool_opts = opts. pool_opts . clone ( ) . with_constraints ( pool_constraints) ;
1600
+ opts. pool_opts = opts. pool_opts . with_constraints ( pool_constraints) ;
1542
1601
} else {
1543
1602
return Err ( UrlError :: InvalidPoolConstraints {
1544
1603
min : pool_min,
0 commit comments