Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection leak when using r2dbc-pool with r2dbc-mysql #294

Closed
1528110566 opened this issue Nov 24, 2024 · 4 comments
Closed

connection leak when using r2dbc-pool with r2dbc-mysql #294

1528110566 opened this issue Nov 24, 2024 · 4 comments
Assignees

Comments

@1528110566
Copy link

Environment
Windows 11
JDK 1.8.0-381
Spring-parent 2.7.13
r2dbc-mysql 0.9.3

Describe the bug
I've been stucked with an bug recently, and I search anywhere on GitHub but nothing.
Here is an issue that very close to answer. pgjdbc/r2dbc-postgresql#661
So I wrote an issue under this repo.

It's quit simple. Update something in database transaction, throw an exception if the updated rows is not 1(always true because it's not exists). 2 update statements are triggled by Flux parallel.

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();
}

The funny thing is, when I change .doOnNext to another block(attention on )), it's works as expected.

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();
}

So, I believe something must not compatible with cancel in reactor and spring-tx.
Another thing confusing me is, why the number of r2dbc_pool_acquired_connections always 9.

image
image

To Reproduce
https://github.com/1528110566/r2dbc-connection-leak-demo
This is a demo that can reproduce this bug.

  1. call http://localhost:8080/test
  2. wait background threads are finished
  3. call http://localhost:8080/actuator/prometheus
  4. 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 https://github.com/r2dbc/r2dbc-pool/blob/bf6540eee26aa55bba4cb6ab0c6a3c622524679c/src/main/java/io/r2dbc/pool/ConnectionPool.java#L106. We'will find the number of connection is actually smaller than expected.
@jchrys
Copy link
Collaborator

jchrys commented Nov 24, 2024

Thanks for describing the issue in detail. Let me look into it

@jchrys
Copy link
Collaborator

jchrys commented Nov 25, 2024

@1528110566
This doesn't seem to be caused by r2dbc-mysql. Could you kindly raise a ticket with r2dbc-pool?

@1528110566
Copy link
Author

Sure, I will.

@1528110566
Copy link
Author

Expecting a good news...
r2dbc/r2dbc-pool#219

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants