Fix/hibernate session integration tests 14920
I've successfully implemented a solution to address the Hibernate session and transaction issue in Grails
integration tests. The solution provides a way for integration tests to run with the same transactional context
as the application.
What was implemented:
- @WithSession annotation (grails-testing-support-core/src/main/groovy/grails/testing/mixin/integration/WithSession.groovy): - New annotation that binds a Hibernate session without starting a transaction - Can be applied at class or method level - Supports specifying specific datasources
- Session binding infrastructure: - GrailsTestSessionInterceptor - Manages session binding without transactions - WithSessionSpecExtension - Spock extension for handling @WithSession - WithSessionTransformation - AST transformation for the annotation
- Enhanced test infrastructure: - Updated GrailsTestMode to support bindSession flag - Modified GrailsTestInterceptor to handle session-only binding - Updated IntegrationSpecConfigurerExtension to enable session binding by default
- Test examples: - WithSessionIntegrationSpec - Demonstrates usage of @WithSession - SessionBindingComparisonSpec - Shows differences between test modes
- Documentation: - Added comprehensive documentation in the integration testing guide
How it solves the problem:
The issue described three scenarios:
- Without @Rollback: No session bound → tests fail for operations that work in the app
- With @Rollback: Both session and transaction → tests pass for operations that fail in the app
- With @WithSession: Session without transaction → tests match application behavior
The new @WithSession annotation provides a session binding that matches the OISV (Open Session In View) pattern
used in running applications, where:
- SELECT operations work (session available)
- save() without flush works (no transaction needed)
- save(flush: true) fails without @Transactional (matches runtime behavior)
This ensures integration tests accurately reflect the application's runtime behavior, preventing false positives
and false negatives in test results.
@RAJ-debug858 Thank you. Can you resubmit with the a target branch of 7.0.x?
When I try to make this change I get "There are no new commits between base branch '7.0.x' and head branch 'fix/hibernate-session-integration-tests-14920'", so I am guessing some of your new commits are not present on your forked branch.
FYI: you can add your branch as a remote, fetch, cherry-pick your change, and then adjust for where its supposed to be. GitHub is showing 10,000+ commits so my guess is this change was made against an earlier branch before we created the mono repo.
@jamesfredley Thank you! I’ve rebased my changes onto the 7.0.x branch and force-pushed the updated branch. The PR now targets 7.0.x as requested.
- grails-test-core/build.gradle (modified)
- grails-testing-support-core/build.gradle (modified)
- The moved files in grails-testing-support-core/src/main/groovy/org/grails/test/
- grails-testing-support-core/src/main/resources/META-INF/services/org.spockframework.runtime.extension.IGlobalE
xtension (modified) - The fixed Java file with the import change
Can't the test behavior be driven by whether the application uses OSIV?
This needs refactored to be an optional feature vs replacing the existing feature.