{"record":{"id":"42365c27fa49e579","repo":"apache/iceberg","slug":"failed-to-create-s-lock-42365c","errorCode":null,"errorMessage":"Failed to create %s lock","messagePattern":"Failed to create (.+?) lock","errorType":"exception","errorClass":"UncheckedSQLException","httpStatus":null,"severity":"error","filePath":"flink/v2.2/flink/src/main/java/org/apache/iceberg/flink/maintenance/api/JdbcLockFactory.java","lineNumber":206,"sourceCode":"                int count = sql.executeUpdate();\n                LOG.info(\n                    \"Created {} lock with instanceId {} with row count {}\",\n                    this,\n                    newInstanceId,\n                    count);\n                return count == 1;\n              }\n            });\n      } catch (InterruptedException e) {\n        Thread.currentThread().interrupt();\n        throw new UncheckedInterruptedException(e, \"Interrupted during tryLock\");\n      } catch (SQLException e) {\n        // SQL exception happened when creating the lock. Check if the lock creation was\n        // successful behind the scenes.\n        if (newInstanceId.equals(instanceId())) {\n          return true;\n        } else {\n          throw new UncheckedSQLException(e, \"Failed to create %s lock\", this);\n        }\n      }\n    }\n\n    @SuppressWarnings(\"checkstyle:NestedTryDepth\")\n    @Override\n    public boolean isHeld() {\n      try {\n        return pool.run(\n            conn -> {\n              try (PreparedStatement sql = conn.prepareStatement(GET_LOCK_SQL)) {\n                sql.setString(1, type.key);\n                sql.setString(2, lockId);\n                try (ResultSet rs = sql.executeQuery()) {\n                  return rs.next();\n                }\n              }\n            });","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/flink/v2.2/flink/src/main/java/org/apache/iceberg/flink/maintenance/api/JdbcLockFactory.java#L188-L224","documentation":"After a SQLException during lock-row insertion, tryLock checks whether the lock was actually created behind the scenes by comparing instance IDs; if the lock row belongs to a different instance (i.e. insertion genuinely failed and was not a duplicate), it throws UncheckedSQLException with \"Failed to create %s lock\" (%s formatted with the lock object). It means another owner may hold the lock or the insert failed for a real SQL reason.","triggerScenarios":"JdbcLock.tryLock when the INSERT raises SQLException AND instanceId() returns an ID different from newInstanceId — i.e. the lock row exists but was created by another process, or the insert failed for a non-duplicate-key reason.","commonSituations":"Two Flink jobs/maintenance triggers contending for the same lock ID, stale lock rows left by a crashed job, or DB constraint/permission errors on insert.","solutions":["Check who currently holds the lock (query the lock table's instance/creation columns) and wait for it to be released.","Remove stale lock rows left behind by crashed jobs after confirming no active owner.","Inspect the wrapped SQLException cause for permission/constraint errors on the lock table.","Ensure each job uses a distinct lock ID to avoid cross-job contention."],"exampleFix":"// before (stale row blocks creation)\nDELETE nothing; tryLock fails\n// after (after confirming owner is dead)\nDELETE FROM iceberg_lock WHERE name = 'maintenance' AND instance_id = 'dead-job-instance';","handlingStrategy":"try-catch","validationCode":"// check lock ownership before attempting\nSELECT instance_id, created_at FROM iceberg_lock WHERE name = ?;","typeGuard":null,"tryCatchPattern":"try {\n  if (!lock.tryLock()) { /* handle contention */ }\n} catch (UncheckedSQLException e) {\n  // inspect cause; check the lock table for a stale/foreign owner\n  // clean stale rows only after confirming the owner is dead\n}","preventionTips":["Use unique lock IDs per job to avoid contention","Add lock-row expiry/TTL handling for crashed owners","Monitor the lock table for stale rows","Log instance IDs to identify competing owners"],"tags":["jdbc","lock-contention","sql-exception","concurrency"],"backgroundTag":"database-write-failed","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}