Correct usage of Stateful Beans with Servlets

后端 未结 5 762
走了就别回头了
走了就别回头了 2020-11-30 05:06

We currently have a Stateful bean that is injected into a Servlet. The problem is that sometimes we get a Caused by: javax.ejb.ConcurrentAccessException: SessionBean i

5条回答
  •  时光说笑
    2020-11-30 05:39

    Session beans cannot be used concurrently, like skaffman said they were meant to handle state corresponding to the client session and are typically stored in the session object per client.

    Refactoring to use a database pool to handle concurrent requests to your resources is the way to go.

    In the meantime, if all you need is this trivial use, you could synchronise the call to constructReport as in:

    synchronised (reportBean) {
           out.write(reportBean.constructReport(parameters));
    }
    

    Note that this is no solution if constructReport takes a significant amount of time relative to your number of clients.

提交回复
热议问题