English 中文(简体)
Glassfish hangs when serving multiple blob-images from an imageservlet
原标题:

In my JSP/HTML files i use the following servlet to get blob-images from the MySQL-database.

<img src="/image?id=1" />

Image servlet

This is mapped to a imageservlet, who:
- gets a stateless session-bean injected
- uses the session-bean to lookup a product, based on the id passed in to the servlet
- streams this image out as the response

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 < image.length; i++){
        output.write(image[i]);
    }
    output.flush();
    output.close();
} 
}

ProductBean:

@Stateless
public class ProductBean implements ProductLocal {

    @PersistenceContext(unitName="xxx")
    private EntityManager em;

    public Product getById(long id) {
        return em.find(Product.class, id);
    }

}

Product (Entity-bean)

@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 */
}

The problem

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):

alt text

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:

alt text

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?

问题回答

This could be related to the fact that your EJB instance retrieved in the Servlet is locking somehow other threads making requests to the same servlet. I have a couple of suggestions:

1) To know the exact answer you should take a thread dump and then you will see which thread is holding back others or what threads are busy doing.

2) Another idea is to try to retrieve EJB instance on each request. It s, of course, not the perfect solution, but maybe it will save you from some locking issues you may have. If it helps, then you can introduce a pool of EJBs latter. But in any case I would suggest going the first path to have more information on what is going on.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签