public class MongoDatastore extends AbstractDatastore implements MappingContextListener, java.io.Closeable, MultiTenantCapableDatastore, MultipleConnectionSourceCapableDatastore, SmartLifecycle, StatelessDatastore, TransactionCapableDatastore
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), 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 |
codecEngine |
|
protected CodecRegistry |
codecRegistry |
|
protected ConnectionSources<MongoClient, MongoConnectionSourceSettings> |
connectionSources |
|
protected java.util.Map<java.lang.String, MongoDatastore> |
datastoresByConnectionSource |
|
protected java.lang.String |
defaultDatabase |
|
protected FlushModeType |
defaultFlushMode |
|
protected ConfigurableApplicationEventPublisher |
eventPublisher |
|
protected GormEnhancer |
gormEnhancer |
|
protected 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<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, PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, PropertyResolver configuration, ConfigurableApplicationEventPublisher eventPublisher, java.lang.Package packages)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, PropertyResolver configuration, java.lang.Class classes)Configures a new MongoDatastore for the given arguments |
MongoDatastore(MongoClient mongoClient, 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, PropertyResolver configuration, MongoMappingContext mappingContext, ConfigurableApplicationEventPublisher eventPublisher)Configures a new MongoDatastore for the given arguments |
MongoDatastore(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()Builds the MongoDB index for this datastore |
|
public void |
close() |
|
protected static ConnectionSources<MongoClient, MongoConnectionSourceSettings> |
createDefaultConnectionSources(MongoClient mongoClient, 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(PropertyResolver configuration, java.lang.Class classes) |
|
protected static MongoClient |
createMongoClient(PropertyResolver configuration, Builder mongoOptions, MongoMappingContext mappingContext) |
|
protected Session |
createSession(PropertyResolver connDetails) |
|
protected Session |
createStatelessSession(PropertyResolver connectionDetails) |
|
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 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 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 |
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<MessageSource> messageSources)The message source used for validation messages |
|
public void |
setMessageSource(MessageSource messageSource) |
|
public void |
start()Builds a replacement MongoClient after a restore, using the same factory and configuration the original was built from, so settings applied at startup still apply. |
|
public void |
stop()Closes the MongoClient 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), 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.
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 MongoClient instanceeventPublisher - The Spring ApplicationContextmappingContext - The mapping contextConfigures a new MongoDatastore for the given arguments
mongoClient - The MongoClient instanceeventPublisher - The Spring ApplicationContextclasses - The persistent classesConfigures 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 scanBuilds the MongoDB index for this datastore
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.
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
entity - The entity 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 sourceBuilds a replacement MongoClient after a restore, using the same factory and configuration the original was built from, so settings applied at startup still apply.
Closes the MongoClient 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.
A client the application supplied is left alone. Its lifecycle belongs to whoever created it, and this datastore stays running so that start() does not later replace something it does not own.