public class MongoDatastore extends AbstractDatastore implements MappingContextListener, java.io.Closeable, MultiTenantCapableDatastore, MultipleConnectionSourceCapableDatastore, StatelessDatastore, TransactionCapableDatastore, 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 org.bson.codecs.configuration.CodecRegistry |
codecRegistry |
|
protected ConnectionSources<com.mongodb.client.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 FlushModeType |
defaultFlushMode |
|
protected ConfigurableApplicationEventPublisher |
eventPublisher |
|
protected GormEnhancer |
gormEnhancer |
|
protected com.mongodb.client.MongoClient |
mongo |
Not final because start() replaces it after a CRaC restore. |
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 PlatformTransactionManager |
transactionManager |
|
protected boolean |
transactionsEnabled |
| Fields inherited from class | Fields |
|---|---|
class AbstractDatastore |
LOG, applicationEventPublisher, cacheAdapterRepository, connectionDetails, mappingContext, serviceRegistry |
| Constructor and description |
|---|
MongoDatastore(ConnectionSources<com.mongodb.client.MongoClient, MongoConnectionSourceSettings> connectionSources, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(ConnectionSources<com.mongodb.client.MongoClient, MongoConnectionSourceSettings> connectionSources, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(com.mongodb.client.MongoClient mongoClient, PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(com.mongodb.client.MongoClient mongoClient, PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(java.util.function.Supplier<com.mongodb.client.MongoClient> clientSupplier, PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Package packages)Configures a new MongoDatastore around the clients a supplier builds, which GORM owns: it builds one now, closes it when the datastore is stopped for a checkpoint, and builds the replacement the restore needs from the same supplier. |
MongoDatastore(java.util.function.Supplier<com.mongodb.client.MongoClient> clientSupplier, PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore around the clients a supplier builds; see MongoDatastore(Supplier, PropertyResolver, ConfigurableApplicationEventPublisher, Package...). |
MongoDatastore(com.mongodb.client.MongoClient mongoClient, PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Package packages)Configures a new MongoDatastore for the given arguments |
MongoDatastore(com.mongodb.client.MongoClient mongoClient, PropertyResolver configuration, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(com.mongodb.client.MongoClient mongoClient, PropertyResolver configuration, java.lang.Package packages)Configures a new MongoDatastore for the given arguments |
MongoDatastore(com.mongodb.client.MongoClient mongoClient, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(com.mongodb.MongoClientSettings$Builder clientOptions, PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(com.mongodb.MongoClientSettings$Builder clientOptions, PropertyResolver configuration, MongoMappingContext mappingContext)Configures a new MongoDatastore for the given arguments |
MongoDatastore(PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(PropertyResolver configuration, MongoConnectionSourceFactory connectionSourceFactory, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(PropertyResolver configuration, MongoMappingContext mappingContext)Configures a new MongoDatastore for the given arguments |
MongoDatastore(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(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(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 void |
close() |
|
protected static ConnectionSources<com.mongodb.client.MongoClient, MongoConnectionSourceSettings> |
createDefaultConnectionSources(com.mongodb.client.MongoClient mongoClient, PropertyResolver configuration, MongoMappingContext mappingContext, boolean closeable)Creates the connection sources for a com.mongodb.client.MongoClient. |
|
protected static MongoMappingContext |
createMappingContext(ConnectionSources<com.mongodb.client.MongoClient, MongoConnectionSourceSettings> connectionSources, java.lang.Class classes) |
|
protected static MongoMappingContext |
createMappingContext(PropertyResolver configuration, java.lang.Class classes) |
|
protected static com.mongodb.client.MongoClient |
createMongoClient(PropertyResolver configuration, com.mongodb.MongoClientSettings$Builder mongoOptions, MongoMappingContext mappingContext) |
|
protected Session |
createSession(PropertyResolver connDetails) |
|
protected Session |
createStatelessSession(PropertyResolver connectionDetails) |
|
public ConfigurableApplicationEventPublisher |
getApplicationEventPublisher()
|
|
public AutoTimestampEventListener |
getAutoTimestampEventListener() |
|
public org.bson.codecs.configuration.CodecRegistry |
getCodecRegistry()
|
|
public com.mongodb.client.MongoCollection<org.bson.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<com.mongodb.client.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 FlushModeType |
getDefaultFlushMode()
|
<D> |
protected GormInstanceApi<D> |
getInstanceApi(java.lang.Class<D> cls, java.lang.String qualifier) |
|
public MongoMappingContext |
getMappingContext()
|
|
public com.mongodb.client.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 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<com.mongodb.client.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<org.bson.codecs.configuration.CodecProvider> codecProviders)Sets any additional codec providers |
|
public void |
setCodecRegistries(java.util.List<org.bson.codecs.configuration.CodecRegistry> codecRegistries)Sets any additional codec registries |
|
public void |
setCodecs(java.util.List<org.bson.codecs.Codec<?>> codecs)Sets any additional codecs |
|
public void |
setMessageSource(java.util.List<MessageSource> messageSources)The message source used for validation messages |
|
public void |
setMessageSource(MessageSource messageSource) |
|
public void |
start()Builds a replacement for each com.mongodb.client.MongoClient that stop() closed, using the same factory the original was built with, so settings applied at startup still apply. |
|
public void |
stop()Closes the com.mongodb.client.MongoClient of every connection, so the process can be checkpointed. |
<T1> |
public T1 |
withNewSession(java.io.Serializable tenantId, 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 com.mongodb.client.model.IndexOptions#expireAfter(java.lang.Long, java.util.concurrent.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. 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 com.mongodb.client.MongoClient instanceeventPublisher - The Spring ApplicationContextmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
mongoClient - The com.mongodb.client.MongoClient instanceeventPublisher - The Spring ApplicationContextclasses - The persistent classes Configures a new MongoDatastore around the clients a supplier builds, which GORM owns: it builds one
now, closes it when the datastore is stopped for a checkpoint, and builds the replacement 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 com.mongodb.client.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 com.mongodb.client.MongoClient, whenever the datastore needs oneconfiguration - The configurationmappingContext - The mapping contexteventPublisher - The Spring ApplicationContextConfigures a new MongoDatastore for the given arguments
mongoClient - The com.mongodb.client.MongoClient instanceeventPublisher - The Spring ApplicationContextpackages - The packages to scanConfigures a new MongoDatastore for the given arguments
mongoClient - The com.mongodb.client.MongoClient instanceclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
mongoClient - The com.mongodb.client.MongoClient instancepackages - The packages to scanConfigures a new MongoDatastore for the given arguments
mongoClient - The com.mongodb.client.MongoClient instanceclasses - The persistent classesConfigures a new MongoDatastore for the given arguments
clientOptions - The com.mongodb.MongoClientSettings instanceconfiguration - The configurationeventPublisher - The Spring ApplicationContextmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
clientOptions - The com.mongodb.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.
Creates the connection sources for a com.mongodb.client.MongoClient.
mongoClient - The com.mongodb.client.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.
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 org.bson.codecs.configuration.CodecProvider instancesSets any additional codec registries
codecRegistries - The org.bson.codecs.configuration.CodecRegistry instancesSets any additional codecs
codecs - The org.bson.codecs.Codec instancesThe message source used for validation messages
messageSources - The message sourceBuilds a replacement for each com.mongodb.client.MongoClient that stop() closed, using the same factory the original was built with, so settings applied at startup still apply. The replacement is handed out by the connection's ConnectionSource as well as by this datastore, when that is the MongoConnectionSource the factory creates.
A background index build that stop() cut short, or that was requested while stopped, runs again on a fresh executor, on every connection.
Closes the com.mongodb.client.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 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 closed.
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.