Skip to content

Add GORM for MongoDB / Spring Data MongoDB interop module - #15745

Merged
jdaugherty merged 11 commits into
apache:8.0.xfrom
codeconsole:feat/mongo-spring-data-interop
Jul 9, 2026
Merged

jdaugherty merged 11 commits into
apache:8.0.xfrom
codeconsole:feat/mongo-spring-data-interop

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

Depends on #15744 (stacked PR)

This is stacked on top of #15744 (opt-in MongoDB transactions). Until #15744 merges, the diff here also includes its commit — please review/merge #15744 first, after which this PR reduces to just the new interop module. The unified transaction here relies on the ClientSession introduced by #15744.

What

A new opt-in module, grails-data-mongodb-spring-data, that lets GORM for MongoDB and Spring Data MongoDB run side by side over the same MongoClient, database and codecs — and, within a single @Transactional method, the same MongoDB transaction.

When the module and spring-data-mongodb are on the classpath of a Spring Boot app that has a GORM MongoDatastore, it auto-configures over GORM's connection:

  • a MongoDatabaseFactory bound to GORM's client + default database (it does not close GORM's client),
  • a MongoTemplate (+ MappingMongoConverter) sharing the driver codec registry,
  • a primary transactionManager (GormSharedSessionMongoTransactionManager) that binds GORM's ClientSession into Spring Data, so a GORM save() and a MongoTemplate/repository write in one @Transactional method commit or roll back together.

Unified transactions require GORM server-side transactions (grails.mongodb.transactional = true, from #15744). Spring Data repositories are enabled the usual way via @EnableMongoRepositories, on a package separate from GORM @Entity classes.

Boundary

Only the connection, codecs and (within a transaction) the ClientSession are shared; the two object-mapping models stay separate (GORM maps @Entity; Spring Data maps its own documents). Sharing the session reaches Spring Data's package-private MongoResourceHolder, so a small helper lives in package org.springframework.data.mongodb — verified against the Spring Data MongoDB 5.x line in Spring Boot 4, class-path only (not JPMS module-path). A single flat transaction (PROPAGATION_REQUIRED) is supported; REQUIRES_NEW/NESTED are not.

Tests

  • UnifiedMongoTransactionSpec — GORM + MongoTemplate commit and roll back together on one session; coexistence read; sequential transactions without session/holder leaks; a Spring Data repository over the shared connection; auto-config wiring; the shared factory does not close GORM's client.
  • GormSpringDataSessionSupportSpec — coupling smoke test that fails loudly if the Spring Data internal shape changes.

Targets 8.0.x.

@borinquenkid

Copy link
Copy Markdown
Member

Hi @codeconsole,

Please keep an eye on #15678, which modifies the way the MongoRegistry is being handled. It might be worth checking your diff against those updates, as this PR is downstream from 8.0.x-hibernate7.

Introduces an optional module that lets GORM for MongoDB and Spring Data MongoDB
run side by side over the same MongoClient, database and codecs, and - within a
single transaction - the same MongoDB ClientSession.

When the module and spring-data-mongodb are on the classpath of a Spring Boot
application that has a GORM MongoDatastore, SpringDataMongoGormAutoConfiguration
registers, over GORM's existing connection: a MongoDatabaseFactory bound to GORM's
client and default database (which does not close that client), a MongoTemplate and
MappingMongoConverter sharing the driver codec registry, and a primary
transactionManager.

That manager, GormSharedSessionMongoTransactionManager, extends GORM's
DatastoreTransactionManager and binds GORM's ClientSession into Spring Data's
thread-bound resources, so a GORM save() and a MongoTemplate/repository write in one
@transactional method commit or roll back together. Unified transactions require GORM
server-side transactions (grails.mongodb.transactional = true). The two object-mapping
models stay separate; only the connection, codecs and session are shared.
@codeconsole
codeconsole force-pushed the feat/mongo-spring-data-interop branch from 36e36a5 to 342579f Compare June 17, 2026 23:16

@jdaugherty jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats the benefit for having Spring Data and GORM?

@codeconsole

Copy link
Copy Markdown
Contributor Author

This is for apps that already have or need both, where today the only option is two separate MongoClients with no transactional consistency between them. The module makes that coexistence correct: GORM and Spring Data share one MongoClient, database and codec registry, and — within a single @Transactional — the same MongoDB ClientSession, so a GORM save() and a MongoTemplate/repository write commit or roll back together.

Concrete cases: Spring-ecosystem libraries built on MongoTemplate/Spring Data repositories (Spring Batch, Spring Session, Spring Integration, Spring Security ACL); incremental migration in either direction without doubling connections; and reaching actively-developed Spring Data features for specific operations while keeping GORM for domain modeling.

It's opt-in — a separate module that pulls in spring-data-mongodb only when an app adds it; core stays driver-only and the mapping models stay separate.

@codeconsole

Copy link
Copy Markdown
Contributor Author

Thanks @borinquenkid — I checked #15678 against this PR's diff. It's Hibernate-only: the registry-scaling changes are all in grails-data-hibernate5/grails-data-hibernate7 (plus graphql and gradle config), with no changes to grails-data-mongodb, grails-datastore-core, or grails-datamapping-core. The only shared touchpoint is gradle/mongodb-test-config.gradle, which this PR doesn't modify, so it merges cleanly. I'll rebase onto 8.0.x and re-run the Mongo suites once #15678 lands, but there's no code overlap with the interop module here.

…tx manager

Move the Spring Boot auto-configuration registration from the non-standard
META-INF/services/spring/ location to META-INF/spring/, which is where Spring Boot's
ImportCandidates actually scans - otherwise the module never auto-configures in a real
application. Add a test that asserts the imports file is at the canonical location (and
not the old one) so the path cannot silently regress.

Also document that the unified manager is @primary (required to win over GORM's
mongoTransactionManager) and how to target the intended manager in an application that
mixes another persistence stack.
Verifies the auto-configuration through an ApplicationContextRunner so the conditions,
ordering and @ConditionalOnMissingBean back-off are actually exercised in a real context
(the existing specs only invoked the @bean methods directly): the interop beans wire when
a MongoDatastore is present, nothing activates without one, and an application-defined
mongoTemplate takes precedence. Runs offline.
@codecov

codecov Bot commented Jun 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.87879% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.3686%. Comparing base (acc277d) to head (89e4ab1).
⚠️ Report is 28 commits behind head on 8.0.x.

Files with missing lines Patch % Lines
...ta/GormSharedSessionMongoTransactionManager.groovy 80.0000% 0 Missing and 3 partials ⚠️
...ork/data/mongodb/GormSpringDataSessionSupport.java 80.0000% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##                8.0.x     #15745        +/-   ##
==================================================
+ Coverage     49.3593%   49.3686%   +0.0093%     
- Complexity      16777      16788        +11     
==================================================
  Files            1986       1989         +3     
  Lines           93336      93369        +33     
  Branches        16337      16339         +2     
==================================================
+ Hits            46070      46095        +25     
- Misses          40140      40146         +6     
- Partials         7126       7128         +2     
Files with missing lines Coverage Δ
...ngdata/SpringDataMongoGormAutoConfiguration.groovy 100.0000% <100.0000%> (ø)
...ork/data/mongodb/GormSpringDataSessionSupport.java 80.0000% <80.0000%> (ø)
...ta/GormSharedSessionMongoTransactionManager.groovy 80.0000% <80.0000%> (ø)

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

# Conflicts:
#	grails-data-mongodb/core/src/main/groovy/org/grails/datastore/mapping/mongo/query/MongoQuery.java
@jdaugherty

Copy link
Copy Markdown
Contributor

Isn't the transaction support a requirement before this PR?

- setTimeout now throws TransactionUsageException for a non-default
  timeout instead of silently ignoring it. The server transaction is
  started before the manager applies a timeout, so it cannot be honored
  at this layer; DatastoreTransactionManager.doBegin catches it, rolls
  back the just-started ClientSession, and rethrows as
  CannotCreateTransactionException, so nothing leaks.
- All nine AbstractMongoSession driver helpers branch on
  hasActiveTransaction() rather than a raw clientSession null check, so a
  session that lingers after its transaction commits falls back to the
  session-less overload.
- Document on the MongoTransaction Javadoc that whole-transaction retry
  on TransientTransactionError is intentionally deferred: it requires
  re-executing the transaction body, which the Spring
  PlatformTransactionManager SPI cannot do.
- Add a spec asserting a per-transaction timeout is rejected and the
  datastore stays usable afterwards, and document the timeout behavior
  in advancedConfig.adoc.
Add an interop test asserting that a timeout on
GormSharedSessionMongoTransactionManager is refused
(CannotCreateTransactionException) before any Spring Data resource holder
is bound, that nothing persists on either stack, and that the manager
stays usable afterward. This pins the ordering guarantee that
super.doBegin runs — and can throw — before the ClientSession is shared
with Spring Data.
Reword the MongoTransaction Javadoc so it no longer reads as deferred
work: whole-transaction retry on a TransientTransactionError is left to
the application (as Spring Data MongoDB's own transaction manager does),
since re-running the transaction body would repeat its side effects.
Bring MongoTransaction and AbstractMongoSession in line with the reviewed
Layer 1 code: MongoTransaction.commit() now explicitly aborts the
server-side transaction on a failed commit rather than relying on
close() to do so implicitly, and the AbstractMongoSession clientSession
field carries its threading/nullability contract note. This makes the
interop branch's copy identical to apache#15744.
…data-interop

# Conflicts:
#	grails-data-mongodb/docs/src/docs/asciidoc/gettingStarted/advancedConfig.adoc

@jdaugherty jdaugherty left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's zero docs added for this, we should add docs to grails-doc instead of just the README. Otherwise, it's approved.

Adds a What's New section in grails-doc for the new opt-in
grails-data-mongodb-spring-data module: the unified @transactional
example, the grails.mongodb.transactional requirement, and a link to
the Spring Data MongoDB Interoperability section of the GORM for MongoDB
guide. Addresses the review request to document the feature in grails-doc
rather than only the module README.
@testlens-app

testlens-app Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 89e4ab1
▶️ Tests: 14597 executed
🟡 Checks: 58/59 completed


Learn more about TestLens at testlens.app.

@jdaugherty
jdaugherty merged commit 7863ae3 into apache:8.0.x Jul 9, 2026
63 of 64 checks passed
codeconsole added a commit to codeconsole/grails-core that referenced this pull request Sep 2, 2026
Passing the definition down left both transaction types with the
constructor they had before read-only existed, and nothing in the tree
calls either one now.

MongoTransaction's has only ever appeared in the 8.0.0 milestones: apache#15744
added the class as an internal replacement for SessionOnlyTransaction with
AbstractMongoSession as its only caller, and the Spring Data interop that
followed in apache#15745 takes the ClientSession off the session rather than
constructing a transaction of its own. It is removed rather than carried
into the release.

SessionOnlyTransaction's shipped in 7.0.x and in GORM before that, where
an out-of-tree datastore may be constructing it, so it is deprecated for
removal in favour of the constructor that states whether the transaction
is read-only instead of assuming it is not.
codeconsole added a commit to codeconsole/grails-core that referenced this pull request Sep 17, 2026
Passing the definition down left both transaction types with the
constructor they had before read-only existed, and nothing in the tree
calls either one now.

MongoTransaction's has only ever appeared in the 8.0.0 milestones: apache#15744
added the class as an internal replacement for SessionOnlyTransaction with
AbstractMongoSession as its only caller, and the Spring Data interop that
followed in apache#15745 takes the ClientSession off the session rather than
constructing a transaction of its own. It is removed rather than carried
into the release.

SessionOnlyTransaction's shipped in 7.0.x and in GORM before that, where
an out-of-tree datastore may be constructing it, so it is deprecated for
removal in favour of the constructor that states whether the transaction
is read-only instead of assuming it is not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants