Skip to content

Commit 31fe900

Browse files
committed
Add documentation.
1 parent 3dd86c1 commit 31fe900

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/main/antora/modules/ROOT/pages/mongodb/template-api.adoc

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ The `execute` callbacks gives you a reference to either a `MongoCollection` or a
3131

3232
* `<T> T` *execute* `(String collectionName, CollectionCallback<T> action)`: Runs the given `CollectionCallback` on the collection of the given name.
3333

34-
* `<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.
34+
* `<T> T` *execute* `(DbCallback<T> action)`: Runs a DbCallback, translating any exceptions as necessary.
35+
Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2.
3536

3637
* `<T> T` *execute* `(String collectionName, DbCallback<T> action)`: Runs a `DbCallback` on the collection of the given name translating any exceptions as necessary.
3738

@@ -90,6 +91,7 @@ List<Jedi> all = template.query(SWCharacter.class) <1>
9091
.matching(query(where("jedi").is(true))) <4>
9192
.all();
9293
----
94+
9395
<1> The type used to map fields used in the query to.
9496
<2> The collection name to use if not defined on the domain type.
9597
<3> Result type if not using the original domain type.
@@ -107,9 +109,8 @@ Flux<Jedi> all = template.query(SWCharacter.class)
107109
----
108110
======
109111

110-
NOTE: Using projections allows `MongoTemplate` to optimize result mapping by limiting the actual response to fields required
111-
by the projection target type. This applies as long as the javadoc:org.springframework.data.mongodb.core.query.Query[] itself does not contain any field restriction and the
112-
target type is a closed interface or DTO projection.
112+
NOTE: Using projections allows `MongoTemplate` to optimize result mapping by limiting the actual response to fields required by the projection target type.
113+
This applies as long as the javadoc:org.springframework.data.mongodb.core.query.Query[] itself does not contain any field restriction and the target type is a closed interface or DTO projection.
113114

114115
WARNING: Projections must not be applied to xref:mongodb/mapping/document-references.adoc[DBRefs].
115116

@@ -143,18 +144,34 @@ Flux<GeoResult<Jedi>> results = template.query(SWCharacter.class)
143144
[[mongo-template.exception-translation]]
144145
== Exception Translation
145146

146-
The Spring framework provides exception translation for a wide variety of database and mapping technologies. T
147-
his has traditionally been for JDBC and JPA.
147+
The Spring framework provides exception translation for a wide variety of database and mapping technologies.
148+
This has traditionally been for JDBC and JPA.
148149
The Spring support for MongoDB extends this feature to the MongoDB Database by providing an implementation of the `org.springframework.dao.support.PersistenceExceptionTranslator` interface.
149150

150151
The motivation behind mapping to Spring's link:{springDocsUrl}/data-access.html#dao-exceptions[consistent data access exception hierarchy] is that you are then able to write portable and descriptive exception handling code without resorting to coding against MongoDB error codes.
151152
All of Spring's data access exceptions are inherited from the root `DataAccessException` class so that you can be sure to catch all database related exception within a single try-catch block.
152153
Note that not all exceptions thrown by the MongoDB driver inherit from the `MongoException` class.
153154
The inner exception and message are preserved so that no information is lost.
154155

155-
Some of the mappings performed by the `MongoExceptionTranslator` are `com.mongodb.Network to DataAccessResourceFailureException` and `MongoException` error codes 1003, 12001, 12010, 12011, and 12012 to `InvalidDataAccessApiUsageException`.
156+
Some of the mappings performed by the javadoc:org.springframework.data.mongodb.core.MongoExceptionTranslator[] are `com.mongodb.Network` to `DataAccessResourceFailureException` and `MongoException` error codes 1003, 12001, 12010, 12011, and 12012 to `InvalidDataAccessApiUsageException`.
156157
Look into the implementation for more details on the mapping.
157158

159+
Exception Translation can be configured by setting a customized javadoc:org.springframework.data.mongodb.core.MongoExceptionTranslator[] on your `MongoDatabaseFactory` or its reactive variant.
160+
You might also want to set the exception translator on the corresponding `MongoClientFactoryBean`.
161+
162+
.Configuring `MongoExceptionTranslator`
163+
====
164+
[source,java]
165+
----
166+
ConnectionString uri = new ConnectionString("mongodb://username:password@localhost/database");
167+
SimpleMongoClientDatabaseFactory mongoDbFactory = new SimpleMongoClientDatabaseFactory(uri);
168+
mongoDbFactory.setExceptionTranslator(myCustomExceptionTranslator);
169+
----
170+
====
171+
172+
A motivation to customize exception can be MongoDB's behavior during transactions where some failures (such as write conflicts) can become transient and where a retry could lead to a successful operation.
173+
In such a case, you could wrap exceptions with a specific MongoDB label and apply a different exception translation stragegy.
174+
158175
[[mongo-template.type-mapping]]
159176
== Domain Type Mapping
160177

0 commit comments

Comments
 (0)