{"record":{"id":"9b458fb6236040d2","repo":"apache/iceberg","slug":"failed-to-create-s-lock","errorCode":null,"errorMessage":"Failed to create %s lock","messagePattern":"Failed to create (.+?) lock","errorType":"exception","errorClass":"UncheckedSQLException","httpStatus":null,"severity":"error","filePath":"flink/v1.20/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/v1.20/flink/src/main/java/org/apache/iceberg/flink/maintenance/api/JdbcLockFactory.java#L188-L224","documentation":"When JdbcLock.tryLock's INSERT throws SQLException, the code checks whether the lock was created anyway (by comparing the stored instance ID). If the stored instanceId does not match this instance's newInstanceId, the lock genuinely could not be created, and the SQLException is wrapped as UncheckedSQLException with 'Failed to create %s lock'.","triggerScenarios":"tryLock fails when the INSERT errors and the lock row in the maintenance lock table holds a different instance ID — meaning another holder owns the row or the row is stale/corrupt, or the SQL failure is a real error (constraint violation, connection drop).","commonSituations":"Stale lock rows left by a crashed job with a different instance ID; DB connectivity dropping mid-INSERT; schema/constraint mismatches on the lock table.","solutions":["Check the lock table contents: if a stale row from a dead job exists, remove it or let the maintenance recovery procedure reset the lock.","Verify DB connectivity and fix any SQLException surfaced in the cause chain.","Ensure only one trigger job with this lock ID runs concurrently.","Recreate the lock table if its schema drifted from what the current version expects."],"exampleFix":"// stale row cleanup (example for MySQL)\n// DELETE FROM iceberg_triggers_lock WHERE trigger_id = 'my-table' AND instance_id != '<current>';","handlingStrategy":"try-catch","validationCode":"// before acquiring, check who holds the lock\nString holder = fetchInstanceId(lockId);\nif (holder != null && !holder.equals(myInstanceId)) {\n    throw new IllegalStateException(\"Lock held by instance: \" + holder);\n}","typeGuard":null,"tryCatchPattern":"try {\n    acquired = lock.tryLock();\n} catch (UncheckedSQLException e) {\n    // inspect cause: constraint violation vs connection loss\n    LOG.error(\"tryLock failed\", e.getCause());\n    // check for stale rows and clean up per runbook, then retry once\n}","preventionTips":["Ensure only one trigger job per lock ID runs concurrently","Clean up stale lock rows from crashed jobs","Monitor DB connectivity to avoid mid-INSERT failures","Keep the lock table schema in sync with the Iceberg version"],"tags":["jdbc","flink","lock","database","contention"],"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-14T16:17:12.679Z"}