alibaba/spring-ai-alibaba · error · RuntimeException

Failed to get store size from database

Error message

Failed to get store size from database

What it means

DatabaseStore.size() runs SELECT COUNT(*) on the store table and wraps any SQLException in this RuntimeException. isEmpty() delegates to size(), so an empty-check also fails with this error. It indicates the count query could not run against the configured database.

Solutions

  1. Inspect the chained SQLException cause for the root error (connection, table missing, permission).
  2. Verify the store table exists (DatabaseStore table-existence check / manual DDL).
  3. Test the DataSource with a trivial query to rule out connection issues.
  4. Grant SELECT on the table to the application's DB user.

Example fix

// before
if (store.isEmpty()) { ... }
// after
boolean empty;
try {
    empty = store.isEmpty();
} catch (RuntimeException e) {
    logger.warn("size query failed", e.getCause());
    empty = true; // or fail fast, per policy
}
Defensive patterns

Strategy: try-catch

Validate before calling

try (Connection c = dataSource.getConnection()) {
    DatabaseMetaData md = c.getMetaData();
    // verify table exists before calling size()
}

Try / catch

try { boolean empty = store.isEmpty(); } catch (RuntimeException e) { log.warn("size failed: {}", e.getCause()); }

Prevention

When it happens

Trigger: Calling size() or isEmpty() when the connection fails, the table doesn't exist, or the user lacks SELECT privilege on the table.

Common situations: Health checks calling isEmpty() during a DB outage; schema not initialized because DatabaseStore was constructed with table creation disabled; wrong credentials.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/283009e26904534e. Report an issue: GitHub.

Appendix: source

Thrown at spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/store/stores/DatabaseStore.java:735

        try (Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement()) {
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            throw new RuntimeException("Failed to clear database store", e);
        }
    }

    @Override
    public long size() {
        String sql = "SELECT COUNT(*) FROM " + tableName;

        try (Connection conn = dataSource.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            rs.next();
            return rs.getLong(1);
        } catch (SQLException e) {
            throw new RuntimeException("Failed to get store size from database", e);
        }
    }

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    /**
     * Initialize database table with dialect-specific DDL.
     * - Keep business unique key `id` for logical identity and upsert conflict handling.
     * - Add auto-increment `pk_id` as physical primary key.
     */
    private void initializeTable() {
        String sql;
        boolean shouldCreate = true;
        try (Connection conn = dataSource.getConnection()) {
            DatabaseDialect dialect = getDatabaseDialect(conn);

View on GitHub (pinned to f82da0b50f)