Below is a snippet of my transaction implementation. Currently MongoDB will throw WriteConflict error every time when it is processing the records from 800k onwards.
ClientSession clientSession = mongoClient.startSession();
clientSession.startTransaction();
int batchSize = 100000;
try {
for (int i = 1; i <= 1000000; i++) {
ArrayList<SampleData> sampleDataList = new ArrayList();
sampleDataList.add(new SampleData(i));
if (i % batchSize == 0 || i == sampleDataList.size()) {
sampleDataRepo.mongoCollection().insertMany(clientSession, sampleDataList);
sampleDataList.clear();
}
}
clientSession.commitTransaction();
}
catch (Exception e){
clientSession.abortTransaction();
throw new Exception(e.getMessage());
}
Resolved by increasing the wiredTigerCacheSizeGB