Error "SQLException: The flow has already been closed" when reading a field of type LONG

0

I make the following request from Java with JDBC to user_tab_columns :

SELECT
     column_name,
     data_type,
     nullable,
     data_default,
     char_length,
     char_used
FROM user_tab_columns
WHERE table_name = 'mi_tabla'
ORDER BY column_id;

Example of a table to use:

CREATE TABLE "SYSTEM"."CW_CONTADORES" ( 
"ID" NUMBER NOT NULL ENABLE,
"SUBTIPO" VARCHAR2(25 BYTE) DEFAULT 'coche',
"ZONA" NUMBER DEFAULT -1)

I go through the records with a while and when the column data_default is -1 it throws me the error:

  

java.sql.SQLException: The flow has already been closed.

Code:

ResultSet resultColumns = stm.executeQuery(query);
while(resultColumns.next())
{
    col_default = resultColumns.getString("data_default") == null ? "" : " DEFAULT " + resultColumns.getString("data_default");
    //trato los datos
}

Can someone illuminate me if there is an idea that is not a try / catch of the problem I have?

    
asked by Rafael Calafell Cladera 30.01.2017 в 17:43
source

1 answer

3

Since the LONG and LONG RAW types are basically obsolete, reading those values correctly requires great care. The problem is that, although it is not apparent at first glance, when a query includes columns of that type, Oracle opens a special stream or flow for those types. But to be able to read them without problems, you must make sure of 2 things:

  • That you read the columns LONG / LONG RAW in your loop before to read the other columns.
  • That you read the columns LONG / LONG RAW only once in the loop.
  • Regarding the first point, according to the code you have shared, it does not seem to be the problem. But the way you've been modifying the code in your question and how you still do not seem clear how to create a minimal, complete and verifiable example , I would not be surprised if the code still does not correspond to reality.

    But, at least as regards the second point, with your last edition it is clear that there is a problem. You are reading the column data_default 2 times, what you can not do. When the value of the column data_default is not null , the second reading is the one that gives you an error, because the flow was already closed after the first reading.

    The solution is to read the value once and assign it to a variable to avoid a second reading:

    ResultSet resultColumns = stm.executeQuery(query);
    while(resultColumns.next())
    {
        String col_default = resultColumns.getString("data_default");
        col_default = col_default == null ? "" : " DEFAULT " + col_default;
        //trato los datos después...
    }
    

    If this does not solve the problem, then most likely the code in the question still does not reflect what you actually have. In order to receive correct answers to these types of questions, and to avoid turning the matter over unnecessarily, the key always lies in creating a minimum example, complete and verifiable . If you have not read the link yet, please do so. So you help yourself and us too.

        
    answered by 31.01.2017 / 13:46
    source