-
-
Notifications
You must be signed in to change notification settings - Fork 85
Description
In some cases, specific initialization/cleanup needs to be done around managed connections when fetching them from the pool and releasing them to the pool.
e.g.:
- begin_test_transaction behavior changed in Diesel 2.0 diesel-rs/diesel#3252 with solution suggested here
- Attempting to close any open transaction on release-to-pool (solution where transactions are generally managed with RAII as suggested here have the issues described here).
Currently this requires having custom wrappers around PooledConnection and Pool, which prevents from always using r2d2 directly (and requires reimplementing a whole bunch of traits on the wrappers).
Something that would remove this need would be to add on_take_from_pool and on_release_to_pool hooks to CustomizeConnection (or another trait if we want backwards-compat.) and/or ManageConnection.
That would solve 1. by allowing users to automatically open a test transaction on_take_from_pool and rolling it back on_release_to_pool (on_release_to_pool would have to be called before is_valid).
That would solve 2. by allowing users (and/or implementors of ManageConnection) to automatically attempt closing any open transactions on_release_to_pool in any way they see fit.
I would imagine the overhead of the CustomizeConnection one to be really-not-too-high despite the dynamic dispatch because that would be absolutely negligible compared with what we actually do with the connection. (And obviously the overhead of the ManageConnection one would be zero because the statically dispatched no-op would be optimized out.)
Would that be something that makes sense and could potentially be added to r2d2?
Thanks,