{"record":{"id":"427cc11f3a85c261","repo":"apache/seatunnel","slug":"write-records-failed-427cc1","errorCode":"WRITE_RECORDS_FAILED","errorMessage":"Flush failed during close","messagePattern":"Flush failed during close","errorType":"error_code","errorClass":"CouchbaseConnectorException","httpStatus":null,"severity":"error","filePath":"seatunnel-connectors-v2/connector-couchbase/src/main/java/org/apache/seatunnel/connectors/seatunnel/couchbase/sink/CouchbaseWriter.java","lineNumber":223,"sourceCode":"            } catch (Exception disconnectEx) {\n                if (primaryThrowable != null) {\n                    // Issue 2: preserve the flush error as the primary cause; attach the\n                    // disconnect failure so it is visible but does not mask the root cause.\n                    primaryThrowable.addSuppressed(disconnectEx);\n                } else {\n                    primaryThrowable =\n                            new CouchbaseConnectorException(\n                                    CouchbaseConnectorErrorCode.CLOSE_CLIENT_FAILED,\n                                    \"Failed to disconnect Couchbase cluster\",\n                                    disconnectEx);\n                }\n            }\n        }\n        if (primaryThrowable != null) {\n            if (primaryThrowable instanceof CouchbaseConnectorException) {\n                throw (CouchbaseConnectorException) primaryThrowable;\n            }\n            throw new CouchbaseConnectorException(\n                    CouchbaseConnectorErrorCode.WRITE_RECORDS_FAILED,\n                    \"Flush failed during close\",\n                    primaryThrowable);\n        }\n    }\n\n    // ---------------------------------------------------------------------------\n    // Internal helpers\n    // ---------------------------------------------------------------------------\n    /** Converts a {@link SeaTunnelRow} to a {@link JsonObject} using the schema field names. */\n    private JsonObject toJsonObject(SeaTunnelRow row) {\n        JsonObject doc = JsonObject.create();\n        String[] fieldNames = rowType.getFieldNames();\n        SeaTunnelDataType<?>[] fieldTypes = rowType.getFieldTypes();\n        for (int i = 0; i < fieldNames.length; i++) {\n            Object value = row.getField(i);\n            if (value == null) {\n                doc.putNull(fieldNames[i]);","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/apache/seatunnel/blob/cf67b549a7a6c35fa0beb12d83c62892427ea919/seatunnel-connectors-v2/connector-couchbase/src/main/java/org/apache/seatunnel/connectors/seatunnel/couchbase/sink/CouchbaseWriter.java#L205-L241","documentation":"close() performs a final flush of buffered records; if that flush throws and the exception is not already a CouchbaseConnectorException, it is wrapped as WRITE_RECORDS_FAILED with message 'Flush failed during close'. This ensures buffered data loss during shutdown is surfaced instead of silently swallowed.","triggerScenarios":"Buffered records exist and close() triggers doFlush which fails (network error, cluster unreachable, timeout, interruption); disconnect also throwing but a primary flush exception already captured.","commonSituations":"Job shutdown while Couchbase cluster is unreachable; connection pool closed before writer close; last flush exceeding timeouts.","solutions":["Inspect the wrapped cause (getCause) for the underlying network/cluster error","Ensure the Couchbase cluster is reachable and credentials valid before job ends","Verify network/DNS/firewall stability during job teardown","Increase timeout/retry settings if the final batch is large"],"exampleFix":"// before\n// ignoring cause\ncatch (Exception e) { log.warn(e.getMessage()); }\n// after\ncatch (CouchbaseConnectorException e) {\n  log.error(\"flush failed during close: {}\", e.getCause(), e);\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"java\ntry {\n    writer.close();\n} catch (CouchbaseConnectorException e) {\n    Throwable cause = e.getCause();\n    // log cause; decide retry or accept data-loss on shutdown\n}","preventionTips":["Ensure cluster reachability before job teardown","Avoid closing connections the writer still depends on","Keep buffers small so final flush is fast"],"tags":["flush","close","couchbase"],"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"}