3 Replies Latest reply on Mar 3, 2003 5:14 PM by Joel Vogt

    How to avoid concurrent calls to stateful session beans?

    Thorsten Kitz Newbie

      Hi,

      I have got the following situation:

      I have a web application, which consists of JSPs, which are connected by one servlet. The servlet takes all the data from the JSPs, stores the data in a stateful session bean and dispatches to the next JSP page. The EJB call in the servlet looks like this

      if (session.getAttribute("ejbAHD") == null) {
      try {
      ejbAHD = ejbAHDHome.create();
      session.setAttribute("ejbAHD", ejbAHD);
      } catch (Exception e) {
      log.error("AdHocDefinitionServlet.service Error: " + e.getMessage(),e);
      }
      } else {
      ejbAHD = (AdHocDefinition)session.getAttribute("ejbAHD");
      }

      After that the servlet sets the data in the session bean.

      The problem is, when the user is not patient enough and pressed the submit button of a JSP page more than once, the servlet is also called more than once and he gets an

      javax.ejb.EJBException: Application Error: no concurrent calls on stateful beans

      error. I understand from reading in these forums, that concurrent calls are a nono for statefull session beans, but how can I avoid them in a web application? Is there an easy way to prohibit the servlet to call the session bean more than once?

      Thanks for any help,

      Thorsten.

        • 1. Re: How to avoid concurrent calls to stateful session beans?
          Peter Doornbosch Apprentice

          > I understand from reading in these forums,
          > that concurrent calls are a nono for statefull
          > session beans, but how can I avoid them in a web
          > application? Is there an easy way to prohibit the
          > servlet to call the session bean more than once?

          What about something like
          [pre]synchronized (session) {
          ejbAHD.whatever();
          }[/pre]
          Peter

          • 2. Re: How to avoid concurrent calls to stateful session beans?
            Thorsten Kitz Newbie

            Hi Peter,

            > What about something like
            > [pre]synchronized (session) {
            > ejbAHD.whatever();
            > }[/pre]


            thanks for the tip. It works great for the servlet part, but I still have problems accessing the bean properties on the JSP side. I tried to put the synchronizd block around the whole JSP code, but Tomcat seems to ignore that during compilation time. Also doing something like

            <%= synchronize (session) { ejbAHD.getMyProperty() } %>
            


            doesn't work either. I get the concurrent error message only sometimes during the first time compilation of the JSP page when I hit the same button several times, which maybe because after that the response of my system is too fast, but I don't want to take any risks in production.

            Thanks,

            Thorsten.


            • 3. Re: How to avoid concurrent calls to stateful session beans?
              Joel Vogt Master

              A good way to do this is to move all your ejb calls out of your servlet. Have your servlet use another class that will access to and from your model layer. Then make the methods on this synchronized so that you will only get one call though at a time.