Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 323107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:00:01+00:00 2026-05-12T09:00:01+00:00

I am writing a webservice that allows users to post files and then retrieve

  • 0

I am writing a webservice that allows users to post files and then retrieve them at a URL (basically think of it as the RESTful Amazon S3). The issue I came across was rather then return a byte[] from my Oracle query (Spring JDBC) I am returning an InputStream and then streaming the data back to the client in chunks. This (IMO) is a much better idea since I put no size restriction on the file and I don’t want 2GB byte arrays in memory.

At first it seemed to work fine, but I ran into a case during heavy load that sometimes a Connection would get reused before the previous servlet could send the file. It seems after the JDBC call that returned the InputStream, the Connection would be returned to the pool (Spring would call conn.close(), but not clear the associated ResultSet). So if no other request was given that Connection then the InputStream would still be valid and could be read from, but if the Connection was given to a new request then the InputStream would be null and the previous request would fail.

My solution was to create a subclass of InputStream that also takes a Connection as a constructor arg, and in the overridden public close() method also close the Connection. I had to ditch the Spring JDBC and just make a normal PreparedStatement call, otherwise Spring would always return the connection to the pool.

public class ConnectionInputStream extends InputStream {

   private Connection conn;
   private InputStream stream;

   public ConnectionInputStream(InputStream s, Connection c) {
      conn = c;
      stream = s;
   }

   // all InputStream methods call the same method on the variable stream

   @Override
   public void close() throws IOException {
      try {
         stream.close();
      } catch (IOException ioex) {
          //do something
      } finally {
         try {
             conn.close();
         } catch (SQLException sqlex) {
             //ignore
         }
      }
   }
} 

Does anyone have a more elegant solution, or see any glaring problems with my solution? Also this code wasn’t cut/paste from my actual code so if there is a typo just ignore it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:00:02+00:00Added an answer on May 12, 2026 at 9:00 am

    Unfortunately, my imagination went wild when you asked this question. I don’t know if this solution is considered more elegant. However, these classes are simple and easily re-usable so you may find a use for them if they are not satisfactory. You will see everything coming together at the end…

    public class BinaryCloseable implements Closeable {
    
        private Closeable first;
        private Closeable last;
    
        public BinaryCloseable(Closeable first, Closeable last) {
            this.first = first;
            this.last = last;
        }
    
        @Override
        public void close() throws IOException {
            try {
                first.close();
            } finally {
                last.close();
            }
        }
    
    }
    

    BinaryCloseable is used by CompositeCloseable:

    public class CompositeCloseable implements Closeable {
    
        private Closeable target;
    
        public CompositeCloseable(Closeable... closeables) {
            target = new Closeable() { public void close(){} };
            for (Closeable closeable : closeables) {
                target = new BinaryCloseable(target, closeable);
            }
        }
    
        @Override
        public void close() throws IOException {
            target.close();
        }
    
    }
    

    The ResultSetCloser closes ResultSet objects:

    public class ResultSetCloser implements Closeable {
    
        private ResultSet resultSet;
    
        public ResultSetCloser(ResultSet resultSet) {
            this.resultSet = resultSet;
        }
    
        @Override
        public void close() throws IOException {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new IOException("Exception encountered while closing result set", e);
            }
        }
    
    }
    

    The PreparedStatementCloser closes PreparedStatement objects:

    public class PreparedStatementCloser implements Closeable {
    
        private PreparedStatement preparedStatement;
    
        public PreparedStatementCloser(PreparedStatement preparedStatement) {
            this.preparedStatement = preparedStatement;
        }
    
        @Override
        public void close() throws IOException {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                throw new IOException("Exception encountered while closing prepared statement", e);
            }
        }
    
    }
    

    The ConnectionCloser closes Connection objects:

    public class ConnectionCloser implements Closeable {
    
        private Connection connection;
    
        public ConnectionCloser(Connection connection) {
            this.connection = connection;
        }
    
        @Override
        public void close() throws IOException {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new IOException("Exception encountered while closing connection", e);
            }
        }
    
    }
    

    We now refactor your original InputStream idea into:

    public class ClosingInputStream extends InputStream {
    
        private InputStream stream;
        private Closeable closer;
    
        public ClosingInputStream(InputStream stream, Closeable closer) {
            this.stream = stream;
            this.closer = closer;
        }
    
        // The other InputStream methods...
    
        @Override
        public void close() throws IOException {
            closer.close();
        }
    
    }
    

    Finally, it all comes together as:

    new ClosingInputStream(
            stream,
            new CompositeCloseable(
                    stream,
                    new ResultSetCloser(resultSet),
                    new PreparedStatementCloser(statement),
                    new ConnectionCloser(connection)
                )
        );
    

    When this ClosingInputStream‘s close() method is called, this is effectively what happens (with exception handling omitted for clarity’s sake):

    public void close() {
        try {
            try {
                try {
                    try {
                        // This is empty due to the first line in `CompositeCloseable`'s constructor
                    } finally {
                        stream.close();
                    }
                } finally {
                    resultSet.close();
                }
            } finally {
                preparedStatement.close();
            }
        } finally {
            connection.close();
        }
    }
    

    You’re now free to close as many Closeable objects as you like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 247k
  • Answers 247k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If a duplex contract is not workable in your environment,… May 13, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer Assuming you do not use authorization and no dynamic content,… May 13, 2026 at 8:34 am
  • Editorial Team
    Editorial Team added an answer I don't think there's a "magical" solution for the iPhone… May 13, 2026 at 8:34 am

Related Questions

I have an application that I am writing that modifies data on a cached
Trying to avoid re-inventing the wheel here. I have a Google Web Toolkit page
I'm writing a service that allows users to register with it and receive notifications
I am writing a client windows app which will allow files and respective metadata

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.