JSixface
JSixface

Reputation: 697

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)

Upvotes: 0

Views: 7709

Answers (3)

Swen Thümmler
Swen Thümmler

Reputation: 306

You might try adding the system property hibernate.jdbc.use_streams_for_binary=true (this fixed the closed connection errors for me)

Upvotes: 0

Thomas
Thomas

Reputation: 4999

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.

Upvotes: 0

ayush
ayush

Reputation: 14568

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.

Upvotes: 2

Related Questions