使用 hibernate 从 oracle 读取 clob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4924536/
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
read a clob from oracle using hibernate
提问by JSixface
I am having a big headache in finding out what is the problem here ..
我在找出这里的问题时很头疼..
I get a session from the session factory and then get the clob out of the persistant object. When it comes to clob.getCharcterStream() or getAsciiStream(), it throws an exception - Closed connection.
我从会话工厂获得一个会话,然后从持久对象中取出 clob。当涉及到 clob.getCharcterStream() 或 getAsciiStream() 时,它会抛出异常 - 关闭连接。
Can someone help me with this.
有人可以帮我弄这个吗。
code :
代码 :
Session session = Dao.getSession(connId);
Package pack = (Package) session.load(Package.class, packId);
Hibernate.initialize(pack);
Clob reportClob = pack.getExpReportFile();
String result = null;
InputStream stream = null;
try
{
System.out.println(session.isConnected() + " " + session.isOpen());
stream = reportClob.getAsciiStream();
result = IOUtils.toString(stream);
}
catch (SQLException e)
{
e.printStackTrace();
}
return result;
Exception :
例外 :
true true
java.sql.SQLException: Closed Connection
at oracle.sql.CLOB.getDBAccess(CLOB.java:1389)
at oracle.sql.CLOB.getAsciiStream(CLOB.java:330)
at org.hibernate.lob.SerializableClob.getAsciiStream(SerializableClob.java:68)
at com.server.WebServiceImpl.fetchPackageReport(WebServiceImpl.java:2070)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:562)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:188)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:224)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
回答by ayush
The reason for this error is that the session that your entity is attached to is now closed.
此错误的原因是您的实体附加到的会话现已关闭。
try db connection with autocommit turned off(hibernate.connection.autocommit
) or you need an open transaction.
尝试关闭自动提交的数据库连接(hibernate.connection.autocommit
)或者您需要一个打开的事务。
回答by Thomas
Is there a specific reason that you need it to be a Clob? Looking at the sample code, it looks like you are putting it into a String, and Hibernate supports putting Clobs into String
, char[]
and Clob
. This should fix the problem.
是否有特定原因需要它成为 Clob?看样例代码,好像是把它放到一个String里面,Hibernate支持把Clob放到String
,char[]
和Clob
. 这应该可以解决问题。
If a requirement is to only have ASCII characters in the string, you can always do something like:
如果要求字符串中只包含 ASCII 字符,您总是可以执行以下操作:
String ascii = new String(oldString.getBytes("ASCII"),"ASCII");
Warning: if you change to using String
you might run into this bug, however it is still worth a try.
警告:如果您更改为使用,String
您可能会遇到此错误,但仍然值得一试。
回答by Swen Thümmler
You might try adding the system property hibernate.jdbc.use_streams_for_binary=true (this fixed the closed connection errors for me)
您可以尝试添加系统属性 hibernate.jdbc.use_streams_for_binary=true (这为我修复了关闭的连接错误)