{"record":{"id":"283009e26904534e","repo":"alibaba/spring-ai-alibaba","slug":"failed-to-get-store-size-from-database","errorCode":null,"errorMessage":"Failed to get store size from database","messagePattern":"Failed to get store size from database","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/store/stores/DatabaseStore.java","lineNumber":735,"sourceCode":"        try (Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement()) {\n            stmt.executeUpdate(sql);\n        } catch (SQLException e) {\n            throw new RuntimeException(\"Failed to clear database store\", e);\n        }\n    }\n\n    @Override\n    public long size() {\n        String sql = \"SELECT COUNT(*) FROM \" + tableName;\n\n        try (Connection conn = dataSource.getConnection();\n             Statement stmt = conn.createStatement();\n             ResultSet rs = stmt.executeQuery(sql)) {\n\n            rs.next();\n            return rs.getLong(1);\n        } catch (SQLException e) {\n            throw new RuntimeException(\"Failed to get store size from database\", e);\n        }\n    }\n\n    @Override\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /**\n     * Initialize database table with dialect-specific DDL.\n     * - Keep business unique key `id` for logical identity and upsert conflict handling.\n     * - Add auto-increment `pk_id` as physical primary key.\n     */\n    private void initializeTable() {\n        String sql;\n        boolean shouldCreate = true;\n        try (Connection conn = dataSource.getConnection()) {\n            DatabaseDialect dialect = getDatabaseDialect(conn);","sourceCodeStart":717,"sourceCodeEnd":753,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/store/stores/DatabaseStore.java#L717-L753","documentation":"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.","triggerScenarios":"Calling size() or isEmpty() when the connection fails, the table doesn't exist, or the user lacks SELECT privilege on the table.","commonSituations":"Health checks calling isEmpty() during a DB outage; schema not initialized because DatabaseStore was constructed with table creation disabled; wrong credentials.","solutions":["Inspect the chained SQLException cause for the root error (connection, table missing, permission).","Verify the store table exists (DatabaseStore table-existence check / manual DDL).","Test the DataSource with a trivial query to rule out connection issues.","Grant SELECT on the table to the application's DB user."],"exampleFix":"// before\nif (store.isEmpty()) { ... }\n// after\nboolean empty;\ntry {\n    empty = store.isEmpty();\n} catch (RuntimeException e) {\n    logger.warn(\"size query failed\", e.getCause());\n    empty = true; // or fail fast, per policy\n}","handlingStrategy":"try-catch","validationCode":"try (Connection c = dataSource.getConnection()) {\n    DatabaseMetaData md = c.getMetaData();\n    // verify table exists before calling size()\n}","typeGuard":null,"tryCatchPattern":"try { boolean empty = store.isEmpty(); } catch (RuntimeException e) { log.warn(\"size failed: {}\", e.getCause()); }","preventionTips":["Ensure DatabaseStore table initialization ran successfully before querying","Grant SELECT on the store table","Monitor DB health before health-check endpoints call isEmpty()","Keep JDBC driver and server versions compatible"],"tags":["database","jdbc","sql-exception","count-query"],"backgroundTag":"sql-query-failed","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}