spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Spring devtools not shutdown connection pool on restart

Open pascencio opened this issue 9 months ago • 1 comments

I'm having trouble with spring-devtools. My application uses Spring JPA with HikariCP. The problem occurs when the application restarts because the connections are not terminated and all connections remain open.

pascencio avatar May 01 '24 02:05 pascencio

Thanks for the report. Unfortunately it does not contain enough information for us to diagnose the problem. For example, you haven't told us what version of Spring Boot you're using or how you have configured Hikari. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

wilkinsona avatar May 01 '24 06:05 wilkinsona

Thanks for the report. Unfortunately it does not contain enough information for us to diagnose the problem. For example, you haven't told us what version of Spring Boot you're using or how you have configured Hikari. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

Thank you, @wilkinsona. Apologies for the inadequate details in my initial description. Here is a link to a simple demo repository: https://github.com/pascencio/spring-dev-tools-issue

I conducted some tests and it seems that the issue may occur when using AbstractRoutingDataSource.

Thanks in the advance!

pascencio avatar May 03 '24 23:05 pascencio

it seems that the issue may occur when using AbstractRoutingDataSource

Yes, that's the problem. The DataSources that your MultiTenantDataSource is using are not beans so the application context does not know about them and does not know that they need to be closed. You could either expose them as beans or you could change MultiTenantDataSource to implement DisposableBean and close each of its targets when it is destroyed.

Note that this isn't a Devtools-specific problem. The target DataSources will also not be closed when the application is shut down normally.

wilkinsona avatar May 04 '24 09:05 wilkinsona

it seems that the issue may occur when using AbstractRoutingDataSource

Yes, that's the problem. The DataSources that your MultiTenantDataSource is using are not beans so the application context does not know about them and does not know that they need to be closed. You could either expose them as beans or you could change MultiTenantDataSource to implement DisposableBean and close each of its targets when it is destroyed.

Note that this isn't a Devtools-specific problem. The target DataSources will also not be closed when the application is shut down normally.

Thanks, @wilkinsona! I tweaked my code based on your suggestions—switched my MultiTenantDataSource to Component and implemented DisposableBean. Now it ensures all connections close properly during the shutdown process.

pascencio avatar May 04 '24 15:05 pascencio