@@ -845,10 +845,16 @@ private BulkWriteResult executeWriteProtocol() {
845
845
WriteResult writeResult = executeWriteProtocol (i );
846
846
if (writeConcern .callGetLastError ()) {
847
847
bulkWriteBatchCombiner .addResult (getResult (writeResult ), indexMap );
848
+ // When a journaled write is requested but journaling is disabled, it's not thrown as an exception.
849
+ if (isWriteConcernError (writeResult .getLastError ())) {
850
+ bulkWriteBatchCombiner .addWriteConcernErrorResult (getWriteConcernError (writeResult .getLastError ()));
851
+ }
848
852
}
849
853
} catch (WriteConcernException writeException ) {
850
- if (isWriteConcernError (writeException )) {
851
- bulkWriteBatchCombiner .addWriteConcernErrorResult (getWriteConcernError (writeException ));
854
+ if (isWriteConcernError (writeException .getCommandResult ())) {
855
+ bulkWriteBatchCombiner .addResult (getResult (new WriteResult (writeException .getCommandResult (), writeConcern )),
856
+ indexMap );
857
+ bulkWriteBatchCombiner .addWriteConcernErrorResult (getWriteConcernError (writeException .getCommandResult ()));
852
858
} else {
853
859
bulkWriteBatchCombiner .addWriteErrorResult (getBulkWriteError (writeException ), indexMap );
854
860
}
@@ -865,10 +871,6 @@ private int getCount(final WriteResult writeResult) {
865
871
return getType () == INSERT ? 1 : writeResult .getN ();
866
872
}
867
873
868
- private boolean isWriteConcernError (final WriteConcernException writeException ) {
869
- return writeException .getCommandResult ().get ("wtimeout" ) != null ;
870
- }
871
-
872
874
List <BulkWriteUpsert > getUpsertedItems (final WriteResult writeResult ) {
873
875
return writeResult .getUpsertedId () == null
874
876
? Collections .<BulkWriteUpsert >emptyList ()
@@ -882,9 +884,26 @@ private BulkWriteError getBulkWriteError(final WriteConcernException writeExcept
882
884
0 );
883
885
}
884
886
885
- private WriteConcernError getWriteConcernError (final WriteConcernException writeException ) {
886
- return new WriteConcernError (writeException .getCode (), writeException .getCommandResult ().getString ("err" ),
887
- getErrorResponseDetails (writeException .getCommandResult ()));
887
+ // Accommodating GLE representation of write concern errors
888
+ private boolean isWriteConcernError (final CommandResult commandResult ) {
889
+ return commandResult .get ("wtimeout" ) != null || commandResult .get ("wnote" ) != null || commandResult .get ("jnote" ) != null ;
890
+ }
891
+
892
+ private WriteConcernError getWriteConcernError (final CommandResult commandResult ) {
893
+ return new WriteConcernError (commandResult .getCode (), getWriteConcernErrorMessage (commandResult ),
894
+ getErrorResponseDetails (commandResult ));
895
+ }
896
+
897
+ // GLE uses jnote and wnote as alternative ways or reporting write concern errors
898
+ private String getWriteConcernErrorMessage (final CommandResult commandResult ) {
899
+ String errorMessage = commandResult .getString ("jnote" );
900
+ if (errorMessage == null ) {
901
+ errorMessage = commandResult .getString ("wnote" );
902
+ }
903
+ if (errorMessage == null ) {
904
+ errorMessage = commandResult .getString ("err" );
905
+ }
906
+ return errorMessage ;
888
907
}
889
908
890
909
private DBObject getErrorResponseDetails (final DBObject response ) {
0 commit comments