Skip to content

Multi DataSource setup stopped working after upgrading Spring Boot to 3.3.x #41229

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

Closed
icepuma opened this issue Jun 25, 2024 · 3 comments
Closed
Labels
status: invalid An issue that we don't feel is valid

Comments

@icepuma
Copy link

icepuma commented Jun 25, 2024

Description

After upgrading Spring Boot from 3.2.7 to 3.3.1 our multi-datasource based on the AbstractRoutingDataSource stopped working. We have a test that tests different @Transactional - combinations.

I did read a lot about the LazyConnectionDataSourceProxy in issues like #15480, but nothing really worked:

  • Use LazyConnectionDataSourceProxy as my primary datasource with the target / readonly datasource feature
  • Wrapping our AbstractRoutingDataSource in LazyConnectionDataSourceProxy

Versions

OS: macOS 14.5
Java: 17
Spring Boot:

  • 3.2.7 ✅
  • 3.3.0 ❌
  • 3.3.1 ❌

How to reproduce the issue

Repository: https://github.com/open-source-issues/spring-multi-datasource
Command: https://github.com/open-source-issues/spring-multi-datasource/blob/main/README.md

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 25, 2024
@wilkinsona
Copy link
Member

Thanks for the sample. The change in behaviour is due to Hibernate which Spring Boot 3.3.x upgrades to 6.5.x. If I run the tests with Spring Boot 3.3.1 and Hibernate downgraded to 6.4.9.Final they all pass.

I'm not sure that a fully understand the purpose of the tests, but they appear to be relying on some side-effects of Hibernate's behavior despite not actually using JPA. Specifically, they seem to be relying up Hibernate calling getConnection() on the DataSource. I would guess that some optimisations in Hibernate 6.5.x mean that it no longer gets a connection so eagerly which results in connectionReadOnlyCaptor not capturing anything.

You can see similar failures with Spring Boot 3.2.7 if you change the tests from @DataJpaTest to @JdbcTest. Doing so takes Hibernate out of the picture and means that getConnection() is never called on the DataSource. As far as I can tell, you need to rework your tests to avoid relying upon a side-effect of how Hibernate behaves.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Jun 25, 2024
@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 25, 2024
@philwebb
Copy link
Member

Just coming here to say the same thing. Adding the following to the build scripts makes the tests pass:

buildscript {
    extra.apply {
        set("hibernate.version", "6.4.9.Final")
    }
}

To cause seems to be a change in org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor where the commit() method now checks isPhysicallyConnected() which returns false.

@icepuma
Copy link
Author

icepuma commented Jun 25, 2024

Thanks for the explanation!

I think I got the issue and related commit.

Adding a simple entityManager.createQuery("select 1").getResultList(); to all inner transactions fixes nearly all tests, because it forces a connection acquisition (took it from the test from the linked commit) and hence a call for dataSource.getConnection().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants