Discussions

EJB design: Statefull Session EJB Access Synchornization

  1. I have a particular requirement within my Portal Project where I have to use Stateful Session EJB. I have two portlets on the same page accessing the statefull session bean. When I am using parallel rendering , I get an exception because two threads are accessing the same stateful session bean. I understand that this is as per EJB Specification.

    My question is how do I synchronize access to the stateful session bean. Clients objects interace with the SFSB through a business delegate. If I synchronize the access methods in Business Delegate, this will mean all threads belonging to all users will be affected(Hope I am correct). All I want to do is synchronize the threads belonging to the same user.

    I was thinking about wrapping the EJB Handle with a custom class in order to achieve this. Because EJB Handle is for a particular instance of a SFSB, and the clients use the EJB Handle to get to the SFSB object once it is created, I was thinking about creating a custom object and storing the EJB Handle in that object. The class will have a single method
    public synchronized EJBObject getEJBObject() which inturn will call the getEJBObject in the EJBHandle object. Clients will use this custom object instead of EJB Handle directly to get to SFSB.

    Is there anything wrong with this approach? Am I missing something? Your comments and suggestions will be greatly appreciated.
  2. Hi sirnivas,
             
    >>>>>I have two portlets on the same page accessing the statefull session bean

    Can you please explain this a bit more . I am not sure that you are saying that your two portlets are having a reference to the same EJBObject. If yes then may be the portlets can call the some helper method and this method will do the jndi lookup and after that will synchronize the method invocations on the EJBObject. But on the other hand this is going to affact the performance.

    >>>>>public synchronized EJBObject getEJBObject() which inturn will call the getEJBObject in the EJBHandle object.

    I am not sure that this will solve your problem as the portlets will still have a reference to same EJBOBject. May be you can make a small test case on this and correct me if I am wrong as i am not very sure.

    Hope that helps.