oracle 无法将 clob 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2278445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 20:00:44  来源:igfitidea点击:

cannot convert clob to string

javaoraclehibernateclob

提问by Vipul

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

我正在尝试将我的 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;
 }

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

但是在使用“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 isMust be logged on to server

除了是必须登录到服务器

but I am getting the clob object from database

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

回答by Bozho

In your hibernate entity (which we can't see) declare the property to be of type Stringand 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.

在您的休眠实体(我们看不到)中,将属性声明为类型String并使用@Lob(同样,我不知道您使用的是注释还是 XML)进行注释,并且无需任何手册即可工作转换。

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.

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

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

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

回答by Thilo

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?

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

回答by starwin

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

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