public class MongoDatastore extends AbstractDatastore implements MappingContextListener, java.io.Closeable, MultiTenantCapableDatastore, MultipleConnectionSourceCapableDatastore, StatelessDatastore, TransactionCapableDatastore, org.springframework.context.SmartLifecycle
A Datastore implementation for the Mongo document store.
| Modifiers | Name | Description |
|---|---|---|
static java.lang.String |
CODEC_ENGINE |
|
static java.lang.String |
INDEX_ATTRIBUTES |
|
static java.lang.String |
INDEX_EXPIRE_AFTER_SECONDS |
TTL index attribute. |
static java.lang.String |
INDEX_RECREATE_ON_CONFLICT |
Opt-in index attribute: when an index already exists on the same keys with conflicting options that cannot be reconciled in place (i.e. anything other than a TTL change), or the declared name is taken by an index on other keys, drop the existing index and recreate it with the declared options instead of just logging the conflict. |
static int |
LIFECYCLE_PHASE |
Below the web server's phase so the client outlives the requests using it, and above
EmbeddedMongoLifecycle.PHASE so an embedded server outlives this client:
Spring starts in ascending phase order and stops in descending. |
static java.lang.String |
SETTING_CONNECTION_STRING |
|
static java.lang.String |
SETTING_DATABASE_NAME |
|
static java.lang.String |
SETTING_DEFAULT_MAPPING |
|
static java.lang.String |
SETTING_ENGINE |
|
static java.lang.String |
SETTING_HOST |
|
static java.lang.String |
SETTING_OPTIONS |
|
static java.lang.String |
SETTING_PASSWORD |
|
static java.lang.String |
SETTING_PORT |
|
static java.lang.String |
SETTING_STATELESS |
|
static java.lang.String |
SETTING_URL |
|
static java.lang.String |
SETTING_USERNAME |
|
protected AutoTimestampEventListener |
autoTimestampEventListener |
|
protected boolean |
buildIndexes |
|
protected boolean |
buildIndexesAsync |
|
protected boolean |
codecEngine |
|
protected CodecRegistry |
codecRegistry |
|
protected ConnectionSources<MongoClient, MongoConnectionSourceSettings> |
connectionSources |
|
protected java.util.Map<java.lang.String, MongoDatastore> |
datastoresByConnectionSource |
Concurrent because it is written by the connection sources listener, which can add a child for a connection registered at runtime, while close() may be iterating it. |
protected java.lang.String |
defaultDatabase |
|
protected jakarta.persistence.FlushModeType |
defaultFlushMode |
|
protected ConfigurableApplicationEventPublisher |
eventPublisher |
|
protected GormEnhancer |
gormEnhancer |
|
protected MongoClient |
mongo |
Not final because start() replaces it after a CRaC restore when it is not a RestartableMongoClient, which is restarted in place instead. |
protected java.util.Map<PersistentEntity, java.lang.String> |
mongoCollections |
|
protected java.util.Map<PersistentEntity, java.lang.String> |
mongoDatabases |
|
protected MultiTenancyMode |
multiTenancyMode |
|
protected boolean |
stateless |
|
protected TenantResolver |
tenantResolver |
|
protected org.springframework.transaction.PlatformTransactionManager |
transactionManager |
|
protected boolean |
transactionsEnabled |
| Fields inherited from class | Fields |
|---|---|
class AbstractDatastore |
LOG, applicationEventPublisher, cacheAdapterRepository, connectionDetails, mappingContext, serviceRegistry |
| Constructor and description |
|---|
MongoDatastore(ConnectionSources<MongoClient, MongoConnectionSourceSettings> connectionSources, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(ConnectionSources<MongoClient, MongoConnectionSourceSettings> connectionSources, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, org.springframework.core.env.PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, org.springframework.core.env.PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(java.util.function.Supplier<MongoClient> clientSupplier, org.springframework.core.env.PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Package packages)Configures a new MongoDatastore around the clients a supplier builds, which GORM owns: it builds one the first time the client is needed, closes it when the datastore is stopped for a checkpoint, and builds the one the restore needs from the same supplier. |
MongoDatastore(java.util.function.Supplier<MongoClient> clientSupplier, org.springframework.core.env.PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore around the clients a supplier builds; see MongoDatastore(Supplier, PropertyResolver, ConfigurableApplicationEventPublisher, Package...). |
MongoDatastore(MongoClient mongoClient, org.springframework.core.env.PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Package packages)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, org.springframework.core.env.PropertyResolver configuration, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, org.springframework.core.env.PropertyResolver configuration, java.lang.Package packages)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(Builder clientOptions, org.springframework.core.env.PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(Builder clientOptions, org.springframework.core.env.PropertyResolver configuration, MongoMappingContext mappingContext)Configures a new MongoDatastore for the given arguments |
MongoDatastore(org.springframework.core.env.PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(org.springframework.core.env.PropertyResolver configuration, MongoConnectionSourceFactory connectionSourceFactory, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(org.springframework.core.env.PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(org.springframework.core.env.PropertyResolver configuration, MongoMappingContext mappingContext)Configures a new MongoDatastore for the given arguments |
MongoDatastore(org.springframework.core.env.PropertyResolver configuration, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(java.util.Map<java.lang.String, java.lang.Object> configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(java.util.Map<java.lang.String, java.lang.Object> configuration, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(java.util.Map<java.lang.String, java.lang.Object> configuration)Creates a MongoDatastore with the given configuration |
MongoDatastore(java.util.Map<java.lang.String, java.lang.Object> configuration, MongoMappingContext mappingContext) |
MongoDatastore(MongoMappingContext mappingContext)Configures a new MongoDatastore for the given arguments |
MongoDatastore(java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(java.lang.Package packagesToScan)Construct a Mongo datastore scanning the given packages |
MongoDatastore(java.lang.Package packageToScan)Construct a Mongo datastore scanning the given package |
MongoDatastore(org.springframework.core.env.PropertyResolver configuration, java.lang.Package packagesToScan)Construct a Mongo datastore scanning the given packages |
MongoDatastore(java.util.Map<java.lang.String, java.lang.Object> configuration, java.lang.Package packagesToScan)Construct a Mongo datastore scanning the given packages |
MongoDatastore(org.springframework.core.env.PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Package packagesToScan)Construct a Mongo datastore scanning the given packages |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
buildIndex()Creates and reconciles the indexes declared by the domain classes mapped to this datastore's connection. |
|
public java.util.concurrent.CompletableFuture<IndexBuildResult> |
buildIndexAsync()Runs the same build as buildIndex() on this connection's background index build thread, whatever grails.mongodb.buildIndexesAsync says, and returns at once with a handle on its outcome. |
|
public void |
close() |
|
protected static ConnectionSources<MongoClient, MongoConnectionSourceSettings> |
createDefaultConnectionSources(MongoClient mongoClient, org.springframework.core.env.PropertyResolver configuration, MongoMappingContext mappingContext, boolean closeable)Creates the connection sources for a MongoClient. |
|
protected static MongoMappingContext |
createMappingContext(ConnectionSources<MongoClient, MongoConnectionSourceSettings> connectionSources, java.lang.Class classes) |
|
protected static MongoMappingContext |
createMappingContext(org.springframework.core.env.PropertyResolver configuration, java.lang.Class classes) |
|
protected static MongoClient |
createMongoClient(org.springframework.core.env.PropertyResolver configuration, Builder mongoOptions, MongoMappingContext mappingContext) |
|
protected Session |
createSession(org.springframework.core.env.PropertyResolver connDetails) |
|
protected Session |
createStatelessSession(org.springframework.core.env.PropertyResolver connectionDetails) |
|
public java.util.List<UndeclaredIndex> |
dropUndeclaredIndexes()Drops every index that findUndeclaredIndexes() reports. |
|
public java.util.List<UndeclaredIndex> |
dropUndeclaredIndexes(java.util.List<UndeclaredIndex> indexes)Drops the given indexes: those findUndeclaredIndexes() reported, once they have been reviewed. |
|
public java.util.List<MissingIndex> |
findMissingIndexes()Lists the indexes the domain classes declare that their collections do not have, on the collections whose declared indexes buildIndex() builds for this datastore's connection. |
|
public java.util.List<UndeclaredIndex> |
findUndeclaredIndexes()Lists the indexes that no domain class declares, on the collections whose declared indexes buildIndex() builds for this datastore's connection. |
|
public ConfigurableApplicationEventPublisher |
getApplicationEventPublisher()
|
|
public AutoTimestampEventListener |
getAutoTimestampEventListener() |
|
public CodecRegistry |
getCodecRegistry()
|
|
public com.mongodb.client.MongoCollection<Document> |
getCollection(PersistentEntity entity)Obtain the raw com.mongodb.client.MongoCollection for the given entity |
|
public java.lang.String |
getCollectionName(PersistentEntity entity)Gets the default collection name for the given entity |
|
public ConnectionSources<MongoClient, MongoConnectionSourceSettings> |
getConnectionSources()
|
|
public java.lang.String |
getDatabaseName(PersistentEntity entity) |
|
public MongoDatastore |
getDatastoreForConnection(java.lang.String connectionName) |
|
public Datastore |
getDatastoreForConnection(java.lang.String connectionName) |
|
public MongoDatastore |
getDatastoreForTenantId(java.io.Serializable tenantId) |
|
public java.lang.String |
getDefaultDatabase()
|
|
public jakarta.persistence.FlushModeType |
getDefaultFlushMode()
|
<D> |
protected GormInstanceApi<D> |
getInstanceApi(java.lang.Class<D> cls, java.lang.String qualifier) |
|
public MongoMappingContext |
getMappingContext()
|
|
public MongoClient |
getMongoClient()
|
|
public MultiTenancyMode |
getMultiTenancyMode() |
|
public PersistentEntityCodec |
getPersistentEntityCodec(PersistentEntity entity)Obtains a PersistentEntityCodec for the given entity |
|
public PersistentEntityCodec |
getPersistentEntityCodec(java.lang.Class entityClass)Obtains a PersistentEntityCodec for the given entity |
|
public int |
getPhase() |
<D> |
protected MongoStaticApi<D> |
getStaticApi(java.lang.Class<D> cls, java.lang.String qualifier) |
|
public TenantResolver |
getTenantResolver() |
|
public org.springframework.transaction.PlatformTransactionManager |
getTransactionManager()
|
<D> |
protected GormValidationApi<D> |
getValidationApi(java.lang.Class<D> cls, java.lang.String qualifier) |
|
protected MongoGormEnhancer |
initialize(MongoConnectionSourceSettings settings) |
|
protected MongoGormEnhancer |
initialize(MongoConnectionSourceSettings settings)Runs the initialization sequence |
|
protected void |
initializeIndices(PersistentEntity entity)Indexes any properties that are mapped with index:true. |
|
public boolean |
isBuildIndexes()Whether GORM creates and reconciles the indexes declared in the domain class mapping blocks by itself: when the datastore starts, and for connections and domain classes added later. |
|
public boolean |
isBuildIndexesAsync()Whether index builds run on a background thread instead of blocking the thread that starts them - the one GORM starts on startup, and any buildIndex() the application calls. |
|
public boolean |
isRunning() |
|
public boolean |
isSchemaless() |
|
public boolean |
isTransactionsEnabled()Whether GORM should use real MongoDB multi-document transactions (a server-side ClientSession) for transactional operations. |
|
public void |
newConnectionSource(ConnectionSource<MongoClient, MongoConnectionSourceSettings> connectionSource) |
|
public void |
persistentEntityAdded(PersistentEntity entity) |
|
public void |
persistentEntityAdded(PersistentEntity entity) |
|
protected void |
registerAllEntitiesWithEnhancer() |
|
protected void |
registerEntity(PersistentEntity entity) |
|
protected void |
registerEventListeners(ConfigurableApplicationEventPublisher eventPublisher) |
|
public java.io.Serializable |
resolveTenantIdentifier() |
|
public java.lang.Iterable<java.io.Serializable> |
resolveTenantIds() |
|
public void |
setCodecProviders(java.util.List<CodecProvider> codecProviders)Sets any additional codec providers |
|
public void |
setCodecRegistries(java.util.List<CodecRegistry> codecRegistries)Sets any additional codec registries |
|
public void |
setCodecs(java.util.List<Codec<?>> codecs)Sets any additional codecs |
|
public void |
setMessageSource(java.util.List<org.springframework.context.MessageSource> messageSources)The message source used for validation messages |
|
public void |
setMessageSource(org.springframework.context.MessageSource messageSource) |
|
public void |
start()Connects the datastore, which is the first point at which it opens a socket. |
|
public void |
stop()Stops the MongoClient of every connection, so the process can be checkpointed. |
<T1> |
public T1 |
withNewSession(java.io.Serializable tenantId, groovy.lang.Closure<T1> callable) |
| Methods inherited from class | Name |
|---|---|
class AbstractDatastore |
connect, connect, connectStateless, createSession, createStatelessSession, destroy, doInSession, getApplicationContext, getApplicationEventPublisher, getCurrentSession, getMappingContext, getService, getServices, getSessionResolver, hasCurrentSession, initializeConverters, isIndexed, isSchemaless, mapToPropertyResolver, retrieveSession, retrieveSession, setApplicationContext, withSession |
TTL index attribute. MongoDB's IndexOptions#expireAfter(Long, TimeUnit)#expireAfter(Long, TimeUnit) is the only
way to set a TTL, but it is a two-argument setter that MongoConstants.mapToObject
cannot reach (that helper only invokes single-argument setters). So expireAfterSeconds
is pulled out of the index attributes and applied explicitly. TTL indexes are single-field
only — MongoDB silently ignores the option on a compound index.
Opt-in index attribute: when an index already exists on the same keys with conflicting options that cannot be reconciled in place (i.e. anything other than a TTL change), or the declared name is taken by an index on other keys, drop the existing index and recreate it with the declared options instead of just logging the conflict.
Below the web server's phase so the client outlives the requests using it, and above
EmbeddedMongoLifecycle.PHASE so an embedded server outlives this client:
Spring starts in ascending phase order and stops in descending.
Concurrent because it is written by the connection sources listener, which can add a child for a connection registered at runtime, while close() may be iterating it.
Not final because start() replaces it after a CRaC restore when it is not a RestartableMongoClient, which is restarted in place instead. Everything other than construction reaches it through getMongoClient(), so a replacement is picked up without anything else having to be told.
Configures a new MongoDatastore for the given arguments
connectionSources - The ConnectionSources to useeventPublisher - The Spring ApplicationContextmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
connectionSources - The ConnectionSources to useeventPublisher - The Spring ApplicationContextclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
mongoClient - The MongoClient instanceeventPublisher - The Spring ApplicationContextmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
mongoClient - The MongoClient instanceeventPublisher - The Spring ApplicationContextclasses - The persistent classes Configures a new MongoDatastore around the clients a supplier builds, which GORM owns: it builds one
the first time the client is needed, closes it when the datastore is stopped for a checkpoint, and builds the
one the restore needs from the same supplier. Use this where the client cannot be rebuilt from
grails.mongodb settings, such as one built from Spring Boot's own MongoClientSettings.
clientSupplier - Builds a MongoClient, whenever the datastore needs oneconfiguration - The configurationeventPublisher - The Spring ApplicationContextpackages - The packages to scanConfigures a new MongoDatastore around the clients a supplier builds; see MongoDatastore(Supplier, PropertyResolver, ConfigurableApplicationEventPublisher, Package...).
clientSupplier - Builds a MongoClient, whenever the datastore needs oneconfiguration - The configurationmappingContext - The mapping contexteventPublisher - The Spring ApplicationContextConfigures a new MongoDatastore for the given arguments
mongoClient - The MongoClient instanceeventPublisher - The Spring ApplicationContextpackages - The packages to scanConfigures a new MongoDatastore for the given arguments
mongoClient - The MongoClient instanceclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
mongoClient - The MongoClient instancepackages - The packages to scanConfigures a new MongoDatastore for the given arguments
mongoClient - The MongoClient instanceclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
clientOptions - The MongoClientSettings instanceconfiguration - The configurationeventPublisher - The Spring ApplicationContextmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
clientOptions - The MongoClientSettings instanceconfiguration - The configurationmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
configuration - The configuration for the datastoreeventPublisher - The Spring ApplicationContextmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
configuration - The configuration for the datastoreeventPublisher - The Spring ApplicationContextconnectionSourceFactory - The connection source factory to useclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
configuration - The configuration for the datastoreeventPublisher - The Spring ApplicationContextclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
configuration - The configuration for the datastoremappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
configuration - The configuration for the datastoreclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
configuration - The configurationeventPublisher - The event publisherclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
configuration - The configurationclasses - The persistent classesCreates a MongoDatastore with the given configuration
configuration - The configurationConfigures a new MongoDatastore for the given arguments
mappingContext - The MongoMappingContextConfigures a new MongoDatastore for the given arguments
classes - The persistent classesConstruct a Mongo datastore scanning the given packages
packagesToScan - The packages to scanConstruct a Mongo datastore scanning the given package
packageToScan - The packages to scanConstruct a Mongo datastore scanning the given packages
configuration - The configurationpackagesToScan - The packages to scanConstruct a Mongo datastore scanning the given packages
configuration - The configurationpackagesToScan - The packages to scanConstruct a Mongo datastore scanning the given packages
configuration - The configurationeventPublisher - The event publisherpackagesToScan - The packages to scanCreates and reconciles the indexes declared by the domain classes mapped to this datastore's connection.
GORM calls this itself when the datastore starts, unless grails.mongodb.buildIndexes is
false; that setting only stops GORM building by itself, so an application that leaves indexes
alone at startup can call this when it chooses to build them. Each named connection builds its own
domain classes: call it on getDatastoreForConnection(String) for those.
Each index is created by a command that the server answers only once the index has been built,
so this blocks the calling thread for as long as MongoDB takes to build every declared index. With
grails.mongodb.buildIndexesAsync enabled the work goes to a background thread and this
returns immediately instead. buildIndexAsync() runs it in the background whatever the setting,
and reports the outcome to its caller. Builds on a connection run one at a time, wherever they run: a
build on the calling thread first waits for one running in the background, and the other way round, and a
domain class registered after startup is indexed once neither is running.
Runs the same build as buildIndex() on this connection's background index build thread, whatever
grails.mongodb.buildIndexesAsync says, and returns at once with a handle on its outcome.
The future completes with the build's IndexBuildResult once every declaration has been applied, including when some of them failed: those are counted in IndexBuildResult.failures and logged as they happen. It completes exceptionally with the exception that stopped the build partway, such as a lost connection or a write concern the server could not satisfy. Created and already-present indexes are always told apart, whatever the log level.
Builds on a connection run one at a time, so a build requested while another is running waits for it, including one running on a caller's thread with buildIndex(), or one indexing a domain class registered after startup. Cancelling the future does not stop the build. If the datastore is stopped or closed, the future completes exceptionally at once, and a build that stopping or closing the datastore cuts short completes exceptionally too; a restart runs the cut-short build again, without a future. Each named connection builds its own domain classes: call this on getDatastoreForConnection(String) for those.
Creates the connection sources for a MongoClient.
mongoClient - The MongoClientconfiguration - The configurationmappingContext - The MongoMappingContextcloseable - whether GORM owns the client and should close it on shutdown. Pass
false for an externally-supplied client (its lifecycle is owned by the
caller, e.g. a Spring-managed bean) and true for a client GORM created
itself, so it is not leaked.Drops every index that findUndeclaredIndexes() reports.
Run this deliberately, once every instance of the application runs the release whose domain classes declare the indexes to keep. An instance still on an earlier release does not declare what a later release added, nor an index created by hand ahead of a deployment, so to it those indexes are undeclared and this drops them.
Drops the given indexes: those findUndeclaredIndexes() reported, once they have been reviewed.
Each index is checked again before it is dropped, since the reviewed list can be older than what is on
the server: one is dropped only while the index of that name is on the keys reviewed, its collection is
mapped on this datastore's connection, and no domain class mapped to it declares those keys. An index that
no longer exists, or whose collection no longer does, is skipped, and so is one that fails a check, which
is logged at WARN.
indexes - the indexes to dropLists the indexes the domain classes declare that their collections do not have, on the collections whose declared indexes buildIndex() builds for this datastore's connection.
A declared index is present when its collection has an index on the same key pattern, the same fields in the same order, whatever its name and options: an option that differs is the build's to reconcile. A text index is present when one indexes the same text fields, in any order, with the same keys before and after them, whatever its weights. Keys that several classes mapped to one collection declare are reported once. Nothing is changed: buildIndex() or buildIndexAsync() creates them. Each named connection has its own domain classes, so call this on getDatastoreForConnection(String) for those.
Lists the indexes that no domain class declares, on the collections whose declared indexes buildIndex() builds for this datastore's connection.
An index is declared when a domain class mapped to the same collection declares its key pattern, the
same fields in the same order, with compoundIndex, index or a property's
index: true. Its name and options do not matter. A text index is declared when a class declares
text on the same fields, in any order, with the same keys before and after them; its weights do not
matter. The _id index is never reported, and
neither is any index on a collection that no domain class maps. An index that an
initializeIndices(PersistentEntity) override creates by itself is not a declaration, so it is
reported.
Nothing is changed: see dropUndeclaredIndexes(). Each named connection has its own domain classes, so call this on getDatastoreForConnection(String) for those.
Obtain the raw com.mongodb.client.MongoCollection for the given entity
entity - The entityGets the default collection name for the given entity
entity - The entity
Obtains a PersistentEntityCodec for the given entity
entity - The entityObtains a PersistentEntityCodec for the given entity
entityClass - The entity class
Runs the initialization sequence
Indexes any properties that are mapped with index:true. Called for both startup builds and entities registered later, so subclasses can customise index creation on either path.
With grails.mongodb.buildIndexesAsync enabled the startup build calls this on a background
thread, and it can do so before the constructor of a subclass has finished. An override must not
depend on state that its own constructor or field initializers set up.
entity - The entity Whether GORM creates and reconciles the indexes declared in the domain class mapping blocks by itself:
when the datastore starts, and for connections and domain classes added later. Disabled with
grails.mongodb.buildIndexes = false, which leaves the indexes on the server as they are until
buildIndex() is called.
true if GORM builds the declared indexes by itself Whether index builds run on a background thread instead of blocking the thread that starts them - the
one GORM starts on startup, and any buildIndex() the application calls. Enabled with
grails.mongodb.buildIndexesAsync = true.
true if declared indexes are built asynchronously Whether GORM should use real MongoDB multi-document transactions (a server-side
ClientSession) for transactional operations. This is opt-in via
grails.mongodb.transactional and additionally requires a replica set or sharded
cluster; if a standalone topology is positively detected the feature is disabled (with a
one-time warning) and GORM falls back to the legacy client-side flush behavior.
true if server-side transactions should be usedSets any additional codec providers
codecProviders - The CodecProvider instancesSets any additional codec registries
codecRegistries - The CodecRegistry instancesSets any additional codecs
codecs - The Codec instancesThe message source used for validation messages
messageSources - The message sourceConnects the datastore, which is the first point at which it opens a socket.
The first start connects the client of every connection GORM owns and builds the indexes the domain
classes declare, unless grails.mongodb.buildIndexes is false. Nothing before it connects: the
clients GORM creates are RestartableMongoClients, which connect when first used, and building the
datastore builds no index. So a datastore created while an application context refreshes holds no socket
until Spring starts it in LIFECYCLE_PHASE, which is what lets the process be checkpointed with CRaC as
the context refreshes (spring.context.checkpoint=onRefresh). Spring starts it before it publishes
ContextRefreshedEvent, so the indexes are in place before BootStrap runs and before the web
server accepts a request. A datastore that nothing starts - one built outside an application context - starts
itself the first time a session is opened on it.
Starting after stop() brings back each MongoClient it stopped: a RestartableMongoClient builds a new driver client and stays the one handed out, and any other client is replaced by one built by the same factory the original was built with, so settings applied at startup still apply. A replacement is handed out by the connection's ConnectionSource as well as by this datastore, when that is a MongoConnectionSource. The indexes are not built again, since they outlive a checkpoint on the server, but a background index build that stop() cut short, or that was requested while stopped, runs again on a fresh executor, on every connection. A connection added while the datastore was stopped is connected and has its indexes built here.
A connection registered while this runs is not left out. One registered by another thread waits for it to finish and is then connected and built as the running datastore's connections are; one registered by this thread, from an index build hook, is taken up by another pass before this returns.
Stops the MongoClient of every connection, so the process can be checkpointed.
CRaC refuses to checkpoint a process holding open sockets, and a connected driver
holds one per pooled connection plus its server monitors. Closing the driver client shuts
the monitor threads down and releases every socket, which nothing else in the driver
offers: draining the pool leaves the monitors connected. Each connection declared under
grails.mongodb.connections, or added at runtime, has a client of its own, and
each is stopped.
A RestartableMongoClient, which is what GORM creates, closes its driver client and
stays the client the datastore hands out, so whatever holds it - the mongo bean among
them - works again once start() has restarted it. Any other client GORM owns is
closed, and start() builds a replacement.
Every client is stopped whether or not the datastore has started: one used before it was started, or connected by a start() that then failed, holds sockets as much as one the datastore started. Until start() is called again the clients refuse to be used, and using the datastore does not start it.
A client the application supplied is left alone. Its lifecycle belongs to whoever created it, and start() does not replace it. A datastore that owns none of its clients stays running.
A background index build still running, on this connection or any other, is interrupted first rather than left to fail against a closed client, and start() runs it again.