Closed ssharan27 closed 1 year ago
@ssharan27 Thank you for submitting a high-quality issue report for r2dbc-mysql! Your detailed input is greatly appreciated and will help us investigate and resolve the problem effectively. We value your contribution to improving the project and will keep you updated on our progress.
Thanks again!
@ssharan27 After reviewing the issue, it appears that the problem lies with the connection pool rather than the driver. In HikariCP, when acquiring a connection, it attempts to retrieve it repeatedly within the maxAcquireTime, even if the connection is closed. On the other hand, r2dbc-pool seems to attempt a limited number of retries, specified by maxRetry.
To prevent the r2dbc connection pool from retrieving closed connections due to remote mysql server, I found that setting the maxIdleTime to a value lower than the wait_timeout of MySQL resolves the issue. This adjustment ensures that such occurrences no longer happen.
logging.level.org.springframework.r2dbc=DEBUG
spring.r2dbc.url=r2dbc:mysql://localhost:3306/test
spring.r2dbc.username=root
spring.r2dbc.password=password
spring.r2dbc.pool.max-idle-time=10s <-- added
if you believe there is a need to modify the current behavior of the connection pool, I recommend creating an issue on the following GitHub repository: https://github.com/r2dbc/r2dbc-pool. They will be better equipped to assist you with the necessary modifications.
Thanks a lot for your understanding and cooperation. 👍
Thanks @jchrys and @JohnNiang for taking a look, will raise this on r2dbc pool repo. In the mean time will tweak the maxIdleTime value. Cheers :+1:
Closed this issue cc @JohnNiang @ssharan27
Describe the bug I have a Spring boot application which I am migrating to Spring Boot 3. I am using the r2dbc mysql driver to connect with db. I am facing issues as queries are randomly failing. Error occurs when idle connection(s) are closed by the DB, queries coming post connection closure fail to execute with exception
org.springframework.dao.DataAccessResourceFailureException: Failed to obtain R2DBC Connection
To Reproduce platform: Ubuntu 22.04 jdk version:
openjdk-20
maven version:3.8.1
spring-boot version:3.1.0
r2dbc-mysql version:1.0.2
Sample repo link Complete app log gist
Steps:
1) Install and run mysql on local (i had used this doc for reference). It runs on port
3306
by default. 2) Reduceidle_timeout
andinteractive_timeout
to 180 by adding following configuration to/etc/alternatives/my.cnf
(Reducing these timeout values causes the DB to close idle connections after 3 minutes thus saving time :P):create a test database, the reproducer application uses the database:
test
. On mysql CLI run:3) Clone and run the reproducer spring boot application using maven, by default the application will run on port 8080. (Check the application.properties to configure database related properties according to your local setup)
Hit these endpoints to validate the application running correctly.
4) Now wait for 3 minutes (as we set the idle and interactive timeout to 3 minutes) so that the DB starts evicting idle connections. This can be seen using the
show processlist;
command. It will look something like this:This is also indicated in the application logs:
5) Now hit the api using the previous curls. It will give error response:
Stacktrace from application logs:
Expected behavior Expect the driver to reconnect upon connection validation failure and then execute the queries as is the case with the 'traditional' JPA which uses JDBC driver. In case of JDBC driver, application is able to recover from the connection validation error and return the response.
Additional context If required, I can add another sample application that uses JDBC which executes queries as expected with the same reproduction steps mentioned previously, for now I have only added the application logs, config properties and relevant dependencies from pom. application.properties:
logs:
dependencies: