Can i store the remote interface of an EJB in session object??

Discussions

EJB programming & troubleshooting: Can i store the remote interface of an EJB in session object??

  1.  Hi,

    Let's say i have a session bean called MySessionBean and a couple of entity beans in my application. MySessionBean acts as a wrapper around the entity beans (session facade).

    Thus, processes that read/write to and from the database have to go through the MySessionBean. Does this mean that i need to lookup the home interface of MySessionBean and call the create() method to get its remote interface in every servlet and JSP just so that I can gain access to the session bean's methods? Or is it possible that for example, right after a user logs in to my application, i create and store the remote interface MySession in the session object (i.e session.setAttribute("sessionRemote",mySession)) and simply call getAttribute("sessionRemote") to get the remote interface everytime i need it?

    Pls advice. Thanx :)
  2. The servlet spec states the following for the session object for distributable servlets:
    "• The container must accept objects that implement the Serializable interface
    • The container may choose to support storage of other designated objects in the HttpSession, such as references to Enterprise JavaBean components and transactions."

    So, keeping non-Serializable values in the session data is a concern if you run your servlets in a distributed environment.

    A better approach would be to implement a home interface cache or a remote interface pool or both; with a home interface cache, you lookup only once, with a remote interface pool, servlets and JSPs can share EJB instances over time.
  3. Other way, Just have a Class with static methods, which would make calls to appropriate Session bean methods.


    Hope this helps
    sudhi
  4. Hi,
     
      u mentioned that:
      "A better approach would be to implement a home interface cache or a remote interface pool or both; with a home interface cache, you lookup only once, with a remote interface pool, servlets and JSPs can share EJB instances over time."

      pardon me but may i ask how u implement that home interface cache and remote interface pool?
  5. An elementary home cache could be implemented as

    public class GenericHomeCache { // this is not type-safe
        private Context ctx;

        private Context getContext() {
            if (ctx == null) {
                ctx = new InitialContext();
                //...
            }
            
            return ctx;
        }
        
        private HashMap homeInterfaces = null;
        
        public EJBHome getHomeInterface(String jndiName) {
            if (homeInterfaces == null) {
                homeInterfaces = new HashMap();
            }
            
            EJBHome home = null;
            home = (EJBHome)homeInterfaces.get(jndiName);
            if (home == null) {
                home = (EJBHome)getContext().lookup(jndiName);
                homeInterfaces.put(jndiName,home);
            }
            
            return home;
        }
        
        private static SimpleHomeCache singleInstance = null;
        
        public static SimpleHomeCache getInstance() {
            if (singleInstance == null) {
                singleInstance = new SimpleHomeCache();
            }
            
            return singleInstance;
        }
    }

    Or, you can go for a type-safe version:

    public class SimpleHomeCache {
        private Context ctx;

        private Context getContext() {
            if (ctx == null) {
                ctx = new InitialContext();
                //...
            }

            return ctx;
        }

        private AnglerHome anglerHomeInterface = null;

        public AnglerHome getAnglerHomeInterface() {
            if (anglerHomeInterface == null) {
                anglerHomeInterface = (AnglerHome)getContext().lookup("ejb/fishingBook/Angler");
            }

            return anglerHomeInterface;
        }

        // repeat for other home interfaces...

        private static SimpleHomeCache singleInstance = null;

        public static SimpleHomeCache getInstance() {
            if (singleInstance == null) {
                singleInstance = new SimpleHomeCache();
            }

            return singleInstance;
        }
    }

    To beat run condition odds you would use:

    AnglerHome h = (AnglerHome)GenericHomeCache.getInstance().getHomeInterface("ejb/fishingBook/Angler");
    synchronized (h) {
     ....
    }


    or
    AnglerHome h = SimpleHomeCache.getInstance().getAnglerHomeInterface();
    synchronized (h) {
     ....
    }

    Cheers.