title: Glassfish hangs when serving multiple blob-images from an imageservlet
body: <p>In my JSP/HTML files i use the following servlet to get blob-images from the MySQL-database.</p>
  
  <pre><code>&lt;img src="/image?id=1" /&gt;
  </code></pre>
  
  <h2>Image servlet</h2>
  
  <p>This is mapped to a imageservlet, who:<br>
  - gets a stateless session-bean injected<br>
  - uses the session-bean to lookup a product, based on the id passed in to the servlet<br>
  - streams this image out as the response</p>
  
  <pre><code>public class Image extends HttpServlet {
  
      @EJB
      private ProductLocal productBean;
  
      protected void processRequest(HttpServletRequest request, 
          HttpServletResponse response) throws ServletException, IOException {
      long id = 0;
      Product product = null;
  
      String possibleID = request.getParameter("id");
      if(possibleID == null){
          response.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
      }
  
      // Try to parse id
      try{
          id = Long.parseLong(possibleID);
          product = productBean.getById(id);
          if(product == null) throw new NullPointerException("Product not found");
      } catch(NumberFormatException e){
          response.sendError(HttpServletResponse.SC_BAD_REQUEST);
          return;
      } catch(NullPointerException e){
          response.sendError(HttpServletResponse.SC_NOT_FOUND);
          return;
      }
  
      // Serve image
      byte[] image = product.getImage();
      response.setContentType(product.getImageContentType());
      response.setContentLength(image.length);
      ServletOutputStream output = response.getOutputStream();
  
      for(int i = 0; i &lt; image.length; i++){
          output.write(image[i]);
      }
      output.flush();
      output.close();
  } 
  }
  </code></pre>
  
  <h2>ProductBean:</h2>
  
  <pre><code>@Stateless
  public class ProductBean implements ProductLocal {
  
      @PersistenceContext(unitName="xxx")
      private EntityManager em;
  
      public Product getById(long id) {
          return em.find(Product.class, id);
      }
  
  }
  </code></pre>
  
  <h2>Product (Entity-bean)</h2>
  
  <pre><code>@Entity
  public class Product implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      @Lob
      private byte[] image;
  
      private String imageContentType;
  
      /* getters and setters */
  }
  </code></pre>
  
  <h2>The problem</h2>
  
  <p>When iterating over a page of products, say 15, the servlet gets called 15 times and I consequently get the same result (although with different order on the IDs):</p>
  
  <p><a href="https://i.stack.imgur.com/Zq1mZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zq1mZ.png" alt="alt text"></a>
   </p>
  
  <p>Some images always hang until they time out (15 sec. shown in firebug above). The server is Glassfish v2.1 (integrated in Netbeans 6.7.1). At first the timeout was 30 sec, so I started setting different timeout values in Glassfish to narrow the problem. One of these timeouts were HttpService -> Keep Alive -> Timeout, which I sat (as the only one) to 15 sec. After restarting GF, firebug now reports the timeout after 15 sec. instead of the default 30. Since I put different timeouts in GF, I'm pretty sure the problem is related to Keep-Alive. Here is the rest of my settings in this tab:</p>
  
  <p><a href="https://i.stack.imgur.com/qRvRb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qRvRb.png" alt="alt text"></a>
  </p>
  
  <p>This is out of the box configuration from the version bundled with NetBeans, and I haven't done anything besides changing the timeout value. My question: is this caused by wrong settings in Glassfish, or problems with my ImageServlet or other code?</p>