Skip to content

Commit c0b82fc

Browse files
committed
Polishing.
1 parent b233c8f commit c0b82fc

File tree

4 files changed

+43
-58
lines changed

4 files changed

+43
-58
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/TransientClientSessionException.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,23 @@
1616
package org.springframework.data.mongodb;
1717

1818
import org.springframework.dao.TransientDataAccessException;
19-
import org.springframework.lang.Nullable;
2019

2120
/**
2221
* {@link TransientDataAccessException} specific to MongoDB {@link com.mongodb.session.ClientSession} related data
2322
* access failures such as reading data using an already closed session.
2423
*
2524
* @author Christoph Strobl
26-
* @since 3.3
25+
* @since 4.4
2726
*/
2827
public class TransientClientSessionException extends TransientMongoDbException {
2928

3029
/**
3130
* Constructor for {@link TransientClientSessionException}.
3231
*
33-
* @param msg the detail message. Can be {@literal null}.
34-
* @param cause the root cause. Can be {@literal null}.
32+
* @param msg the detail message.
33+
* @param cause the root cause.
3534
*/
36-
public TransientClientSessionException(@Nullable String msg, @Nullable Throwable cause) {
35+
public TransientClientSessionException(String msg, Throwable cause) {
3736
super(msg, cause);
3837
}
3938
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/TransientMongoDbException.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,24 @@
1616
package org.springframework.data.mongodb;
1717

1818
import org.springframework.dao.TransientDataAccessException;
19-
import org.springframework.lang.Nullable;
2019

2120
/**
2221
* Root of the hierarchy of MongoDB specific data access exceptions that are considered transient such as
2322
* {@link com.mongodb.MongoException MongoExceptions} carrying {@link com.mongodb.MongoException#hasErrorLabel(String)
2423
* specific labels}.
2524
*
2625
* @author Christoph Strobl
27-
* @since 3.3
26+
* @since 4.4
2827
*/
2928
public class TransientMongoDbException extends TransientDataAccessException {
3029

3130
/**
3231
* Constructor for {@link TransientMongoDbException}.
3332
*
34-
* @param msg the detail message. Can be {@literal null}.
35-
* @param cause the root cause. Can be {@literal null}.
33+
* @param msg the detail message.
34+
* @param cause the root cause.
3635
*/
37-
public TransientMongoDbException(String msg, @Nullable Throwable cause) {
36+
public TransientMongoDbException(String msg, Throwable cause) {
3837
super(msg, cause);
3938
}
4039
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoExceptionTranslator.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
import java.util.Set;
1919

2020
import org.bson.BsonInvalidOperationException;
21+
2122
import org.springframework.dao.DataAccessException;
2223
import org.springframework.dao.DataAccessResourceFailureException;
2324
import org.springframework.dao.DataIntegrityViolationException;
2425
import org.springframework.dao.DuplicateKeyException;
2526
import org.springframework.dao.InvalidDataAccessApiUsageException;
2627
import org.springframework.dao.InvalidDataAccessResourceUsageException;
2728
import org.springframework.dao.PermissionDeniedDataAccessException;
28-
import org.springframework.dao.TransientDataAccessException;
2929
import org.springframework.dao.support.PersistenceExceptionTranslator;
3030
import org.springframework.data.mongodb.ClientSessionException;
3131
import org.springframework.data.mongodb.TransientClientSessionException;
32-
import org.springframework.data.mongodb.TransientMongoDbException;
3332
import org.springframework.data.mongodb.UncategorizedMongoDbException;
3433
import org.springframework.data.mongodb.util.MongoDbErrorCodes;
3534
import org.springframework.lang.Nullable;
@@ -70,18 +69,7 @@ public class MongoExceptionTranslator implements PersistenceExceptionTranslator
7069
@Override
7170
@Nullable
7271
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
73-
74-
DataAccessException translatedException = doTranslateException(ex);
75-
if (translatedException == null) {
76-
return null;
77-
}
78-
79-
// Translated exceptions that per se are not be recoverable (eg. WriteConflicts), might still be transient inside a
80-
// transaction. Let's wrap those.
81-
return (isTransientFailure(ex) && !(translatedException instanceof TransientDataAccessException))
82-
? new TransientMongoDbException(ex.getMessage(), translatedException)
83-
: translatedException;
84-
72+
return doTranslateException(ex);
8573
}
8674

8775
@Nullable
@@ -187,13 +175,13 @@ DataAccessException doTranslateException(RuntimeException ex) {
187175
/**
188176
* Check if a given exception holds an error label indicating a transient failure.
189177
*
190-
* @param e
178+
* @param e the exception to inspect.
191179
* @return {@literal true} if the given {@link Exception} is a {@link MongoException} holding one of the transient
192180
* exception error labels.
193181
* @see MongoException#hasErrorLabel(String)
194-
* @since 3.3
182+
* @since 4.4
195183
*/
196-
public static boolean isTransientFailure(Exception e) {
184+
public boolean isTransientFailure(Exception e) {
197185

198186
if (!(e instanceof MongoException)) {
199187
return false;

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/MongoDbErrorCodes.java

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ public final class MongoDbErrorCodes {
128128
clientSessionCodes.put(263, "OperationNotSupportedInTransaction");
129129
clientSessionCodes.put(264, "TooManyLogicalSessions");
130130

131-
errorCodes = new HashMap<>();
131+
errorCodes = new HashMap<>(
132+
dataAccessResourceFailureCodes.size() + dataIntegrityViolationCodes.size() + duplicateKeyCodes.size()
133+
+ invalidDataAccessApiUsageException.size() + permissionDeniedCodes.size() + clientSessionCodes.size(),
134+
1f);
132135
errorCodes.putAll(dataAccessResourceFailureCodes);
133136
errorCodes.putAll(dataIntegrityViolationCodes);
134137
errorCodes.putAll(duplicateKeyCodes);
@@ -149,12 +152,12 @@ public static boolean isDataIntegrityViolationCode(@Nullable Integer errorCode)
149152
/**
150153
* @param exception can be {@literal null}.
151154
* @return
152-
* @since 3.3
155+
* @since 4.4
153156
*/
154-
public static boolean isDataIntegrityViolationError(@Nullable Exception exception) {
157+
public static boolean isDataIntegrityViolationError(Exception exception) {
155158

156-
if(exception instanceof MongoException) {
157-
return isDataIntegrityViolationCode(((MongoException) exception).getCode());
159+
if (exception instanceof MongoException me) {
160+
return isDataIntegrityViolationCode(me.getCode());
158161
}
159162
return false;
160163
}
@@ -166,12 +169,12 @@ public static boolean isDataAccessResourceFailureCode(@Nullable Integer errorCod
166169
/**
167170
* @param exception can be {@literal null}.
168171
* @return
169-
* @since 3.3
172+
* @since 4.4
170173
*/
171-
public static boolean isDataAccessResourceError(@Nullable Exception exception) {
174+
public static boolean isDataAccessResourceError(Exception exception) {
172175

173-
if(exception instanceof MongoException) {
174-
return isDataAccessResourceFailureCode(((MongoException) exception).getCode());
176+
if (exception instanceof MongoException me) {
177+
return isDataAccessResourceFailureCode(me.getCode());
175178
}
176179
return false;
177180
}
@@ -183,27 +186,23 @@ public static boolean isDuplicateKeyCode(@Nullable Integer errorCode) {
183186
/**
184187
* @param exception can be {@literal null}.
185188
* @return
186-
* @since 3.3
189+
* @since 4.4
187190
*/
188-
public static boolean isDuplicateKeyError(@Nullable Exception exception) {
191+
public static boolean isDuplicateKeyError(Exception exception) {
189192

190-
if(exception instanceof MongoException) {
191-
return isDuplicateKeyCode(((MongoException) exception).getCode());
193+
if (exception instanceof MongoException me) {
194+
return isDuplicateKeyCode(me.getCode());
192195
}
193196
return false;
194197
}
195198

196199
/**
197200
* @param exception can be {@literal null}.
198201
* @return
199-
* @since 3.3
202+
* @since 4.4
200203
*/
201-
public static boolean isDataDuplicateKeyError(@Nullable Exception exception) {
202-
203-
if(exception instanceof MongoException) {
204-
return isDuplicateKeyCode(((MongoException) exception).getCode());
205-
}
206-
return false;
204+
public static boolean isDataDuplicateKeyError(Exception exception) {
205+
return isDuplicateKeyError(exception);
207206
}
208207

209208
public static boolean isPermissionDeniedCode(@Nullable Integer errorCode) {
@@ -213,11 +212,11 @@ public static boolean isPermissionDeniedCode(@Nullable Integer errorCode) {
213212
/**
214213
* @param exception can be {@literal null}.
215214
* @return
216-
* @since 3.3
215+
* @since 4.4
217216
*/
218-
public static boolean isPermissionDeniedError(@Nullable Exception exception) {
217+
public static boolean isPermissionDeniedError(Exception exception) {
219218

220-
if(exception instanceof MongoException) {
219+
if (exception instanceof MongoException) {
221220
return isPermissionDeniedCode(((MongoException) exception).getCode());
222221
}
223222
return false;
@@ -230,12 +229,12 @@ public static boolean isInvalidDataAccessApiUsageCode(@Nullable Integer errorCod
230229
/**
231230
* @param exception can be {@literal null}.
232231
* @return
233-
* @since 3.3
232+
* @since 4.4
234233
*/
235-
public static boolean isInvalidDataAccessApiUsageError(@Nullable Exception exception) {
234+
public static boolean isInvalidDataAccessApiUsageError(Exception exception) {
236235

237-
if(exception instanceof MongoException) {
238-
return isInvalidDataAccessApiUsageCode(((MongoException) exception).getCode());
236+
if (exception instanceof MongoException me) {
237+
return isInvalidDataAccessApiUsageCode(me.getCode());
239238
}
240239
return false;
241240
}
@@ -265,12 +264,12 @@ public static boolean isTransactionFailureCode(@Nullable Integer errorCode) {
265264
/**
266265
* @param exception can be {@literal null}.
267266
* @return
268-
* @since 3.3
267+
* @since 4.4
269268
*/
270-
public static boolean isClientSessionFailure(@Nullable Exception exception) {
269+
public static boolean isClientSessionFailure(Exception exception) {
271270

272-
if(exception instanceof MongoException) {
273-
return isClientSessionFailureCode(((MongoException) exception).getCode());
271+
if (exception instanceof MongoException me) {
272+
return isClientSessionFailureCode(me.getCode());
274273
}
275274
return false;
276275
}

0 commit comments

Comments
 (0)