Skip to content

Do not close an externally-supplied MongoClient in GORM for MongoDB#15743

Open
codeconsole wants to merge 1 commit into
apache:8.0.xfrom
codeconsole:feat/mongo-external-client-lifecycle
Open

Do not close an externally-supplied MongoClient in GORM for MongoDB#15743
codeconsole wants to merge 1 commit into
apache:8.0.xfrom
codeconsole:feat/mongo-external-client-lifecycle

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

What

GORM's connection-source shutdown closes any Closeable source, and com.mongodb.client.MongoClient is Closeable, so a client handed to GORM — a Spring-managed bean, or one passed to new MongoDatastore(mongoClient, ...) — was closed when the datastore shut down. That double-closes a client whose lifecycle is owned elsewhere.

This makes GORM not close a MongoClient it did not create:

  • DefaultConnectionSource gains a non-closeable mode (new 4-arg constructor + isCloseable()); the existing constructor is unchanged, so there is no behavior change for current callers.
  • MongoDatastore.createDefaultConnectionSources wraps an externally-supplied client as non-closeable.
  • MongoDbGormAutoConfiguration implements DisposableBean and closes only the client it created itself; a client supplied as a bean is left for the application context to close.

GORM still closes clients it creates from grails.mongodb.* configuration.

Why

  • Removes a redundant double-close in the common Spring Boot setup (Spring already owns and closes the MongoClient bean).
  • Establishes "whoever creates the client owns closing it" — the safe basis for sharing one MongoClient across GORM and other consumers.

Behavior change

The only externally-visible change: callers that relied on MongoDatastore.close() to also close a client they passed in must now close it themselves. Documented in the upgrade notes.

Tests

  • DefaultConnectionSourceSpec — closes by default; no-op when non-closeable; closes an AutoCloseable when closeable.
  • MongoDatastoreExternalClientSpec — a supplied client is not closed on datastore shutdown.
  • MongoDbGormAutoConfigurationCloseSpec — the auto-config closes only an internally-created client.

Targets 8.0.x.

GORM's connection-source shutdown closes any Closeable source, and a MongoClient is
Closeable, so a client handed to GORM (for example a Spring-managed bean, or one passed
to new MongoDatastore(mongoClient, ...)) was closed when the datastore shut down -
double-closing a client whose lifecycle is owned elsewhere.

DefaultConnectionSource now supports a non-closeable mode (a new constructor plus
isCloseable()); the existing constructor is unchanged. MongoDatastore wraps an
externally-supplied client as non-closeable so GORM no longer closes a client it did not
create, and MongoDbGormAutoConfiguration closes only the client it created itself. GORM
still closes clients it creates from grails.mongodb.* configuration.

The only behavior change affects callers that relied on MongoDatastore.close() to also
close a client they passed in; they must now close it themselves. This is documented in
the upgrade notes.
@testlens-app

testlens-app Bot commented Jun 18, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 735529e
▶️ Tests: 24421 executed
⚪️ Checks: 44/44 completed


Learn more about TestLens at testlens.app.

@jdaugherty

Copy link
Copy Markdown
Contributor

Why are you using gorm's mongo and spring's mongo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants