22
22
import com .mongodb .MongoException ;
23
23
import com .mongodb .MongoInternalException ;
24
24
import com .mongodb .MongoSocketException ;
25
+ import com .mongodb .MongoTimeoutException ;
25
26
import com .mongodb .ReadConcern ;
26
27
import com .mongodb .ReadPreference ;
27
28
import com .mongodb .ServerAddress ;
46
47
import java .util .ArrayList ;
47
48
import java .util .List ;
48
49
50
+ import static com .mongodb .MongoException .TRANSIENT_TRANSACTION_ERROR_LABEL ;
49
51
import static com .mongodb .MongoException .UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL ;
50
52
import static com .mongodb .ReadPreference .primary ;
51
53
import static com .mongodb .assertions .Assertions .isTrue ;
@@ -175,7 +177,7 @@ public <T> T execute(final ReadOperation<T> operation, final ReadPreference read
175
177
throw new MongoClientException ("Read preference in a transaction must be primary" );
176
178
}
177
179
return operation .execute (binding );
178
- } catch (MongoSocketException e ) {
180
+ } catch (MongoException e ) {
179
181
labelException (session , e );
180
182
throw e ;
181
183
} finally {
@@ -189,7 +191,7 @@ public <T> T execute(final WriteOperation<T> operation, final ReadConcern readCo
189
191
WriteBinding binding = getWriteBinding (readConcern , actualClientSession , session == null && actualClientSession != null );
190
192
try {
191
193
return operation .execute (binding );
192
- } catch (MongoSocketException e ) {
194
+ } catch (MongoException e ) {
193
195
labelException (session , e );
194
196
throw e ;
195
197
} finally {
@@ -217,8 +219,9 @@ ReadWriteBinding getReadWriteBinding(final ReadPreference readPreference, final
217
219
}
218
220
219
221
private void labelException (final @ Nullable ClientSession session , final MongoException e ) {
220
- if (session != null && session .hasActiveTransaction () && !e .hasErrorLabel (UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL )) {
221
- e .addLabel (MongoException .TRANSIENT_TRANSACTION_ERROR_LABEL );
222
+ if ((e instanceof MongoSocketException || e instanceof MongoTimeoutException )
223
+ && session != null && session .hasActiveTransaction () && !e .hasErrorLabel (UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL )) {
224
+ e .addLabel (TRANSIENT_TRANSACTION_ERROR_LABEL );
222
225
}
223
226
}
224
227
0 commit comments