Skip to content

ERR-5102E ( 331822) Invalid cursor state.#

Overview#

This document describes the causes of and solutions for the ERR-5102E ( 331822) Invalid cursor state. error, which occurs when an application does not handle a cursor in the correct state or sequence.

It covers common cases in APRE(SESC) and CLI/ODBC applications. In this document, CLI/ODBC collectively refers to SQLCLI and ODBC.

Version#

Altibase 4 or later

Symptom#

The following error occurs while an APRE(SESC) or CLI/ODBC application is fetching data.

ERR-5102E ( 331822) Invalid cursor state.

Cause#

The main causes are as follows.

Reusing a cursor without closing it after completing FETCH#

In Altibase 5 or later, a cursor remains open unless the application explicitly closes it after completing FETCH. If the application then DECLAREs or OPENs the same cursor again, the error occurs.

This case does not cause the error in Altibase 4.

declare cursor A;
open cursor A;
fetch cursor A;       // FETCH completed
// close cursor A;    // Cursor is not closed
declare cursor A;     // Reuse the same cursor

Reusing a cursor or Statement while rows remain to be fetched#

The error occurs if the application uses the same cursor for another operation while rows remain to be fetched.

In APRE or SESC, this case occurs when the application DECLAREs or OPENs a cursor again while FETCH is still in progress.

while (1) 
{ 
   fetch cursor A;      // Rows remain to be fetched 

   open cursor A;       // Reopen the same cursor 
}

In CLI/ODBC, this case occurs when the application calls SQLExecute(), SQLPrepare(), or SQLExecDirect() on the same Statement while processing rows with SQLFetch().

while (1)
{
    SQLFetch(stmt);     // Rows remain to be fetched

    SQLExecute(stmt);   // Reuse the same statement
}

Not handling a cursor CLOSE failure#

If CLOSE fails and the application does not check the result, the cursor remains open. Reusing the same cursor afterward causes the error.

This case applies to Altibase 5 or later. It does not cause the error in Altibase 4.

declare cursor A;

if (sqlca.sqlcode != SQL_SUCCESS) {
   Error handling;
}

open cursor A;

if (sqlca.sqlcode != SQL_SUCCESS) {
   Error handling;
}

fetch cursor A;

if (sqlca.sqlcode != SQL_SUCCESS) {
   Error handling;
}

close cursor A;

/* The result of CLOSE is not checked */

declare cursor A;

if (sqlca.sqlcode != SQL_SUCCESS) {
   Error handling;
}

Executing COMMIT or ROLLBACK during FETCH#

By default, Altibase closes an open cursor when the application executes COMMIT or ROLLBACK. If the application attempts to fetch from the closed cursor afterward, the error can occur.

However, a cursor declared with WITH HOLD in Altibase 6.3.1 or later may behave differently.

Sharing a single database connection in a multithreaded environment#

If multiple threads share a single database connection without controlling concurrent access, cursor operations can occur in an unexpected order.

In CLI/ODBC, the error can occur when one thread changes the state of a Statement and another thread subsequently uses the same Statement.

Solution#

Use the cursor according to its FETCH state#

Do not use the same cursor for another operation while rows remain to be fetched. After completing FETCH, CLOSE the cursor before reusing it.

fetch cursor A;

close cursor A;

declare cursor A;

In APRE or SESC, CLOSE the cursor after fetching all rows. If the remaining rows are no longer needed, CLOSE the cursor before reusing it.

In CLI/ODBC, close the result set after fetching all rows before reusing the same Statement. If the remaining rows are no longer needed, close the result set before reusing the Statement.

Check the result of cursor CLOSE#

Add error handling to check the result after executing CLOSE.

If CLOSE fails, identify the cause of the failure and close the cursor successfully before reusing it.

Do not execute COMMIT or ROLLBACK during FETCH#

In the same session, execute COMMIT or ROLLBACK after completing FETCH.

If another transaction must be processed during FETCH, use a new database connection.

In Altibase 6.3.1 or later, consider using a WITH HOLD cursor when appropriate.

Control concurrent access to database connections in multithreaded applications#

When multiple threads share a single database connection, control concurrent access and verify the order of cursor operations in each thread.

In CLI/ODBC, also control the call sequence when multiple threads share the same Statement.

If concurrent access is difficult to control, use a separate database connection for each thread.