Open Ten0 opened 2 years ago
2. have the issues described here)
Those issues are described in the context of an async API, but r2d2 works with blocking connections.
Those issues are described in the context of an async API, but r2d2 works with blocking connections.
I think the points about "how would one handle connection errors in the Drop implementation?" (also mentionned here) are still valid in a synchronous context. (In addition, how do you make sure to never forget to make the commit call with a such API?)
Anway, that is just to justify that it isn't easy or even necessarily desirable to "just move to an RAII transaction API" that would dodge these transaction-management-on-pooled-connection-drop issues by other means.
(In addition, how do you make sure to never forget to make the commit call with a such API?)
How would you make sure to never forget a commit call in any API?
How would you make sure to never forget a commit call in any API?
Well Diesel's prevents that ^^
Well Diesel's prevents that ^^
I think that should be restricted to diesel::Connection::transaction
, as it is easily possible to just open a transaction via diesel::connection::TransactionManager::begin
without closing it later. Nevertheless I think it would be really helpful to have additional methods in CustomizeConnection
that allows users to perform actions on connection checkout/checkin. Maybe even allow to control modify the is_valid()
/has_broken()
functions there. As far as I can tell these methods can be added without breaking change, as we could add default implementations can just call the existing functions. If you are interested in this I can open a PR next week.
I am a bit confused as to how Diesel simultaneously guarantees that transactions will always be committed and needs the ability to forcibly roll back transactions when a pooled connection is returned.
In any case though, it seems reasonable to add those methods to CustomizeConnection.
I am a bit confused as to how Diesel simultaneously guarantees
I realize my previous messages may have been lacking explanation to be understandable by anyone who isn't quite familiar with diesel and its issue history ^^ I'm going to try and give more details :)
that transactions will always be committed
It does not precisely "guarantees that transactions will always be committed", however it does provide a way for users to "make sure to never forget to make the commit call".
If you were to have a guard-based transaction API, it would probably look something like this:
let transaction = conn.begin_transaction()?;
query.execute(&mut transaction.connection);
transaction.commit()?;
Now this has several issues:
(What I call "forget to make the commit call" is specifically "forgetting to write that last line, resulting in transaction never getting committed".)
On the other hand, Diesel's API is based on a Connection::transaction
function:
conn.transaction(|conn| {
query.execute(&mut conn);
Ok(())
})?;
Notice several things:
transaction
functionOk
or Err
result, and the code will obviously fail to compile if it contains neither.Hence indeed, "always using the Connection::transaction
when dealing with transactions" constitutes "a way for users to make sure to never forget to make the commit call"
Anyway - I realize now this is irrelevant to this issue, for reasons described later.
needs the ability to forcibly roll back transactions when a pooled connection is returned
Now there's actually two ways that this could be useful:
transaction
abstraction for some reasontransaction
function, besides Ok
and Err
(last one I promise!): by panicking out of it.For that second case we have several design choices:
Notice in particular that attempting rollback only ever makes sense if the connection is pooled - otherwise it's probably fine to just let the panic unwind and drop the actual connection, which will not be reused.
Diesel went with the second approach, which in particular allows users to, depending on their use-case:
Now if the user's will happens to be "I know this panic in my request handling code will be catched later, and the server will continue its life normally besides that, so I want to attempt rollback and report the error to e.g. Sentry if it fails" (spoiler: that's what we do), then the current only possible approach is
having custom wrappers around
PooledConnection
andPool
, which prevents from always using r2d2 directly (and requires reimplementing a whole bunch of traits on the wrappers).
which having this would solve.
Side note: I realize now that the "no-rollback-attempt on panic" behavior and "don't forget to commit" behavior are pretty untied: it's possible to obtain both "no-rollback-attempt on panic" and "rollback-attempt on panic" in both designs, by checking whether std::thread::panicking
in the guard's Drop
impl, and using std::panic::catch_unwind
in closure-based interface.
That makes the only relevant question between "no-rollback-attempt on panic" and "don't forget to commit" with regards to this issue the "no-rollback-attempt on panic" one: Because we don't want a hardcoded "rollback-attempt on panic" for their previously cited downsides, it would be very handy to let users parametrize how they want to handle not-rolled-back transactions that come back to the pool.
Not to negate of course the other uses this may have, like managing a pool of connections dedicated to tests, where we would open a transaction everytime we get it from the pool, and roll it back every time it comes back, which is very close to our original issue.
I've opened #131 for this.
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.:
Currently this requires having custom wrappers around
PooledConnection
andPool
, 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
andon_release_to_pool
hooks toCustomizeConnection
(or another trait if we want backwards-compat.) and/orManageConnection
.That would solve 1. by allowing users to automatically open a test transaction
on_take_from_pool
and rolling it backon_release_to_pool
(on_release_to_pool
would have to be called beforeis_valid
).That would solve 2. by allowing users (and/or implementors of
ManageConnection
) to automatically attempt closing any open transactionson_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 theManageConnection
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,