Skip to content

Commit 459dba9

Browse files
committed
Allow configuration of ExceptionTranslator in MongoDatabaseFactorySupport.
1 parent c0b82fc commit 459dba9

File tree

4 files changed

+28
-31
lines changed

4 files changed

+28
-31
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ public void setExceptionTranslator(@Nullable PersistenceExceptionTranslator exce
119119
this.exceptionTranslator = exceptionTranslator == null ? DEFAULT_EXCEPTION_TRANSLATOR : exceptionTranslator;
120120
}
121121

122+
@Override
122123
public Class<? extends MongoClient> getObjectType() {
123124
return MongoClient.class;
124125
}
125126

127+
@Override
126128
@Nullable
127129
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
128130
return exceptionTranslator.translateExceptionIfPossible(ex);

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232

3333
/**
3434
* Common base class for usage with both {@link com.mongodb.client.MongoClients} defining common properties such as
35-
* database name and exception translator.
36-
* <br />
35+
* database name and exception translator. <br />
3736
* Not intended to be used directly.
3837
*
3938
* @author Christoph Strobl
@@ -47,8 +46,8 @@ public abstract class MongoDatabaseFactorySupport<C> implements MongoDatabaseFac
4746
private final C mongoClient;
4847
private final String databaseName;
4948
private final boolean mongoInstanceCreated;
50-
private final PersistenceExceptionTranslator exceptionTranslator;
5149

50+
private PersistenceExceptionTranslator exceptionTranslator;
5251
private @Nullable WriteConcern writeConcern;
5352

5453
/**
@@ -78,12 +77,28 @@ protected MongoDatabaseFactorySupport(C mongoClient, String databaseName, boolea
7877
/**
7978
* Configures the {@link WriteConcern} to be used on the {@link MongoDatabase} instance being created.
8079
*
81-
* @param writeConcern the writeConcern to set
80+
* @param writeConcern the writeConcern to set.
8281
*/
8382
public void setWriteConcern(WriteConcern writeConcern) {
8483
this.writeConcern = writeConcern;
8584
}
8685

86+
/**
87+
* Configures the {@link PersistenceExceptionTranslator} to be used.
88+
*
89+
* @param exceptionTranslator the exception translator to set.
90+
* @since 4.4
91+
*/
92+
public void setExceptionTranslator(PersistenceExceptionTranslator exceptionTranslator) {
93+
this.exceptionTranslator = exceptionTranslator;
94+
}
95+
96+
@Override
97+
public PersistenceExceptionTranslator getExceptionTranslator() {
98+
return this.exceptionTranslator;
99+
}
100+
101+
@Override
87102
public MongoDatabase getMongoDatabase() throws DataAccessException {
88103
return getMongoDatabase(getDefaultDatabaseName());
89104
}
@@ -116,10 +131,7 @@ public void destroy() throws Exception {
116131
}
117132
}
118133

119-
public PersistenceExceptionTranslator getExceptionTranslator() {
120-
return this.exceptionTranslator;
121-
}
122-
134+
@Override
123135
public MongoDatabaseFactory withSession(ClientSession session) {
124136
return new MongoDatabaseFactorySupport.ClientSessionBoundMongoDbFactory(session, this);
125137
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,10 @@ DataAccessException doTranslateException(RuntimeException ex) {
183183
*/
184184
public boolean isTransientFailure(Exception e) {
185185

186-
if (!(e instanceof MongoException)) {
186+
if (!(e instanceof MongoException mongoException)) {
187187
return false;
188188
}
189189

190-
MongoException mongoException = (MongoException) e;
191-
192190
return mongoException.hasErrorLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL)
193191
|| mongoException.hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL);
194192
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoExceptionTranslatorUnitTests.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,16 @@
2020
import org.bson.BsonDocument;
2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
23-
2423
import org.mockito.Mockito;
24+
2525
import org.springframework.core.NestedRuntimeException;
2626
import org.springframework.dao.DataAccessException;
2727
import org.springframework.dao.DataAccessResourceFailureException;
28-
import org.springframework.dao.DataIntegrityViolationException;
2928
import org.springframework.dao.DuplicateKeyException;
3029
import org.springframework.dao.InvalidDataAccessApiUsageException;
3130
import org.springframework.dao.InvalidDataAccessResourceUsageException;
3231
import org.springframework.data.mongodb.ClientSessionException;
3332
import org.springframework.data.mongodb.MongoTransactionException;
34-
import org.springframework.data.mongodb.TransientMongoDbException;
3533
import org.springframework.data.mongodb.UncategorizedMongoDbException;
3634
import org.springframework.lang.Nullable;
3735

@@ -41,9 +39,7 @@
4139
import com.mongodb.MongoSocketException;
4240
import com.mongodb.MongoSocketReadTimeoutException;
4341
import com.mongodb.MongoSocketWriteException;
44-
import com.mongodb.MongoWriteException;
4542
import com.mongodb.ServerAddress;
46-
import com.mongodb.WriteError;
4743

4844
/**
4945
* Unit tests for {@link MongoExceptionTranslator}.
@@ -180,30 +176,19 @@ public void translateTransientTransactionExceptions() {
180176
MongoException source = new MongoException(267, "PreparedTransactionInProgress");
181177
source.addLabel(MongoException.TRANSIENT_TRANSACTION_ERROR_LABEL);
182178

183-
expectExceptionWithCauseMessage(translator.translateExceptionIfPossible(source), TransientMongoDbException.class,
179+
expectExceptionWithCauseMessage(translator.translateExceptionIfPossible(source),
180+
UncategorizedMongoDbException.class,
184181
"PreparedTransactionInProgress");
185182
}
186183

187184
@Test // DATAMONGO-2073
188-
public void translateMongoExceptionWithTransientLabelToTransientMongoDbException() {
185+
public void translateMongoExceptionWithTransientLabel() {
189186

190187
MongoException exception = new MongoException(0, "");
191188
exception.addLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL);
192189
DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
193190

194-
expectExceptionWithCauseMessage(translatedException, TransientMongoDbException.class);
195-
}
196-
197-
@Test // DATAMONGO-2073
198-
public void wrapsTranslatedExceptionsWhenTransientLabelPresent() {
199-
200-
MongoException exception = new MongoWriteException(new WriteError(112, "WriteConflict", new BsonDocument()), null);
201-
exception.addLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL);
202-
203-
DataAccessException translatedException = translator.translateExceptionIfPossible(exception);
204-
205-
assertThat(translatedException).isInstanceOf(TransientMongoDbException.class);
206-
assertThat(translatedException.getCause()).isInstanceOf(DataIntegrityViolationException.class);
191+
expectExceptionWithCauseMessage(translatedException, UncategorizedMongoDbException.class);
207192
}
208193

209194
private void checkTranslatedMongoException(Class<? extends Exception> clazz, int code) {

0 commit comments

Comments
 (0)