4

The was a change to the FilterOutputStream.close() method in Java 8 that is causing us some problems. (See http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf)

In previous versions of Java, the following code worked without throwing an exception. However, under Java 8 we always get an exception when the try-with-resources mechanism closes the streams.

try( InputStream bis = new BufferedInputStream( inputStream );
     OutputStream outStream = payloadData.setBinaryStream( 0 );
     BufferedOutputStream bos = new BufferedOutputStream( outStream );
     DeflaterOutputStream deflaterStream = new DeflaterOutputStream(
         bos, new Deflater( 3 ) ) )
{
    fileSize = IOUtil.copy( bis, deflaterStream );
}

The try-with-resources mechanism will first call close() on deflaterStream. Since deflaterStream wraps bos which wraps outStream, deflaterStream.close() will call bos.close() which will call outStream.close() which closes the underlying stream to the database.

The try-with-resources mechanism will next call close() on bos. Since bos extends FilterOutputStream, flush() will first be called on outStream. However, since outStream is already closed, outStream.flush() throws exception: java.sql.SQLException: Closed LOB

caused by: java.io.IOException: Closed LOB
     at oracle.jdbc.driver.OracleBlobOutputStream.ensureOpen(OracleBlobOutputStream.java:265)
     at oracle.jdbc.driver.OracleBlobOutputStream.flush(OracleBlobOutputStream.java:167)
     at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:141)
     at java.io.FilterOutputStream.close(FilterOutputStream.java:158)
     at com.blah.uploadFile(CustomerUploadFacade.java:162)
     ... 38 more
Caused by: java.sql.SQLException: Closed LOB
     at oracle.jdbc.driver.OracleBlobOutputStream.ensureOpen(OracleBlobOutputStream.java:257)
     ... 42 more

Has anyone else experienced this issue? If so how did you work around it? Is there something wrong with the way we're using try-with-resources?

2
  • I assume blobDeflaterStream in the try body wants to be deflaterStream .
    – icza
    Aug 7, 2014 at 6:56
  • Yes you're absolutely right. I've edited the question.
    – Nathan
    Aug 7, 2014 at 7:02

4 Answers 4

2

This is a bug in FilterOutputStream. This implements Closable. The contract for this interface states:

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

So calling .close() a second time should have no effect, but in this case it calls flush() which throws an exception.

FilterOutputStream is stuck in an inheritance hierarchy, so it's not easy to apply a fix at the point of the problem.

Failing to declare some of the streams as local variables in the try block will cause tools like FindBugs and Eclipse to flag the code as resource usage. Diligent developers looking at this code later might think the same thing.

You could create a class, CloseOnceBufferedOutputStream which extends BufferedOutputStream. It will have a boolean to remember if it has already been closed so it can meet the contract. Override the close() method to check if the stream is already closed. If so, just return.

If and when Oracle fix the underlying bug, your new class would be useless but the code would continue to work.

3
  • I agree that it's a bug. I've submitted a bug report to Oracle and have been given Review ID JI-9014085. I'll update this comment if I get a Bug ID.
    – Nathan
    Aug 8, 2014 at 1:14
  • This has been assigned Bug ID JDK-8054565
    – Nathan
    Dec 3, 2014 at 22:00
  • A fix has been committed against that Bug ID for JDK 9: hg.openjdk.java.net/jdk9/dev/jdk/rev/5f22b23442ba I don't believe it will be backported to JDK 8.
    – Nathan
    Jan 21, 2015 at 22:43
1

Simply don't declare bos and outStream in the try and so they won't be automatically closed. Only the declared AutoClosables will be closed.

The JVM does not analyze the declared AutoClosables whether one is constructed using another, so each of them must be closed explicitly. If their close() method can only be called once, that could lead to problems you're experiencing.

Only declare deflaterStream like this:

try( InputStream bis = new BufferedInputStream( inputStream );
     DeflaterOutputStream deflaterStream = new DeflaterOutputStream(
        new BufferedOutputStream( payloadData.setBinaryStream( 0 ) ),
            new Deflater( 3 ) ) )
{
    fileSize = IOUtil.copy( bis, deflaterStream );
}

Edit:

Following the comments here's how you could close the stream returned by payloadData.setBinaryStream( 0 ) in case of some other issue:

OutputStream outStream = null;
try( InputStream bis = new BufferedInputStream( inputStream );
     DeflaterOutputStream deflaterStream = new DeflaterOutputStream(
        new BufferedOutputStream( outStream = payloadData.setBinaryStream( 0 ) ),
            new Deflater( 3 ) ) )
{
    fileSize = IOUtil.copy( bis, deflaterStream );
}
catch (ExceptionsYouWantToCatch eywtc)
{
    if (outStream != null) {
        // Here you have the chance to close it
        try { outStream.close(); } catch(IOException ie){}
    }
}
7
  • My understanding is that could lead to problems if an exception is thrown when creating the BufferedOutputStream or DeflaterOutputStream. For example, if the creation of the BufferedOutputStream threw an OutOfMemoryError, the underlying BLOB stream couldn't be closed by the try-with-resources mechanism because it's not listed separately. Is that correct?
    – Nathan
    Aug 7, 2014 at 7:00
  • Yes, of course. If the BufferedInputStream constructor would throw an exception, there would be no reference returned by the constructor to call the close() method on. But you can handle this by adding a catch branch to the try and close inputstream by yourself.
    – icza
    Aug 7, 2014 at 7:05
  • Oh, and if BufferedOutputStream constructor would throw an exception (as you asked), bis.close() would still be called (bis is already created at this point hence its close() method can and will be called).
    – icza
    Aug 7, 2014 at 7:06
  • My comment was about failing to close the stream created by payloadData.setBinaryStream rather than bis
    – Nathan
    Aug 7, 2014 at 7:09
  • Yes, that won't be closed. You can go around this by storing the returned stream in a variable of yours, and in the catch branch check it and close it yourself - if you need to.
    – icza
    Aug 7, 2014 at 7:11
0

IMHO, You shouldn't have to do this but...

You don't need to declare all output streams you create, instead you can just declare the outer most ones.

try( InputStream bis = new BufferedInputStream( inputStream );
     DeflaterOutputStream deflaterStream = new DeflaterOutputStream(
         new BufferedOutputStream( payloadData.setBinaryStream( 0 ) ), new Deflater( 3 ) ) )
{
    fileSize = IOUtil.copy( bis, blobDeflaterStream );
}

This way deflatorStream will be closed, which will close the others.

3
  • This is the same as icza's answer below - I'd be interested on your thoughts about the comment I left on that answer.
    – Nathan
    Aug 7, 2014 at 7:01
  • @Nathan If you get an OutOfMemoryError the default action should be to shutdown the JVM. If you get something so critically wrong, a small resource leak is the least of your problems. Aug 7, 2014 at 7:03
  • To my knowledge, the JVM doesn't have an option to exit when an OutOfMemoryError occurs (although it does have the heap dump option). We've seen numerous cases where the system recovers after such an error (normally when one thread tries to create a very large local variable). That being said, OutOfMemoryError was just an example - there could be other reasons why the creation of the wrapping stream fails. We want the code to be as robust as possible.
    – Nathan
    Aug 7, 2014 at 7:06
0

Our final solution was to fix the FilterOutputStream ourselves:

public void close() throws IOException {
    if (!closed) {
        closed = true;
        try (OutputStream ostream = out) {
            flush();
        }
    }
}

private boolean closed = false;

This class was included in a small JAR file and added to the boot classpath: -Xbootclasspath/p:%APP_HOME%\lib\jdkFix.jar

Hopefully Oracle will acknowledge this issue is a bug and fix it in a future update of Java 8.

1
  • As per the comment on @WW.'s answer, I will probably need to keep my jdkFix.jar until JDK 9.
    – Nathan
    Jan 21, 2015 at 22:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.