The current default for spring.datasource.generate-unique-name (false) causes issues in combination with the default used for Hibernates DDL setting spring.jpa.generate-ddl (create-drop) in the context of Spring Test's context caching. I've attached a sample project that shows the issue.
mvn clean testHere is what's going on:
create-drop causes the database schema being wiped and recreated. As this context does not contain the component setting up the reference data, the database remains initialized but unpopulated. This is fine for this particular context. However, as the database is shared across the application contexts, the other cached one is affected, too.There are a couple of problematic aspects about the current setup:
spring.datasource.generate-unique-name=true causes database instances to be created per ApplicationContext.spring.datasource.generate-unique-name to true.ApplicationContext and – with the new default – would lose the data entered while using the application. This could be mitigated by letting DevTools explicitly disable the property by default.AFAIK, @sbrannen had a few thoughts on that as well.
AFAIK, @sbrannen had a few thoughts on that as well.
Yes, I always advocate generating a random database name for embedded databases in Spring integration tests.
Over the years I have seen numerous projects that ran into issues with @DirtiesContext as well as the LRU eviction policy for the ContextCache, and the only viable solution was using a random database name.
In fact, I updated the Spring Framework reference manual quite awhile ago (several years?) to point that out.
In summary, if it's feasible I think it would be beneficial if spring.datasource.generate-unique-name=true was the default in Spring Boot for integration tests.
Digging though the code a bit we think this might be a safe change to make but we'll need some time to experiment to make sure there are no unexpected side effects.
See https://github.com/spring-projects/spring-boot/issues/4699 for the DevTools side of things.
Can we bring this on the table for 2.3 again?
@odrotbohm Yeah, we'll try again to get this into the next version