简体   繁体   中英

read a clob from oracle using hibernate

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.

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)

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.

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.

If a requirement is to only have ASCII characters in the string, you can always do something like:

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.

您可以尝试添加系统属性hibernate.jdbc.use_streams_for_binary = true(这为我修复了关闭的连接错误)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM