0

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)

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

turning off the autocommit doesn't work ayush.. and for an open transaction, i inserted session.beginTransaction() just after the Hibernate.initialize() statement.. that too didn't work out..
which oracle db is it? oracle 9?
It worked out now.. I inserted a session.beginTransaction() inside the try block, and its working now.. but i can't figure out why it's working now and not earlier.. may be some scope issue i hope..
0

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.

1 Comment

I store some HTML and XML file in the CLOB.. the size of the XML file may have a maximum of 2MB size. That is the reason i'm using clob..
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.