Bug Report
Here is my demo. Update something that not exists, throw an exception in database transaction. Two statements executed parallel using Flux.
Query query = Query.query(Criteria.where("id").is(3));
Update update = Update.update("name", "3");
List<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
for (int i = 0; i < 1000; i++) {
Flux.fromIterable(list)
.flatMap(item -> {
return transactionalOperator.transactional(template.update(query, update, User.class)
.doOnNext(res -> {
if (res != 1) {
throw new RuntimeException("test");
}
}));
})
.subscribe();
}
pool-max-size is 10.
After execution, actuator shows the number of idle connections is smaller than expected, which is 1.


However, when I move .doOnNext to another block, it works as expected (attention on ) behind User.class).
Query query = Query.query(Criteria.where("id").is(3));
Update update = Update.update("name", "3");
List<Integer> list = new ArrayList<>();
list.add(0);
list.add(1);
for (int i = 0; i < 1000; i++) {
Flux.fromIterable(list)
.flatMap(item -> {
return transactionalOperator.transactional(template.update(query, update, User.class))
.doOnNext(res -> {
if (res != 1) {
throw new RuntimeException("test");
}
});
})
.subscribe();
}
Versions
Windows 11
JDK 1.8.0-381
Spring-parent 2.7.13
r2dbc-mysql 0.9.3
MySQL Server 9.1.0-1.el9
Steps to reproduce
https://github.com/1528110566/r2dbc-connection-leak-demo
This is a demo that can reproduce this bug.
- call http://localhost:8080/test
- wait background threads are finished
- call http://localhost:8080/actuator/prometheus
- search
r2dbc_pool_acquired_connectionsand r2dbc_pool_idle_connections. We'll find the number of connection in the pool is not as expected. This can be proved with debugging on
|
Mono<Connection> mono = this.connectionPool.acquire() |
We'will find the number of connection is actually smaller than expected.
Expected behavior/code
r2dbc_pool_acquired_connections is 0 and r2dbc_pool_idle_connections is 10 after execution.
Possible Solution
No idea.
I searched everywhere on GitHub, found lots of issues related to this problem.
I think this issue is very close to answer: pgjdbc/r2dbc-postgresql#661 , but it's too difficult to follow to me :(
Bug Report
Here is my demo. Update something that not exists, throw an exception in
database transaction. Two statements executed parallel usingFlux.pool-max-sizeis 10.After execution,
actuatorshows the number ofidle connectionsis smaller than expected, which is 1.However, when I move
.doOnNextto another block, it works as expected (attention on)behindUser.class).Versions
Windows 11
JDK 1.8.0-381
Spring-parent 2.7.13
r2dbc-mysql 0.9.3
MySQL Server 9.1.0-1.el9
Steps to reproduce
https://github.com/1528110566/r2dbc-connection-leak-demo
This is a demo that can reproduce this bug.
r2dbc_pool_acquired_connectionsandr2dbc_pool_idle_connections. We'll find the number of connection in the pool is not as expected. This can be proved with debugging onr2dbc-pool/src/main/java/io/r2dbc/pool/ConnectionPool.java
Line 106 in bf6540e
We'will find the number of connection is actually smaller than expected.
Expected behavior/code
r2dbc_pool_acquired_connectionsis 0 andr2dbc_pool_idle_connectionsis 10 after execution.Possible Solution
No idea.
I searched everywhere on GitHub, found lots of issues related to this problem.
I think this issue is very close to answer: pgjdbc/r2dbc-postgresql#661 , but it's too difficult to follow to me :(