How to handle concurrent stateless session bean in cluster


EJB design: How to handle concurrent stateless session bean in cluster

  1. I'm using stateless session bean to perform my business logic and persistence to the database. I would have used entity bean for the same purpose but the number of beans in memory would be too much for me to use this approach.

    In a clustered environment with 2 machines, I find that I need to make sure that 2 instances of stateless session bean performing the same operation at the same time (assuming that the EJB stub load-balance the call) do not result in 2 identical copies of data in the database. I rely on the database to tell me this through a SQLException (some duplicate primary key already exist).

    Is this the right way to go? Is there a better way? How does the application handle this if I use BMP entity bean? What will be different if it's implemented as a CMP entity bean?
  2. The problem of trying to insert same rows from 2 different stateless session beans exists with in a single server also. It will also occur if you use BMP or CMP.

    Go thru the application scenario and try to avoid such a use of the system. For our application we were expecting these sort problem for static table data which will be inserted by system administrator. Client was OK with having a single administrator making the changes to the static data.

    If that is not possible, you try one of the following 2 approaches

    1. Catch the SQLException and check whether it is because of duplicate row, if so throw an exception to the client indicating that data has been added by another bean.

    2. Check for the existing row just before the insertion. If the row is already present in DB throw an exception.

    Hope this helps.