Source:
<code>private void saveDocs(CRUDOperationContext ctx, Op op, boolean upsert, DBCollection collection, List<DocInfo> batch) { if(op==DocSaver.Op.save) { LOGGER.debug("Retrieving existing documents for save operation",batch.size()); List<BasicDBObject> idQueries=new ArrayList<>(batch.size()); for(DocInfo doc:batch) { doc.id=getFieldValues(doc.newDoc,idPaths); if(!isNull(doc.id)) { idQueries.add(doc.getIdQuery()); } } if(!idQueries.isEmpty()) { BasicDBObject retrievalq=new BasicDBObject("$or",idQueries); LOGGER.debug("Existing document retrieval query={}",retrievalq); <START> DBCursor cursor=collection.find(retrievalq,null); <END> List<DBObject> results=cursor.toArray(); LOGGER.debug("Retrieved docs",results.size()); for(DBObject dbDoc:results) { Object[] id=getFieldValues(dbDoc,idPaths); DocInfo doc=findDoc(batch,id); if(doc!=null) { doc.oldDoc=dbDoc; } else { LOGGER.warn("Cannot find doc with id={}",id); } } } } List<DocInfo> saveList; List<DocInfo> insertList; if(op==DocSaver.Op.insert) { saveList=new ArrayList<>(); insertList=batch; } else { saveList=new ArrayList<>(); insertList=new ArrayList<>(); for(DocInfo doc:batch) { if(doc.oldDoc==null) { if(upsert) { insertList.add(doc); } else { LOGGER.warn("Invalid request, cannot update or insert"); doc.inputDoc.addError(Error.get(op.toString(), MongoCrudConstants.ERR_SAVE_ERROR, "Invalid request")); } } else { saveList.add(doc); } } } LOGGER.debug("Save docs={}, insert docs={}, error docs={}",saveList.size(),insertList.size(),batch.size()-saveList.size()-insertList.size()); insertDocs(ctx,collection,insertList); updateDocs(ctx,collection,saveList); }</code><technical_language>close cursor. Recommend try-with-resource</technical_language>

Target:
private void saveDocs(CRUDOperationContext ctx, Op op, boolean upsert, DBCollection collection, List<DocInfo> batch) { if(op==DocSaver.Op.save) { LOGGER.debug("Retrieving existing documents for save operation",batch.size()); List<BasicDBObject> idQueries=new ArrayList<>(batch.size()); for(DocInfo doc:batch) { doc.id=getFieldValues(doc.newDoc,idPaths); if(!isNull(doc.id)) { idQueries.add(doc.getIdQuery()); } } if(!idQueries.isEmpty()) { BasicDBObject retrievalq=new BasicDBObject("$or",idQueries); LOGGER.debug("Existing document retrieval query={}",retrievalq); try (DBCursor cursor=collection.find(retrievalq,null)) { List<DBObject> results=cursor.toArray(); LOGGER.debug("Retrieved docs",results.size()); for(DBObject dbDoc:results) { Object[] id=getFieldValues(dbDoc,idPaths); DocInfo doc=findDoc(batch,id); if(doc!=null) { doc.oldDoc=dbDoc; } else { LOGGER.warn("Cannot find doc with id={}",id); } } } } } List<DocInfo> saveList; List<DocInfo> insertList; if(op==DocSaver.Op.insert) { saveList=new ArrayList<>(); insertList=batch; } else { saveList=new ArrayList<>(); insertList=new ArrayList<>(); for(DocInfo doc:batch) { if(doc.oldDoc==null) { if(upsert) { insertList.add(doc); } else { LOGGER.warn("Invalid request, cannot update or insert"); doc.inputDoc.addError(Error.get(op.toString(), MongoCrudConstants.ERR_SAVE_ERROR, "Invalid request")); } } else { saveList.add(doc); } } } LOGGER.debug("Save docs={}, insert docs={}, error docs={}",saveList.size(),insertList.size(),batch.size()-saveList.size()-insertList.size()); insertDocs(ctx,collection,insertList); updateDocs(ctx,collection,saveList); }