SonarSource/sonarqube · critical · java.lang.IllegalStateException

Can not get database connection

Error message

Can not get database connection

What it means

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.

Source

Thrown at server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DbPrimaryKeyConstraintFinder.java:92

      PreparedStatement pstmt = connection
        .prepareStatement(query);
      ResultSet rs = pstmt.executeQuery()) {
      if (rs.next()) {
        return Optional.ofNullable(rs.getString(1));
      }
      return Optional.empty();
    }
  }

  private String getPostgresSqlConstraintQuery(String tableName) {
    try (Connection connection = db.getDataSource().getConnection()) {
      return format("SELECT conname " +
        "FROM pg_constraint c " +
        "JOIN pg_namespace n on c.connamespace = n.oid " +
        "JOIN pg_class cls on c.conrelid = cls.oid " +
        "WHERE cls.relname = '%s' AND n.nspname = '%s' AND c.contype = 'p'", tableName, connection.getSchema());
    } catch (SQLException throwables) {
      throw new IllegalStateException("Can not get database connection");
    }
  }

  private static String getMssqlConstraintQuery(String tableName) {
    return format("SELECT name " +
      "FROM sys.key_constraints " +
      "WHERE type = 'PK' " +
      "AND OBJECT_NAME(parent_object_id) = '%s'", tableName);
  }

  private static String getOracleConstraintQuery(String tableName) {
    return format("SELECT constraint_name " +
      "FROM user_constraints " +
      "WHERE table_name = UPPER('%s') " +
      "AND constraint_type='P'", tableName);
  }

  private static String getH2ConstraintQuery(String tableName) {

View on GitHub (pinned to 184c821202)

Solutions

  1. Inspect server logs/datasource health for the underlying SQLException; fix DB connectivity or credentials.
  2. Increase connection pool size or fix connection leaks so a connection is available.
  3. Verify PostgreSQL is running and reachable, then rerun migration.

Example fix

// before
catch (SQLException throwables) {
  throw new IllegalStateException("Can not get database connection");
}
// after
catch (SQLException throwables) {
  throw new IllegalStateException("Can not get database connection", throwables);
}
Defensive patterns

Strategy: try-catch

Validate before calling

try (Connection c = dataSource.getConnection()) { if (!c.isValid(5)) throw new IllegalStateException("DB connection invalid"); }

Try / catch

try { finder.getPostgresSqlConstraintQuery(table); } catch (IllegalStateException e) { retry with backoff after checking pool/DB health }

Prevention

When it happens

Trigger: getPostgresSqlConstraintQuery/getPostgresSqlSequence path where db.getDataSource().getConnection() throws — pool exhausted, DB down, bad credentials.

Common situations: Connection pool depleted during long migration runs; database restarted mid-migration; misconfigured datasource; network interruption to PostgreSQL.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/67b14e662df11481. Report an issue: GitHub.