views:

468

answers:

4

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 is executing another request. [session-key: 7d90c02200a81f-752fe1cd-1] when executing a method on the stateful bean.

public class NewServlet extends HttpServlet {  
    @EJB  
    private ReportLocal reportBean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           String[] parameters  = fetchParameters(request);
           out.write(reportBean.constructReport(parameters));
        } finally { 
            out.close();
        }
    } 
}

In the above code, constructReport will check if it needs to open a new connection to the database specified in the Report after which a Report in HTML is constructed from a query which is built from the parameters specified.

The reason why we chose to use a stateful bean over a stateless bean was because we need to open a database connection to an unknown database and perform queries on it. With a stateless bean it seems terribly inefficient to repeatedly open and close database connections with each injected instance of the bean.

+1  A: 

Until you provide some code and the stacktrace, I'd suggest that you consider using a connection pool. If by "unknown database" you mean a database whose parameters are supplied by the end user, and hence no preconfigured connection pool is possible, you can still use the connection pool concept, rather than opening a new connection each time.

Also, theck this thread.

Bozho
+1  A: 

This is not what stateful session beans (SFSB) are intended to be used for. They are designed to hold conversation state, and are to be bound to the user's http session to hold that state, like a heavyweight alternative to storing state in the session directly.

If you want to hold things like database connections, then there are better ways to go about it.

Best option is to use a connection pool. You should always use a connection pool, and if you're running inside an application server (which, if you're using EJBs, then you are), then you can easily use your appserver's datasource configuration to create a connection pool, and use that inside your stateless session bean (SLSB).

skaffman
AFAIK, When creating a datasource on an appserver you have to know before hand that the database exists. The above connection is made to a database that a user specified we must connect to.
Burmudar
This seems terribly insecure to me.
duffymo
A: 

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.

rsp
+1  A: 

A few more precision about the ConcurrentAccessException: as per the EJB spec, access to SLSB is synchronize by the app. server. However, this is not the case of SFSB. The burden of making sure that the SFSB is not accessed concurrently is on the application developer's shoulder.

Why? Well, synchronization of SLSB is necessary only at the instance-level. That is, each particular instance of the SLSB is synchronized, but you may have multiple instance in a pool or on different node in a cluster, and concurrent request on different instances is not a problem. This is unfortunately not so easy with SFSB because of passivation/activation of instance and replication across the cluster. This is why the spec don't enforce this. Have a look at this dicussion if you are interested in the topic.

This means that using SFSB from servlet is complicated. User with multiple windows of the same session, or reloading page before the rendering finished can lead to concurrent access. Each access to the EJB that is done in a servlet need theoretically to be synchronized on the bean itself. What I did was to to create an InvocationHandler to synchronize all invocations on the particular EJB instance:

public class SynchronizatinoHandler implements InvocationHandler {

 private Object target;  // the EJB

 public SynchronizationHandler( Object bean )
 {
        target = bean;
 }

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
  {
    synchronized( target )
    {
       // invoke method to the target EJB
    }
  }

}

Then, right after you obtain the remote reference to the EJB, you wrap it with the SynchronizationHandler. This way you are sure that this particular instance will not be accessed concurrently from your app (as long as it run in only one JVM). You can also write a regular wrapper class which synchronized all the methods of the bean.

My conclusion is nevertheless: use SLSB whenever possible.

ewernli