无法将clob转换为字符串

时间:2010-02-17 05:04:55

标签: java oracle hibernate clob

我正在尝试将我的clob对象转换为字符串,以便在我的JSTL页面上显示我正在编写以下代码

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;
 }

但是在使用“long length = clob.length()”计算长度时,它会抛出异常

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必须登录到服务器

但是我从数据库中获取了clob对象

3 个答案:

答案 0 :(得分:5)

在你的hibernate实体中(我们看不到)将属性声明为String类型,并用@Lob注释它(同样,我不知道你是使用注释还是XML),它可以在不需要任何手动转换的情况下工作。

这是因为在您尝试解析实体时,会话和底层连接已关闭,因此您无法从数据库中流式传输CLOB的值。

上述解决方案的替代方案可能是使用OpenSessionInView过滤器。它将让你在渲染视图时打开会话(和底层数据库连接)(即你的jsps)

答案 1 :(得分:2)

你从哪里获得CLOB?创建它的Connection是否仍然打开? 错误消息似乎表明数据库连接已在此期间关闭。 如果您正在使用Hibernate,Hibernate Session是否仍然打开?

答案 2 :(得分:1)

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