---
title: Fetch 报错 ORA-01002：fetch out of sequence-OceanBase数据库使用指南
description: 了解OceanBase数据库在实际应用中关于Fetch 报错 ORA-01002：fetch out of sequence相关的常见问题和使用技巧，帮助您快速解决Fetch 报错 ORA-01002：fetch out of sequence的难题。
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

划线反馈

# Fetch 报错 ORA-01002：fetch out of sequence

更新时间：2026-05-29 08:46

适用版本： V4.2.x 内容类型：Troubleshoot  

## 适用版本

OceanBase 数据库 V4.x 版本。

## 问题现象

OceanBase 数据库从 V3.2.4 迁移升级到 V4.2.1 后，应用程序报错 `ORA-01002：fetch out of sequence`。

![Alt text](https://obbusiness-private.oss-cn-shanghai.aliyuncs.com/doc/img/knowledge-base/database/sql/9a1ce3f5-e7ec-4c39-891d-8f4f38829be2.png)

## 问题原因

JDBC 驱动使用二合一协议，默认使用 PS Cursor，Execute SQL 时会自动使用 Fetch 来获取结果集。当 AutoCommit=0 时，如果 Cursor 处于更新事务中（Open 前或后有 Update 操作），OceanBase 数据库 V4.x 会在 Commit 时将 cursor 失效并关闭，因此后续的 Fetch 就会报错。

代码简单逻辑如下。

```shell
        conn.setAutoCommit(false);
        conn.createStatement().execute("insert into t0(c0) values(0)");
        PreparedStatement ps00 =conn.prepareStatement("select * from t0");
        ResultSet rs00 = ps00.executeQuery();
        while (rs00.next()){
            conn.commit();
        }

```

基于 Cursor Fetch 的 While 循环 (while rs500.next()) 中有 commit 操作（conn.commit()），导致执行第一次 Fetch 后 cursor 被关闭，后续的 While 循环将报错。

OceanBase 数据库 V3.x 版本和 OceanBase 数据库 V4.x 版本的处理逻辑发生了变化。OceanBase 数据库 V3.x 版本兼容 Oracle 的行为，对流式 cursor 与非流式 cursor 中的 commit 操作区别对待。

- 流式 cursor：PL 中的普通 cursor 默认是流式的。在流式 cursor open 后执行 commit，会对 cursor 进行失效和关闭。
 - 非流式 cursor：PS cursor 默认是非流式的。在非流式 cursor open 后执行 commit，不会对 cursor 进行失效操作。

OceanBase 数据库 V4.x 没有区分流式 cursor 与非流式 cursor，只要 cursor open 后执行了 commit，均会对 cursor 做失效处理。

## 解决方法

OceanBase 数据库 V4.2.1 BP4 修复了非流式 cursor 后执行 commit 关闭 cursor 的问题。

## 相关文档

- [Prepare+Execute 二合一协议介绍](https://www.oceanbase.com/knowledge-base/oceanbase-connector-j-1000000000532687)

上一篇

[应用侧报错 multi stmts sql while OB_CLIENT_MULTI_STATEMENTS not enabled](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000442368)

下一篇

[SQL 存在 || 符号导致 and 逻辑不生效](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000003210200) ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
