@@ -209,9 +209,15 @@ pub struct PoolOpts {
209209 constraints : PoolConstraints ,
210210 inactive_connection_ttl : Duration ,
211211 ttl_check_interval : Duration ,
212+ reset_connection : bool ,
212213}
213214
214215impl PoolOpts {
216+ /// Calls `Self::default`.
217+ pub fn new ( ) -> Self {
218+ Self :: default ( )
219+ }
220+
215221 /// Creates the default [`PoolOpts`] with the given constraints.
216222 pub fn with_constraints ( mut self , constraints : PoolConstraints ) -> Self {
217223 self . constraints = constraints;
@@ -223,6 +229,50 @@ impl PoolOpts {
223229 self . constraints
224230 }
225231
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+
226276 /// Pool will recycle inactive connection if it is outside of the lower bound of the pool
227277 /// and if it is idling longer than this value (defaults to
228278 /// [`DEFAULT_INACTIVE_CONNECTION_TTL`]).
@@ -309,6 +359,7 @@ impl Default for PoolOpts {
309359 constraints : DEFAULT_POOL_CONSTRAINTS ,
310360 inactive_connection_ttl : DEFAULT_INACTIVE_CONNECTION_TTL ,
311361 ttl_check_interval : DEFAULT_TTL_CHECK_INTERVAL ,
362+ reset_connection : true ,
312363 }
313364 }
314365}
@@ -1340,7 +1391,6 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
13401391 Ok ( value) => {
13411392 opts. pool_opts = opts
13421393 . pool_opts
1343- . clone ( )
13441394 . with_inactive_connection_ttl ( Duration :: from_secs ( value) )
13451395 }
13461396 _ => {
@@ -1355,7 +1405,6 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
13551405 Ok ( value) => {
13561406 opts. pool_opts = opts
13571407 . pool_opts
1358- . clone ( )
13591408 . with_ttl_check_interval ( Duration :: from_secs ( value) )
13601409 }
13611410 _ => {
@@ -1421,6 +1470,16 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
14211470 } ) ;
14221471 }
14231472 }
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+ }
14241483 } else if key == "tcp_nodelay" {
14251484 match bool:: from_str ( & * value) {
14261485 Ok ( value) => opts. tcp_nodelay = value,
@@ -1538,7 +1597,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
15381597 }
15391598
15401599 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) ;
15421601 } else {
15431602 return Err ( UrlError :: InvalidPoolConstraints {
15441603 min : pool_min,
0 commit comments