When using R2dbcTransactionManager, connection will not be released if it encounters error while doing afterCleanup. As afterCleanup uses a database connection when doing setAutoCommit(true), it can be failed under some conditions where the connection not reliable.

The problem is that when this issue occurs, the number of acquired connections are not decreased because the connection is not released. Also, if all the connections in the pool are depleted, the connection pool cannot allocate further connections and the request that needs database connections hang.

I created a project to easily reproduce this issue: https://github.com/FutureGadget/spring-r2dbc-connection-not-released

Since I use Spring 5.3.25 in production, I hope this fix to be backported at least to 5.3.25 and later versions. Thank you.

Comment From: FutureGadget

@sbrannen Hi, thank you for adding tags for this PR. I know there are a lot of other urgent issues going on, but I just wonder if this issue can be fixed in the next release. Thank you.

Comment From: oshai

This is related to https://github.com/jasync-sql/jasync-sql/pull/347

Comment From: simonbasle

@FutureGadget I was looking at the reproducer and I don't see it fail. Does it actually reproduce the issue ? I even see the Releasing R2DBC Connection log:

DEBUG 35433 --- [-netty-thread-8] o.s.r.c.R2dbcTransactionManager          : Releasing R2DBC Connection [PooledConnection[com.github.jasync.r2dbc.mysql.JasyncClientConnection@45106529]] after transaction
DEBUG 35433 --- [-netty-thread-8] io.r2dbc.pool.PooledConnection           : Releasing connection
DEBUG 35433 --- [-netty-thread-8] io.r2dbc.pool.PooledConnection           : Release failed

Comment From: FutureGadget

@FutureGadget I was looking at the reproducer and I don't see it fail. Does it actually reproduce the issue ? I even see the Releasing R2DBC Connection log:

DEBUG 35433 --- [-netty-thread-8] o.s.r.c.R2dbcTransactionManager : Releasing R2DBC Connection [PooledConnection[com.github.jasync.r2dbc.mysql.JasyncClientConnection@45106529]] after transaction DEBUG 35433 --- [-netty-thread-8] io.r2dbc.pool.PooledConnection : Releasing connection DEBUG 35433 --- [-netty-thread-8] io.r2dbc.pool.PooledConnection : Release failed

I've recently created a PR which can mitigate the issue on the driver side (https://github.com/jasync-sql/jasync-sql/pull/378) and that's released in version 2.1.20. To reproduce this issue, I've downgraded the driver version to 2.1.19 on the main branch. I tried it again and it correctly reproduces the issue mentioned here. Sorry for the inconvenience.

Comment From: simonbasle

(...) that's released in version 2.1.20. To reproduce this issue, I've downgraded the driver version to 2.1.19 on the main branch.

Ah that makes sense now @FutureGadget ! I saw this PR but GitHub indicated the fix was released in 2.1.21, so I didn't think twice about the sample using 2.1.20. Thanks for clarifying.

Will you have time in the next few days to take my suggestions from the PR review into account ?

Comment From: FutureGadget

Will you have time in the next few days to take my suggestions from the PR review into account ?

No problem! I appreciate for the detailed review. Let me work on this right now.