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)
JSixface
  • 577
  • 7
  • 11

3 Answers3

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.

ayush
  • 14,350
  • 11
  • 53
  • 100
  • 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.. – JSixface Feb 07 '11 at 18:04
  • which oracle db is it? oracle 9? – ayush Feb 07 '11 at 18:20
  • 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.. – JSixface Feb 08 '11 at 06:58
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.

Community
  • 1
  • 1
Thomas
  • 4,889
  • 4
  • 24
  • 19
  • 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.. – JSixface Feb 08 '11 at 06:56
0

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

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Swen Thümmler
  • 186
  • 2
  • 6