cannot convert clob to string

13,396

Solution 1

In your hibernate entity (which we can't see) declare the property to be of type String and annotate it with @Lob (again, I don't know whether you are using annotations or XML), and it will work without the need for any manual conversion.

This happens because at the time you are trying to parse the entity, the session and the underlying connection are closed, so you can't stream the value of the CLOB from the database.

An alternative for the above solution might be to use an OpenSessionInView Filter. It will let you have the session (and the underlying database connection) open while rendering the view (i.e. your jsps)

Solution 2

Where are you getting the CLOB from? Is the Connection that created it still open? The error message seems to indicate the database connection has been closed in the meantime. If you are using Hibernate, is the Hibernate Session still open?

Solution 3

Clob resume = Hibernate.createClob ( "This is Clob");

Share:
13,396
Vipul
Author by

Vipul

Updated on June 07, 2022

Comments

  • Vipul
    Vipul almost 2 years

    I am trying to convert my clob object to string to display that on my JSTL page for that I am writting the following code

    public String convertClobToString(Clob clob){
         String toRet="";
         if(clob!=null)
         {
             try
             {
                 long length=clob.length();
                 toRet=clob.getSubString(1, (int)length);
             }
             catch(Exception ex)
             {
                 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                 ex.printStackTrace();
             }
         }
         return toRet;
     }
    

    but while counting the length using "long length = clob.length()" its throwing following exception

    java.sql.SQLException: Must be logged on to server
    
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
    at oracle.jdbc.dbaccess.DBError.check_error(DBError.java:1160)
    at oracle.jdbc.ttc7.TTC7Protocol.assertLoggedIn(TTC7Protocol.java:2196)
    at oracle.jdbc.ttc7.TTC7Protocol.lobLength(TTC7Protocol.java:2698)
    at oracle.sql.LobDBAccessImpl.length(LobDBAccessImpl.java:468)
    at oracle.sql.CLOB.length(CLOB.java:214)
    at org.hibernate.lob.SerializableClob.length(SerializableClob.java:33)
    at com.starcj.cmt.application.dao.ApplicationDaoImpl.convertClobToString(ApplicationDaoImpl.java:110)
    at com.starcj.cmt.termsandcondmgmt.dao.TermsAndCondDaoImpl.getAllActiveTermsandcond(TermsAndCondDaoImpl.java:91)
    at com.starcj.cmt.termsandcondmgmt.service.TermsAndCondServiceImpl.getAllActiveTermsAndCond(TermsAndCondServiceImpl.java:43)
    at com.starcj.cmt.termsandcondmgmt.controller.TermsAndCondListingCMTController.handleRequest(TermsAndCondListingCMTController.java:66)
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Unknown Source)
    

    Exceptoin isMust be logged on to server

    but I am getting the clob object from database