You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/antora/modules/ROOT/pages/mongodb/template-api.adoc
+24-7Lines changed: 24 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,8 @@ The `execute` callbacks gives you a reference to either a `MongoCollection` or a
31
31
32
32
* `<T> T` *execute* `(String collectionName, CollectionCallback<T> action)`: Runs the given `CollectionCallback` on the collection of the given name.
33
33
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.
35
36
36
37
* `<T> T` *execute* `(String collectionName, DbCallback<T> action)`: Runs a `DbCallback` on the collection of the given name translating any exceptions as necessary.
37
38
@@ -90,6 +91,7 @@ List<Jedi> all = template.query(SWCharacter.class) <1>
90
91
.matching(query(where("jedi").is(true))) <4>
91
92
.all();
92
93
----
94
+
93
95
<1> The type used to map fields used in the query to.
94
96
<2> The collection name to use if not defined on the domain type.
95
97
<3> Result type if not using the original domain type.
@@ -107,9 +109,8 @@ Flux<Jedi> all = template.query(SWCharacter.class)
107
109
----
108
110
======
109
111
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.
113
114
114
115
WARNING: Projections must not be applied to xref:mongodb/mapping/document-references.adoc[DBRefs].
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.
148
149
The Spring support for MongoDB extends this feature to the MongoDB Database by providing an implementation of the `org.springframework.dao.support.PersistenceExceptionTranslator` interface.
149
150
150
151
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.
151
152
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.
152
153
Note that not all exceptions thrown by the MongoDB driver inherit from the `MongoException` class.
153
154
The inner exception and message are preserved so that no information is lost.
154
155
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`.
156
157
Look into the implementation for more details on the mapping.
157
158
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);
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.
0 commit comments