0

The below program is always giving the exception

"java.sql.SQLRecoverableException: Closed Connection" in this line of " final Reader reader = clb.getCharacterStream();"

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException;

public class ClobStringConversion {

    public static String clobStringConversion(Clob clb) throws IOException, SQLException
    {
        if (clb == null)
              return  "";

               StringBuffer sb = new StringBuffer();
               String strng;

               try{
                   final Reader reader = clb.getCharacterStream();
                   final BufferedReader br     = new BufferedReader(reader);

                   int b;
                   while(-1 != (b = br.read()))
                   {
                       sb.append((char)b);
                   }
                   br.close();
               }

               catch (SQLException e)
               {
                   //log.error("SQL. Could not convert CLOB to string",e);
                   return e.toString();
               }
               catch (IOException e)
               {
                   //log.error("IO. Could not convert CLOB to string",e);
                   return e.toString();
               }
               return sb.toString();
    }     

}
8
  • You should provide more details like the database, JDBC driver that you're using, what is the expected length of CLOB, whether this is happening intermittently or every time. Commented Jun 2, 2017 at 19:12
  • Currently using Oracle database with jdbc driver . CLOB length is very big. Commented Jun 2, 2017 at 19:19
  • 1
    Going to need to provide more code if you expect help here. This error message is stating that by this time, the socket connection has already been closed; you can not read data from a closed stream. Nothing we can do here. Commented Jun 2, 2017 at 19:19
  • Please confirm if it happens every time you try to read it or just some times. When it happens is the JDBC connection closed out, meaning can you run other queries at the time and just cannot read the CLOB data. If you can not run other queries then it means that JDBC connection has closed as suggested by @MattClark . You will have to increase the timeout duration of the database in that case. Commented Jun 2, 2017 at 19:35
  • It is happened every time.Currently using spring boot application and not sure where need to increase the duration of database connection. I am getting list of object from the database then am looping the list and converting the particular variable which has return type is clob. Commented Jun 2, 2017 at 19:50

1 Answer 1

1

You are probably closing the connection before calling clobStringConversion(). Try closing the connection after reading the Clob.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.