{"record":{"id":"a417b94592523a2c","repo":"apache/seatunnel","slug":"writer-operation-failed-a417b9","errorCode":"WRITER_OPERATION_FAILED","errorMessage":"Bulk Write to MongoDB failed","messagePattern":"Bulk Write to MongoDB failed","errorType":"error_code","errorClass":"MongodbConnectorException","httpStatus":null,"severity":"critical","filePath":"seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/sink/MongodbWriter.java","lineNumber":194,"sourceCode":"                IntStream.rangeClosed(0, maxRetries)\n                        .anyMatch(\n                                i -> {\n                                    try {\n                                        lastSendTime = System.currentTimeMillis();\n                                        collectionProvider\n                                                .getDefaultCollection()\n                                                .bulkWrite(\n                                                        bulkRequests,\n                                                        new BulkWriteOptions().ordered(true));\n                                        bulkRequests.clear();\n                                        return true;\n                                    } catch (MongoException e) {\n                                        log.debug(\n                                                \"Bulk Write to MongoDB failed, retry times = {}\",\n                                                i,\n                                                e);\n                                        if (i >= maxRetries) {\n                                            throw new MongodbConnectorException(\n                                                    WRITER_OPERATION_FAILED,\n                                                    \"Bulk Write to MongoDB failed\",\n                                                    e);\n                                        }\n                                        try {\n                                            TimeUnit.MILLISECONDS.sleep(retryIntervalMs * (i + 1));\n                                        } catch (InterruptedException ex) {\n                                            Thread.currentThread().interrupt();\n                                            throw new MongodbConnectorException(\n                                                    WRITER_OPERATION_FAILED,\n                                                    \"Unable to flush; interrupted while doing another attempt\",\n                                                    e);\n                                        }\n                                        return false;\n                                    }\n                                });\n\n        if (!success) {","sourceCodeStart":176,"sourceCodeEnd":212,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-connectors-v2/connector-mongodb/src/main/java/org/apache/seatunnel/connectors/seatunnel/mongodb/sink/MongodbWriter.java#L176-L212","documentation":"The MongoDB sink writer wraps a MongoException thrown during a bulk write into a MongodbConnectorException after exhausting its retry loop. doBulkWrite retries the write up to maxRetries times with linear backoff (retryIntervalMs * attempt); if the underlying Mongo client keeps failing (network, auth, replica unavailability, oversized documents), the last MongoException is attached as the cause.","triggerScenarios":"write(), prepareCommit(), timerFlush() or close() invokes doBulkWrite, the driver throws MongoException, and the retry limit (maxRetries) is reached on a retry attempt.","commonSituations":"MongoDB server unreachable or restarting; replica-set elections; bad credentials; network partition between SeaTunnel worker and MongoDB; write rejected by server (document too large, duplicate key with certain configs).","solutions":["Inspect the attached cause MongoException in the stack trace to find the underlying server/network problem.","Verify MongoDB connectivity and credentials from the SeaTunnel worker nodes (firewall, mongod/replica set health).","Increase sink retry settings (max-retries, retry-interval-ms) if failures are transient.","Reduce batch size so no single bulk write exceeds MongoDB limits (e.g. 16MB max document/message size)."],"exampleFix":"// before\nurl = \"mongodb://localhost:27017\"\n// after (point to reachable replica set with auth)\nurl = \"mongodb://user:pass@rs1.example.com:27017,rs2.example.com:27017/?replicaSet=rs0&authSource=admin\"","handlingStrategy":"retry","validationCode":"// Before submitting the job, verify connectivity from the worker host:\n// mongosh \"mongodb://host:27017\" --eval \"db.runCommand({ping:1})\"","typeGuard":null,"tryCatchPattern":"try {\n    sinkWriter.write(row);\n} catch (MongodbConnectorException e) {\n    if (e.getErrorCode() == WRITER_OPERATION_FAILED && e.getCause() instanceof MongoException) {\n        // inspect MongoException code; decide retry vs fail job\n    }\n    throw e;\n}","preventionTips":["Point the sink at a replica set URI so elections don't break writes.","Tune max-retries and retry-interval-ms for your network reliability.","Keep batches below MongoDB size limits to avoid server-side rejections.","Monitor broker/server logs for persistent write errors."],"tags":["mongodb","bulk-write","retry-exhausted","network"],"backgroundTag":"database-write-failed","analyzedSha":"cf67b549a7a6c35fa0beb12d83c62892427ea919","analyzedAt":"2026-09-10T21:44:55.265Z","contentChangedAt":"2026-09-10T21:44:55.265Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}