move execution callbacks to main API section

This commit is contained in:
Christoph Strobl
2023-09-05 09:12:24 +02:00
parent da0d49d261
commit c08176ac85
3 changed files with 43 additions and 40 deletions

View File

@@ -7,27 +7,42 @@
* xref:mongodb.adoc[]
** xref:mongodb/getting-started.adoc[]
** xref:mongodb/configuration.adoc[]
// Template
** xref:mongodb/template-api.adoc[]
*** xref:mongodb/template-config.adoc[]
*** xref:mongodb/template-crud-operations.adoc[]
*** xref:mongodb/template-query-operations.adoc[]
**** xref:mongodb/template-query-options.adoc[]
*** xref:mongodb/template-document-count.adoc[]
*** xref:mongodb/aggregation-framework.adoc[]
// still needed???
*** xref:mongodb/mongo-mapreduce.adoc[]
*** xref:mongodb/mongo-server-side-scripts.adoc[]
*** xref:mongodb/mongo-group.adoc[]
// Mapping
// Repository
// Mongo Specifics
*** xref:mongodb/mongo-template-index-and-collections.adoc[]
*** xref:mongodb/mongo-template-commands.adoc[]
*** xref:mongodb/mapping-usage-events.adoc[]
*** xref:mongodb/mongo-exception.adoc[]
*** xref:mongodb/mongo-executioncallback.adoc[]
** xref:mongodb/mongo-json-schema.adoc[]
** xref:mongodb/aggregation-framework.adoc[]
** xref:mongodb/mongo-entity-callbacks.adoc[]
** xref:mongodb/gridfs.adoc[]
** xref:mongodb/tailable-cursors.adoc[]

View File

@@ -1,38 +0,0 @@
[[mongo.executioncallback]]
= Execution Callbacks
One common design feature of all Spring template classes is that all functionality is routed into one of the template's `execute` callback methods. Doing so helps to ensure that exceptions and any resource management that may be required are performed consistently. While JDBC and JMS need this feature much more than MongoDB does, it still offers a single spot for exception translation and logging to occur. Consequently, using these `execute` callbacks is the preferred way to access the MongoDB driver's `MongoDatabase` and `MongoCollection` objects to perform uncommon operations that were not exposed as methods on `MongoTemplate`.
The following list describes the `execute` callback methods.
* `<T> T` *execute* `(Class<?> entityClass, CollectionCallback<T> action)`: Runs the given `CollectionCallback` for the entity collection of the specified class.
* `<T> T` *execute* `(String collectionName, CollectionCallback<T> action)`: Runs the given `CollectionCallback` on the collection of the given name.
* `<T> T` *execute* `(DbCallback<T> action)`: Runs a DbCallback, translating any exceptions as necessary. Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2.
* `<T> T` *execute* `(String collectionName, DbCallback<T> action)`: Runs a `DbCallback` on the collection of the given name translating any exceptions as necessary.
* `<T> T` *executeInSession* `(DbCallback<T> action)`: Runs the given `DbCallback` within the same connection to the database so as to ensure consistency in a write-heavy environment where you may read the data that you wrote.
The following example uses the `CollectionCallback` to return information about an index:
[source,java]
----
boolean hasIndex = template.execute("geolocation", new CollectionCallbackBoolean>() {
public Boolean doInCollection(Venue.class, DBCollection collection) throws MongoException, DataAccessException {
List<Document> indexes = collection.getIndexInfo();
for (Document document : indexes) {
if ("location_2d".equals(document.get("name"))) {
return true;
}
}
return false;
}
});
----
include:../:gridfs.adoc[]
include:../:tailable-cursors.adoc[]
include:../:change-streams.adoc[]
include:../:time-series.adoc[]

View File

@@ -29,7 +29,33 @@ See "`xref:mongodb/mongo-exception.adoc[Exception Translation]`" for more inform
`MongoTemplate` offers many convenience methods to help you easily perform common tasks.
However, if you need to directly access the MongoDB driver API, you can use one of several `Execute` callback methods.
The `execute` callbacks gives you a reference to either a `MongoCollection` or a `MongoDatabase` object.
See the xref:mongodb/mongo-executioncallback.adoc["`Execution Callbacks`"] section for more information.
* `<T> T` *execute* `(Class<?> entityClass, CollectionCallback<T> action)`: Runs the given `CollectionCallback` for the entity collection of the specified class.
* `<T> T` *execute* `(String collectionName, CollectionCallback<T> action)`: Runs the given `CollectionCallback` on the collection of the given name.
* `<T> T` *execute* `(DbCallback<T> action)`: Runs a DbCallback, translating any exceptions as necessary. Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2.
* `<T> T` *execute* `(String collectionName, DbCallback<T> action)`: Runs a `DbCallback` on the collection of the given name translating any exceptions as necessary.
* `<T> T` *executeInSession* `(DbCallback<T> action)`: Runs the given `DbCallback` within the same connection to the database so as to ensure consistency in a write-heavy environment where you may read the data that you wrote.
The following example uses the `CollectionCallback` to return information about an index:
[source,java]
----
boolean hasIndex = template.execute("geolocation", new CollectionCallbackBoolean>() {
public Boolean doInCollection(Venue.class, DBCollection collection) throws MongoException, DataAccessException {
List<Document> indexes = collection.getIndexInfo();
for (Document document : indexes) {
if ("location_2d".equals(document.get("name"))) {
return true;
}
}
return false;
}
});
----
Being the central component when it comes to more low-level interaction with MongoDB `MongoTemplate` offers a wide range of methods covering needs from collection creation, index creation, and CRUD operations to more advanced functionality, such as Map-Reduce and aggregations.
You can find multiple overloads for each method.