r2d2 icon indicating copy to clipboard operation
r2d2 copied to clipboard

Is there a way to drop or close an actual connection when I'm using PooldConnection?

Open atuk721 opened this issue 3 years ago • 4 comments

Because when I work with the connection of PooldConnection got by Pool.get and face some error that a protocol of the connection became bad state, I'd liket to close the actual connection.

I don't want to use test_on_checkout because it's high impact for performance.

atuk721 avatar Sep 23 '21 07:09 atuk721

I had similar issue, I hacked it like this:

implement has_broken with boolean holder

fn has_broken(&self, conn: &mut MyConnection) -> bool {
    if self.release_on_return_to_pool.get() {
            return true;
    }
...
pub struct MyConnection {
...
    /// Indicates that we want to release this connection on return to pool (used for gracefull shutdown)
    release_on_return_to_pool: Cell<bool>,
...
impl ProtocolRunnerConnection {
...
    /// Mark connection as "destroy" when return back to pool
    pub fn set_release_on_return_to_pool(&self) {
        self.release_on_return_to_pool.set(true);
    }
...
}

so when I have aquired connection and some error occures, I can decide, if to return connection to pool or to destroy it by calling set_release_on_return_to_pool

bkontur avatar Sep 23 '21 07:09 bkontur

@sfackler if this solution is ok, I could maybe do pull request to your library, btw. r2d2 is very cool and works perfectly for me

bkontur avatar Sep 23 '21 07:09 bkontur

This seems like quite the hack. Is there really no way of closing or returning a connection to the pool explicitly?

I'd expect something like connection.close() for example— or pool.reclaim(connection)

thomasmost avatar Sep 16 '22 23:09 thomasmost

FWIW I found one source that says connections are returned to the pool as soon as they go out of scope

thomasmost avatar Sep 16 '22 23:09 thomasmost