{"record":{"id":"67b14e662df11481","repo":"SonarSource/sonarqube","slug":"can-not-get-database-connection","errorCode":null,"errorMessage":"Can not get database connection","messagePattern":"Can not get database connection","errorType":"exception","errorClass":"java.lang.IllegalStateException","httpStatus":null,"severity":"critical","filePath":"server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DbPrimaryKeyConstraintFinder.java","lineNumber":92,"sourceCode":"      PreparedStatement pstmt = connection\n        .prepareStatement(query);\n      ResultSet rs = pstmt.executeQuery()) {\n      if (rs.next()) {\n        return Optional.ofNullable(rs.getString(1));\n      }\n      return Optional.empty();\n    }\n  }\n\n  private String getPostgresSqlConstraintQuery(String tableName) {\n    try (Connection connection = db.getDataSource().getConnection()) {\n      return format(\"SELECT conname \" +\n        \"FROM pg_constraint c \" +\n        \"JOIN pg_namespace n on c.connamespace = n.oid \" +\n        \"JOIN pg_class cls on c.conrelid = cls.oid \" +\n        \"WHERE cls.relname = '%s' AND n.nspname = '%s' AND c.contype = 'p'\", tableName, connection.getSchema());\n    } catch (SQLException throwables) {\n      throw new IllegalStateException(\"Can not get database connection\");\n    }\n  }\n\n  private static String getMssqlConstraintQuery(String tableName) {\n    return format(\"SELECT name \" +\n      \"FROM sys.key_constraints \" +\n      \"WHERE type = 'PK' \" +\n      \"AND OBJECT_NAME(parent_object_id) = '%s'\", tableName);\n  }\n\n  private static String getOracleConstraintQuery(String tableName) {\n    return format(\"SELECT constraint_name \" +\n      \"FROM user_constraints \" +\n      \"WHERE table_name = UPPER('%s') \" +\n      \"AND constraint_type='P'\", tableName);\n  }\n\n  private static String getH2ConstraintQuery(String tableName) {","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DbPrimaryKeyConstraintFinder.java#L74-L110","documentation":"Thrown by DbPrimaryKeyConstraintFinder when obtaining a JDBC connection for the PostgreSQL constraint/sequence lookup fails with a SQLException. The wrapped exception is dropped (only a static message is kept), hiding the root cause.","triggerScenarios":"getPostgresSqlConstraintQuery/getPostgresSqlSequence path where db.getDataSource().getConnection() throws — pool exhausted, DB down, bad credentials.","commonSituations":"Connection pool depleted during long migration runs; database restarted mid-migration; misconfigured datasource; network interruption to PostgreSQL.","solutions":["Inspect server logs/datasource health for the underlying SQLException; fix DB connectivity or credentials.","Increase connection pool size or fix connection leaks so a connection is available.","Verify PostgreSQL is running and reachable, then rerun migration."],"exampleFix":"// before\ncatch (SQLException throwables) {\n  throw new IllegalStateException(\"Can not get database connection\");\n}\n// after\ncatch (SQLException throwables) {\n  throw new IllegalStateException(\"Can not get database connection\", throwables);\n}","handlingStrategy":"try-catch","validationCode":"try (Connection c = dataSource.getConnection()) { if (!c.isValid(5)) throw new IllegalStateException(\"DB connection invalid\"); }","typeGuard":null,"tryCatchPattern":"try { finder.getPostgresSqlConstraintQuery(table); } catch (IllegalStateException e) { retry with backoff after checking pool/DB health }","preventionTips":["Size the connection pool for concurrent migration needs","Monitor PostgreSQL availability before migration runs","Always chain the SQLException as cause (fix the swallowed exception)"],"tags":["database","connection","postgresql","jdbc"],"backgroundTag":"connection-refused","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}