alibaba/spring-ai-alibaba · error · RuntimeException

Failed to retrieve all items from database

Error message

Failed to retrieve all items from database

What it means

DatabaseStore's all-items retrieval (SELECT of every row) wraps SQLExceptions from the query execution in this RuntimeException. Individual rows that fail to deserialize are silently skipped; only a whole-query SQL failure raises this error.

Solutions

  1. Check the chained SQLException cause for the root database error.
  2. Verify the table exists and the user has SELECT privilege.
  3. Restore connectivity / reconnect the DataSource and retry.
  4. Avoid dropping/rename of the store table outside library-managed initialization.

Example fix

// before
List<StoreItem> all = store.listAllItems(); // no handling
// after
try {
    List<StoreItem> all = store.listAllItems();
} catch (RuntimeException e) {
    logger.error("list failed: {}", e.getCause(), e);
}
Defensive patterns

Strategy: retry

Validate before calling

try (Connection c = dataSource.getConnection()) {
    c.createStatement().executeQuery("SELECT 1 FROM " + tableName + " WHERE 1=0");
}

Try / catch

try { List<StoreItem> all = store.listAll(); } catch (RuntimeException e) { log.error("list failed: {}", e.getCause(), e); /* retry after reconnect */ }

Prevention

When it happens

Trigger: Listing all store items when the database is unreachable, the table was dropped, or the query fails due to permissions or corruption.

Common situations: Admin/debug listing against a DB that went down; table removed by external tooling; schema drift after manual DDL changes.

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/544d1b63a707378a. 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:875

     * @return list of all items
     */
    private List<StoreItem> getAllItems() {
        List<StoreItem> items = new ArrayList<>();
        String sql = "SELECT namespace, key_name, value_json, created_at, updated_at FROM " + tableName;

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

            while (rs.next()) {
                try {
                    items.add(resultSetToStoreItem(rs));
                } catch (Exception e) {
                    // Skip invalid items
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("Failed to retrieve all items from database", e);
        }

        return items;
    }

    /**
     * Convert ResultSet to StoreItem.
     *
     * @param rs result set
     * @return StoreItem
     * @throws Exception if conversion fails
     */
    @SuppressWarnings("unchecked")
    private StoreItem resultSetToStoreItem(ResultSet rs) {
        try {
            String namespaceJson = rs.getString("namespace");
            String key = rs.getString("key_name");
            String valueJson = rs.getString("value_json");

View on GitHub (pinned to f82da0b50f)